Fix README deserialization-without-macros example

Closes #150.
This commit is contained in:
Erick Tryzelaar 2015-08-31 09:23:33 -07:00
parent b7e3058078
commit 5f4a7e54bc

View File

@ -488,23 +488,23 @@ impl serde::Deserialize for PointField {
fn deserialize<D>(deserializer: &mut D) -> Result<PointField, D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<PointField, D::Error>
where D: serde::de::Deserializer where D: serde::de::Deserializer
{ {
struct FieldVisitor; struct PointFieldVisitor;
impl serde::de::Visitor for FieldVisitor { impl serde::de::Visitor for PointFieldVisitor {
type Value = Field; type Value = PointField;
fn visit_str<E>(&mut self, value: &str) -> Result<PointField, E> fn visit_str<E>(&mut self, value: &str) -> Result<PointField, E>
where E: serde::de::Error where E: serde::de::Error
{ {
match value { match value {
"x" => Ok(Field::X), "x" => Ok(PointField::X),
"y" => Ok(Field::Y), "y" => Ok(PointField::Y),
_ => Err(serde::de::Error::syntax("expected x or y")), _ => Err(serde::de::Error::syntax("expected x or y")),
} }
} }
} }
deserializer.visit(FieldVisitor) deserializer.visit(PointFieldVisitor)
} }
} }
``` ```
@ -516,7 +516,8 @@ impl serde::Deserialize for Point {
fn deserialize<D>(deserializer: &mut D) -> Result<Point, D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<Point, D::Error>
where D: serde::de::Deserializer where D: serde::de::Deserializer
{ {
deserializer.visit_struct("Point", PointVisitor) static FIELDS: &'static [&'static str] = &["x", "y"];
deserializer.visit_struct("Point", FIELDS, PointVisitor)
} }
} }
@ -525,9 +526,7 @@ struct PointVisitor;
impl serde::de::Visitor for PointVisitor { impl serde::de::Visitor for PointVisitor {
type Value = Point; type Value = Point;
fn visit_struct<V>(&mut self, fn visit_map<V>(&mut self, mut visitor: V) -> Result<Point, V::Error>
_fields: &[&str],
mut visitor: V) -> Result<Point, V::Error>
where V: serde::de::MapVisitor where V: serde::de::MapVisitor
{ {
let mut x = None; let mut x = None;
@ -535,8 +534,8 @@ impl serde::de::Visitor for PointVisitor {
loop { loop {
match try!(visitor.visit_key()) { match try!(visitor.visit_key()) {
Some(Field::X) => { x = Some(try!(visitor.visit_value())); } Some(PointField::X) => { x = Some(try!(visitor.visit_value())); }
Some(Field::Y) => { y = Some(try!(visitor.visit_value())); } Some(PointField::Y) => { y = Some(try!(visitor.visit_value())); }
None => { break; } None => { break; }
} }
} }