mirror of
https://github.com/serde-rs/serde.git
synced 2025-09-30 22:41:31 +00:00
fix untestable docs
This commit is contained in:
parent
8c30ec9698
commit
3a687e5369
55
README.md
55
README.md
@ -356,11 +356,8 @@ impl serde::Serialize for i32 {
|
|||||||
```
|
```
|
||||||
|
|
||||||
As you can see it's pretty simple. More complex types like `BTreeMap` need to
|
As you can see it's pretty simple. More complex types like `BTreeMap` need to
|
||||||
pass a
|
use a multi-step process (init, elements, end) in order to walk through the
|
||||||
[MapVisitor](http://serde-rs.github.io/serde/serde/serde/ser/trait.MapVisitor.html)
|
type:
|
||||||
to the
|
|
||||||
[Serializer](http://serde-rs.github.io/serde/serde/serde/ser/trait.Serializer.html)
|
|
||||||
in order to walk through the type:
|
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
impl<K, V> Serialize for BTreeMap<K, V>
|
impl<K, V> Serialize for BTreeMap<K, V>
|
||||||
@ -371,55 +368,17 @@ impl<K, V> Serialize for BTreeMap<K, V>
|
|||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
where S: Serializer,
|
where S: Serializer,
|
||||||
{
|
{
|
||||||
serializer.serialize_map(MapIteratorVisitor::new(self.iter(), Some(self.len())))
|
let mut state = try!(serializer.serialize_map(Some(self.len())));
|
||||||
}
|
for (k, v) in self {
|
||||||
}
|
try!(serializer.serialize_map_elt(&mut state, k, v));
|
||||||
|
|
||||||
pub struct MapIteratorVisitor<Iter> {
|
|
||||||
iter: Iter,
|
|
||||||
len: Option<usize>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<K, V, Iter> MapIteratorVisitor<Iter>
|
|
||||||
where Iter: Iterator<Item=(K, V)>
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
pub fn new(iter: Iter, len: Option<usize>) -> MapIteratorVisitor<Iter> {
|
|
||||||
MapIteratorVisitor {
|
|
||||||
iter: iter,
|
|
||||||
len: len,
|
|
||||||
}
|
}
|
||||||
}
|
serializer.serialize_map_end(state)
|
||||||
}
|
|
||||||
|
|
||||||
impl<K, V, I> MapVisitor for MapIteratorVisitor<I>
|
|
||||||
where K: Serialize,
|
|
||||||
V: Serialize,
|
|
||||||
I: Iterator<Item=(K, V)>,
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
|
||||||
where S: Serializer,
|
|
||||||
{
|
|
||||||
match self.iter.next() {
|
|
||||||
Some((key, value)) => {
|
|
||||||
let value = try!(serializer.serialize_map_elt(key, value));
|
|
||||||
Ok(Some(value))
|
|
||||||
}
|
|
||||||
None => Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn len(&self) -> Option<usize> {
|
|
||||||
self.len
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Serializing structs follow this same pattern. In fact, structs are represented
|
Serializing structs follow this same pattern. In fact, structs are represented
|
||||||
as a named map. Its visitor uses a simple state machine to iterate through all
|
as a named map, with a known length.
|
||||||
the fields:
|
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user