From 73a9dd86b913fc4dd5b7283c2c06e3684ac672b0 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 8 Dec 2014 23:13:17 -0800 Subject: [PATCH] wip: refactor --- serde2/benches/bench_log.rs | 94 +++++++++++++++------------- serde2/src/bin.rs | 95 +++++++++++++++------------- serde2/src/de.rs | 120 +++++++++++++++++------------------ serde2/src/json/de.rs | 121 ++++++++++++++++++------------------ 4 files changed, 223 insertions(+), 207 deletions(-) diff --git a/serde2/benches/bench_log.rs b/serde2/benches/bench_log.rs index 3a905073..408ca7f6 100644 --- a/serde2/benches/bench_log.rs +++ b/serde2/benches/bench_log.rs @@ -38,18 +38,18 @@ struct Http { impl< S: Deserializer, - E, -> Deserialize for Http { + E: de::Error, +> de::Deserialize for Http { fn deserialize(state: &mut S) -> Result { struct Visitor; impl< S: Deserializer, - E, + E: de::Error, > de::Visitor for Visitor { fn visit_map< Visitor: de::MapVisitor, - >(&mut self, state: &mut S, mut visitor: Visitor) -> Result { + >(&mut self, mut visitor: Visitor) -> Result { let mut protocol = None; let mut status = None; let mut host_status = None; @@ -61,19 +61,19 @@ impl< let mut request_uri = None; loop { - match try!(visitor.visit_key(state)) { + match try!(visitor.visit_key()) { Some(s) => { let s: String = s; match s.as_slice() { - "protocol" => { protocol = Some(try!(visitor.visit_value(state))); } - "status" => { status = Some(try!(visitor.visit_value(state))); } - "host_status" => { host_status = Some(try!(visitor.visit_value(state))); } - "up_status" => { up_status = Some(try!(visitor.visit_value(state))); } - "method" => { method = Some(try!(visitor.visit_value(state))); } - "content_type" => { content_type = Some(try!(visitor.visit_value(state))); } - "user_agent" => { user_agent = Some(try!(visitor.visit_value(state))); } - "referer" => { referer = Some(try!(visitor.visit_value(state))); } - "request_uri" => { request_uri = Some(try!(visitor.visit_value(state))); } + "protocol" => { protocol = Some(try!(visitor.visit_value())); } + "status" => { status = Some(try!(visitor.visit_value())); } + "host_status" => { host_status = Some(try!(visitor.visit_value())); } + "up_status" => { up_status = Some(try!(visitor.visit_value())); } + "method" => { method = Some(try!(visitor.visit_value())); } + "content_type" => { content_type = Some(try!(visitor.visit_value())); } + "user_agent" => { user_agent = Some(try!(visitor.visit_value())); } + "referer" => { referer = Some(try!(visitor.visit_value())); } + "request_uri" => { request_uri = Some(try!(visitor.visit_value())); } _ => panic!(), } } @@ -133,7 +133,10 @@ impl ser::Serialize for HttpProtocol { } } -impl, E> de::Deserialize for HttpProtocol { +impl< + S: de::Deserializer, + E: de::Error, +> de::Deserialize for HttpProtocol { #[inline] fn deserialize(state: &mut S) -> Result { de::deserialize_from_primitive(state) @@ -182,7 +185,10 @@ impl ser::Serialize for HttpMethod { } } -impl, E> de::Deserialize for HttpMethod { +impl< + S: de::Deserializer, + E: de::Error, +> de::Deserialize for HttpMethod { #[inline] fn deserialize(state: &mut S) -> Result { de::deserialize_from_primitive(state) @@ -224,7 +230,7 @@ impl ser::Serialize for CacheStatus { } } -impl, E> de::Deserialize for CacheStatus { +impl, E: de::Error> de::Deserialize for CacheStatus { #[inline] fn deserialize(state: &mut S) -> Result { de::deserialize_from_primitive(state) @@ -243,32 +249,32 @@ struct Origin { impl< S: Deserializer, - E, + E: de::Error, > Deserialize for Origin { fn deserialize(state: &mut S) -> Result { struct Visitor; impl< S: Deserializer, - E, + E: de::Error, > de::Visitor for Visitor { fn visit_map< Visitor: de::MapVisitor, - >(&mut self, state: &mut S, mut visitor: Visitor) -> Result { + >(&mut self, mut visitor: Visitor) -> Result { let mut ip = None; let mut port = None; let mut hostname = None; let mut protocol = None; loop { - match try!(visitor.visit_key(state)) { + match try!(visitor.visit_key()) { Some(s) => { let s: String = s; match s.as_slice() { - "ip" => { ip = Some(try!(visitor.visit_value(state))); } - "port" => { port = Some(try!(visitor.visit_value(state))); } - "hostname" => { hostname = Some(try!(visitor.visit_value(state))); } - "protocol" => { protocol = Some(try!(visitor.visit_value(state))); } + "ip" => { ip = Some(try!(visitor.visit_value())); } + "port" => { port = Some(try!(visitor.visit_value())); } + "hostname" => { hostname = Some(try!(visitor.visit_value())); } + "protocol" => { protocol = Some(try!(visitor.visit_value())); } _ => panic!(), } } @@ -323,7 +329,7 @@ impl ser::Serialize for OriginProtocol { } } -impl, E> de::Deserialize for OriginProtocol { +impl, E: de::Error> de::Deserialize for OriginProtocol { #[inline] fn deserialize(state: &mut S) -> Result { de::deserialize_from_primitive(state) @@ -366,7 +372,7 @@ impl ser::Serialize for ZonePlan { } } -impl, E> de::Deserialize for ZonePlan { +impl, E: de::Error> de::Deserialize for ZonePlan { #[inline] fn deserialize(state: &mut S) -> Result { de::deserialize_from_primitive(state) @@ -660,7 +666,7 @@ impl ser::Serialize for Country { } } -impl, E> de::Deserialize for Country { +impl, E: de::Error> de::Deserialize for Country { #[inline] fn deserialize(state: &mut S) -> Result { de::deserialize_from_primitive(state) @@ -687,18 +693,18 @@ struct Log { impl< S: Deserializer, - E, + E: de::Error, > Deserialize for Log { fn deserialize(state: &mut S) -> Result { struct Visitor; impl< S: Deserializer, - E, + E: de::Error, > de::Visitor for Visitor { fn visit_map< Visitor: de::MapVisitor, - >(&mut self, state: &mut S, mut visitor: Visitor) -> Result { + >(&mut self, mut visitor: Visitor) -> Result { let mut timestamp = None; let mut zone_id = None; let mut zone_plan = None; @@ -713,22 +719,22 @@ impl< let mut ray_id = None; loop { - match try!(visitor.visit_key(state)) { + match try!(visitor.visit_key()) { Some(s) => { let s: String = s; match s.as_slice() { - "timestamp" => { timestamp = Some(try!(visitor.visit_value(state))); } - "zone_id" => { zone_id = Some(try!(visitor.visit_value(state))); } - "zone_plan" => { zone_plan = Some(try!(visitor.visit_value(state))); } - "http" => { http = Some(try!(visitor.visit_value(state))); } - "origin" => { origin = Some(try!(visitor.visit_value(state))); } - "country" => { country = Some(try!(visitor.visit_value(state))); } - "cache_status" => { cache_status = Some(try!(visitor.visit_value(state))); } - "server_ip" => { server_ip = Some(try!(visitor.visit_value(state))); } - "server_name" => { server_name = Some(try!(visitor.visit_value(state))); } - "remote_ip" => { remote_ip = Some(try!(visitor.visit_value(state))); } - "bytes_dlv" => { bytes_dlv = Some(try!(visitor.visit_value(state))); } - "ray_id" => { ray_id = Some(try!(visitor.visit_value(state))); } + "timestamp" => { timestamp = Some(try!(visitor.visit_value())); } + "zone_id" => { zone_id = Some(try!(visitor.visit_value())); } + "zone_plan" => { zone_plan = Some(try!(visitor.visit_value())); } + "http" => { http = Some(try!(visitor.visit_value())); } + "origin" => { origin = Some(try!(visitor.visit_value())); } + "country" => { country = Some(try!(visitor.visit_value())); } + "cache_status" => { cache_status = Some(try!(visitor.visit_value())); } + "server_ip" => { server_ip = Some(try!(visitor.visit_value())); } + "server_name" => { server_name = Some(try!(visitor.visit_value())); } + "remote_ip" => { remote_ip = Some(try!(visitor.visit_value())); } + "bytes_dlv" => { bytes_dlv = Some(try!(visitor.visit_value())); } + "ray_id" => { ray_id = Some(try!(visitor.visit_value())); } _ => panic!(), } } diff --git a/serde2/src/bin.rs b/serde2/src/bin.rs index 608d8443..8653afe8 100644 --- a/serde2/src/bin.rs +++ b/serde2/src/bin.rs @@ -77,24 +77,22 @@ impl> Deserializer for MyDeserializer { match self.next() { Some(Token::Null) => { - visitor.visit_null(self) + visitor.visit_null() } Some(Token::Int(v)) => { - visitor.visit_int(self, v) + visitor.visit_int(v) } Some(Token::String(v)) => { - visitor.visit_string(self, v) + visitor.visit_string(v) } Some(Token::Option(is_some)) => { - visitor.visit_option(self, MyOptionVisitor { - is_some: is_some, - }) + visitor.visit_option(MyOptionVisitor { d: self, is_some: is_some }) } Some(Token::SeqStart(len)) => { - visitor.visit_seq(self, MySeqVisitor { len: len }) + visitor.visit_seq(MySeqVisitor { d: self, len: len }) } Some(Token::MapStart(len)) => { - visitor.visit_map(self, MyMapVisitor { len: len }) + visitor.visit_map(MyMapVisitor { d: self, len: len }) } Some(Token::End) => { Err(Error::syntax_error()) @@ -112,18 +110,21 @@ impl> Deserializer for MyDeserializer { match self.peek() { Some(&Token::Null) => { self.next(); - visitor.visit_option(self, MyOptionVisitor { + visitor.visit_option(MyOptionVisitor { + d: self, is_some: false, }) } Some(&Token::Option(is_some)) => { self.next(); - visitor.visit_option(self, MyOptionVisitor { + visitor.visit_option(MyOptionVisitor { + d: self, is_some: is_some, }) } _ => { - visitor.visit_option(self, MyOptionVisitor { + visitor.visit_option(MyOptionVisitor { + d: self, is_some: true, }) } @@ -131,19 +132,21 @@ impl> Deserializer for MyDeserializer { } } -struct MyOptionVisitor { +struct MyOptionVisitor<'a, Iter: 'a> { + d: &'a mut MyDeserializer, is_some: bool, } impl< + 'a, Iter: Iterator, -> de::OptionVisitor, Error> for MyOptionVisitor { +> de::OptionVisitor, Error> for MyOptionVisitor<'a, Iter> { fn visit< T: Deserialize, Error>, - >(&mut self, d: &mut MyDeserializer) -> Result, Error> { + >(&mut self) -> Result, Error> { if self.is_some { self.is_some = false; - let value = try!(Deserialize::deserialize(d)); + let value = try!(Deserialize::deserialize(self.d)); Ok(Some(value)) } else { Ok(None) @@ -151,26 +154,28 @@ impl< } } -struct MySeqVisitor { +struct MySeqVisitor<'a, Iter: 'a> { + d: &'a mut MyDeserializer, len: uint, } impl< + 'a, Iter: Iterator, -> de::SeqVisitor, Error> for MySeqVisitor { +> de::SeqVisitor, Error> for MySeqVisitor<'a, Iter> { fn visit< T: Deserialize, Error> - >(&mut self, d: &mut MyDeserializer) -> Result, Error> { + >(&mut self) -> Result, Error> { use serde2::de::Error; - match d.peek() { + match self.d.peek() { Some(&Token::End) => { - d.next(); + self.d.next(); Ok(None) } Some(_) => { self.len -= 1; - let value = try!(Deserialize::deserialize(d)); + let value = try!(Deserialize::deserialize(self.d)); Ok(Some(value)) } None => { @@ -179,42 +184,44 @@ impl< } } - fn end(&mut self, d: &mut MyDeserializer) -> Result<(), Error> { + fn end(&mut self) -> Result<(), Error> { use serde2::de::Error; - match d.next() { + match self.d.next() { Some(Token::End) => Ok(()), Some(_) => Err(Error::syntax_error()), None => Err(Error::end_of_stream_error()), } } - fn size_hint(&self, _d: &mut MyDeserializer) -> (uint, option::Option) { + fn size_hint(&self) -> (uint, option::Option) { (self.len, Some(self.len)) } } -struct MyMapVisitor { +struct MyMapVisitor<'a, Iter: 'a> { + d: &'a mut MyDeserializer, len: uint, } impl< + 'a, Iter: Iterator, -> de::MapVisitor, Error> for MyMapVisitor { +> de::MapVisitor, Error> for MyMapVisitor<'a, Iter> { fn visit_key< K: Deserialize, Error>, - >(&mut self, d: &mut MyDeserializer) -> Result, Error> { + >(&mut self) -> Result, Error> { use serde2::de::Error; - match d.peek() { + match self.d.peek() { Some(&Token::End) => { - d.next(); + self.d.next(); Ok(None) } Some(_) => { self.len -= 1; - Ok(Some(try!(Deserialize::deserialize(d)))) + Ok(Some(try!(Deserialize::deserialize(self.d)))) } None => { Err(Error::syntax_error()) @@ -224,21 +231,21 @@ impl< fn visit_value< V: Deserialize, Error>, - >(&mut self, d: &mut MyDeserializer) -> Result { - Ok(try!(Deserialize::deserialize(d))) + >(&mut self) -> Result { + Ok(try!(Deserialize::deserialize(self.d))) } - fn end(&mut self, d: &mut MyDeserializer) -> Result<(), Error> { + fn end(&mut self) -> Result<(), Error> { use serde2::de::Error; - match d.next() { + match self.d.next() { Some(Token::End) => Ok(()), Some(_) => Err(Error::syntax_error()), None => Err(Error::end_of_stream_error()), } } - fn size_hint(&self, _d: &mut MyDeserializer) -> (uint, option::Option) { + fn size_hint(&self) -> (uint, option::Option) { (self.len, Some(self.len)) } } @@ -270,11 +277,11 @@ mod json { D: de::Deserializer, E: de::Error, > de::Visitor for Visitor { - fn visit_null(&mut self, _d: &mut D) -> Result { + fn visit_null(&mut self) -> Result { Ok(Value::Null) } - fn visit_int(&mut self, _d: &mut D, v: int) -> Result { + fn visit_int(&mut self, v: int) -> Result { Ok(Value::Int(v)) } @@ -286,8 +293,8 @@ mod json { fn visit_option< Visitor: de::OptionVisitor, - >(&mut self, d: &mut D, mut visitor: Visitor) -> Result { - match try!(visitor.visit(d)) { + >(&mut self, mut visitor: Visitor) -> Result { + match try!(visitor.visit()) { Some(value) => Ok(value), None => Ok(Value::Null), } @@ -295,12 +302,12 @@ mod json { fn visit_seq< Visitor: de::SeqVisitor, - >(&mut self, d: &mut D, mut visitor: Visitor) -> Result { - let (len, _) = visitor.size_hint(d); + >(&mut self, mut visitor: Visitor) -> Result { + let (len, _) = visitor.size_hint(); let mut values = Vec::with_capacity(len); loop { - match try!(visitor.visit(d)) { + match try!(visitor.visit()) { Some(value) => { values.push(value); } @@ -315,11 +322,11 @@ mod json { fn visit_map< Visitor: de::MapVisitor, - >(&mut self, d: &mut D, mut visitor: Visitor) -> Result { + >(&mut self, mut visitor: Visitor) -> Result { let mut values = TreeMap::new(); loop { - match try!(visitor.visit(d)) { + match try!(visitor.visit()) { Some((key, value)) => { values.insert(key, value); } diff --git a/serde2/src/de.rs b/serde2/src/de.rs index ed317410..5728339b 100644 --- a/serde2/src/de.rs +++ b/serde2/src/de.rs @@ -28,85 +28,85 @@ pub trait Deserializer { } pub trait Visitor, R, E: Error> { - fn visit_null(&mut self, state: &mut S) -> Result { + fn visit_null(&mut self) -> Result { Err(Error::syntax_error()) } - fn visit_bool(&mut self, state: &mut S, _v: bool) -> Result { + fn visit_bool(&mut self, _v: bool) -> Result { Err(Error::syntax_error()) } - fn visit_int(&mut self, state: &mut S, v: int) -> Result { - self.visit_i64(state, v as i64) + fn visit_int(&mut self, v: int) -> Result { + self.visit_i64(v as i64) } - fn visit_i8(&mut self, state: &mut S, v: i8) -> Result { - self.visit_i64(state, v as i64) + fn visit_i8(&mut self, v: i8) -> Result { + self.visit_i64(v as i64) } - fn visit_i16(&mut self, state: &mut S, v: i16) -> Result { - self.visit_i64(state, v as i64) + fn visit_i16(&mut self, v: i16) -> Result { + self.visit_i64(v as i64) } - fn visit_i32(&mut self, state: &mut S, v: i32) -> Result { - self.visit_i64(state, v as i64) + fn visit_i32(&mut self, v: i32) -> Result { + self.visit_i64(v as i64) } - fn visit_i64(&mut self, state: &mut S, _v: i64) -> Result { + fn visit_i64(&mut self, _v: i64) -> Result { Err(Error::syntax_error()) } - fn visit_uint(&mut self, state: &mut S, v: uint) -> Result { - self.visit_u64(state, v as u64) + fn visit_uint(&mut self, v: uint) -> Result { + self.visit_u64(v as u64) } - fn visit_u8(&mut self, state: &mut S, v: u8) -> Result { - self.visit_u64(state, v as u64) + fn visit_u8(&mut self, v: u8) -> Result { + self.visit_u64(v as u64) } - fn visit_u16(&mut self, state: &mut S, v: u16) -> Result { - self.visit_u64(state, v as u64) + fn visit_u16(&mut self, v: u16) -> Result { + self.visit_u64(v as u64) } - fn visit_u32(&mut self, state: &mut S, v: u32) -> Result { - self.visit_u64(state, v as u64) + fn visit_u32(&mut self, v: u32) -> Result { + self.visit_u64(v as u64) } - fn visit_u64(&mut self, state: &mut S, _v: u64) -> Result { + fn visit_u64(&mut self, _v: u64) -> Result { Err(Error::syntax_error()) } - fn visit_f32(&mut self, state: &mut S, v: f32) -> Result { - self.visit_f64(state, v as f64) + fn visit_f32(&mut self, v: f32) -> Result { + self.visit_f64(v as f64) } - fn visit_f64(&mut self, state: &mut S, _v: f64) -> Result { + fn visit_f64(&mut self, _v: f64) -> Result { Err(Error::syntax_error()) } - fn visit_str(&mut self, state: &mut S, _v: &str) -> Result { + fn visit_str(&mut self, _v: &str) -> Result { Err(Error::syntax_error()) } - fn visit_string(&mut self, state: &mut S, v: String) -> Result { - self.visit_str(state, v.as_slice()) + fn visit_string(&mut self, v: String) -> Result { + self.visit_str(v.as_slice()) } fn visit_option< V: OptionVisitor, - >(&mut self, state: &mut S, _visitor: V) -> Result { + >(&mut self, _visitor: V) -> Result { Err(Error::syntax_error()) } fn visit_seq< V: SeqVisitor, - >(&mut self, state: &mut S, _visitor: V) -> Result { + >(&mut self, _visitor: V) -> Result { Err(Error::syntax_error()) } fn visit_map< V: MapVisitor, - >(&mut self, state: &mut S, _visitor: V) -> Result { + >(&mut self, _visitor: V) -> Result { Err(Error::syntax_error()) } } @@ -114,18 +114,18 @@ pub trait Visitor, R, E: Error> { pub trait OptionVisitor { fn visit< T: Deserialize, - >(&mut self, state: &mut S) -> Result, E>; + >(&mut self) -> Result, E>; } pub trait SeqVisitor { fn visit< T: Deserialize, - >(&mut self, state: &mut S) -> Result, E>; + >(&mut self) -> Result, E>; - fn end(&mut self, state: &mut S) -> Result<(), E>; + fn end(&mut self) -> Result<(), E>; #[inline] - fn size_hint(&self, _state: &mut S) -> (uint, Option) { + fn size_hint(&self) -> (uint, Option) { (0, None) } } @@ -134,10 +134,10 @@ pub trait MapVisitor { fn visit< K: Deserialize, V: Deserialize, - >(&mut self, state: &mut S) -> Result, E> { - match try!(self.visit_key(state)) { + >(&mut self) -> Result, E> { + match try!(self.visit_key()) { Some(key) => { - let value = try!(self.visit_value(state)); + let value = try!(self.visit_value()); Ok(Some((key, value))) } None => Ok(None) @@ -146,16 +146,16 @@ pub trait MapVisitor { fn visit_key< K: Deserialize, - >(&mut self, state: &mut S) -> Result, E>; + >(&mut self) -> Result, E>; fn visit_value< V: Deserialize, - >(&mut self, state: &mut S) -> Result; + >(&mut self) -> Result; - fn end(&mut self, state: &mut S) -> Result<(), E>; + fn end(&mut self) -> Result<(), E>; #[inline] - fn size_hint(&self, _state: &mut S) -> (uint, Option) { + fn size_hint(&self) -> (uint, Option) { (0, None) } } @@ -173,14 +173,14 @@ impl< S: Deserializer, E: Error, > self::Visitor for Visitor { - fn visit_null(&mut self, _state: &mut S) -> Result<(), E> { + fn visit_null(&mut self) -> Result<(), E> { Ok(()) } fn visit_seq< V: SeqVisitor, - >(&mut self, state: &mut S, mut visitor: V) -> Result<(), E> { - try!(visitor.end(state)); + >(&mut self, mut visitor: V) -> Result<(), E> { + try!(visitor.end()); Ok(()) } } @@ -202,7 +202,7 @@ impl< S: Deserializer, E: Error, > self::Visitor for Visitor { - fn visit_bool(&mut self, _state: &mut S, v: bool) -> Result { + fn visit_bool(&mut self, v: bool) -> Result { Ok(v) } } @@ -215,7 +215,7 @@ impl< macro_rules! impl_deserialize_num_method { ($src_ty:ty, $method:ident, $from_method:ident) => { - fn $method(&mut self, state: &mut S, v: $src_ty) -> Result { + fn $method(&mut self, v: $src_ty) -> Result { match FromPrimitive::$from_method(v) { Some(v) => Ok(v), None => Err(Error::syntax_error()), @@ -294,11 +294,11 @@ impl< S: Deserializer, E: Error, > self::Visitor for Visitor { - fn visit_str(&mut self, _state: &mut S, v: &str) -> Result { + fn visit_str(&mut self, v: &str) -> Result { Ok(v.to_string()) } - fn visit_string(&mut self, _state: &mut S, v: String) -> Result { + fn visit_string(&mut self, v: String) -> Result { Ok(v) } } @@ -324,8 +324,8 @@ impl< > self::Visitor, E> for Visitor { fn visit_option< V: OptionVisitor, - >(&mut self, state: &mut S, mut visitor: V) -> Result, E> { - visitor.visit(state) + >(&mut self, mut visitor: V) -> Result, E> { + visitor.visit() } } @@ -350,12 +350,12 @@ impl< > self::Visitor, E> for Visitor { fn visit_seq< V: SeqVisitor, - >(&mut self, state: &mut S, mut visitor: V) -> Result, E> { - let (len, _) = visitor.size_hint(state); + >(&mut self, mut visitor: V) -> Result, E> { + let (len, _) = visitor.size_hint(); let mut values = Vec::with_capacity(len); loop { - match try!(visitor.visit(state)) { + match try!(visitor.visit()) { Some(value) => { values.push(value); } @@ -403,15 +403,15 @@ macro_rules! impl_deserialize_tuple { > self::Visitor for Visitor { fn visit_seq< V: SeqVisitor, - >(&mut self, state: &mut S, mut visitor: V) -> Result<($($name,)+), E> { + >(&mut self, mut visitor: V) -> Result<($($name,)+), E> { $( - let $name = match try!(visitor.visit(state)) { + let $name = match try!(visitor.visit()) { Some(value) => value, None => { return Err(Error::end_of_stream_error()); } }; )+; - try!(visitor.end(state)); + try!(visitor.end()); Ok(($($name,)+)) } @@ -444,12 +444,12 @@ impl< > self::Visitor, E> for Visitor { fn visit_map< Visitor: MapVisitor, - >(&mut self, state: &mut S, mut visitor: Visitor) -> Result, E> { - let (len, _) = visitor.size_hint(state); + >(&mut self, mut visitor: Visitor) -> Result, E> { + let (len, _) = visitor.size_hint(); let mut values = HashMap::with_capacity(len); loop { - match try!(visitor.visit(state)) { + match try!(visitor.visit()) { Some((key, value)) => { values.insert(key, value); } @@ -484,11 +484,11 @@ impl< > self::Visitor, E> for Visitor { fn visit_map< Visitor: MapVisitor, - >(&mut self, state: &mut S, mut visitor: Visitor) -> Result, E> { + >(&mut self, mut visitor: Visitor) -> Result, E> { let mut values = TreeMap::new(); loop { - match try!(visitor.visit(state)) { + match try!(visitor.visit()) { Some((key, value)) => { values.insert(key, value); } diff --git a/serde2/src/json/de.rs b/serde2/src/json/de.rs index 6b91e00a..1260283d 100644 --- a/serde2/src/json/de.rs +++ b/serde2/src/json/de.rs @@ -94,29 +94,30 @@ impl> Parser { match self.ch_or_null() { b'n' => { try!(self.parse_ident(b"ull")); - visitor.visit_null(self) + visitor.visit_null() } b't' => { try!(self.parse_ident(b"rue")); - visitor.visit_bool(self, true) + visitor.visit_bool(true) } b'f' => { try!(self.parse_ident(b"alse")); - visitor.visit_bool(self, false) + visitor.visit_bool(false) } b'0' ... b'9' | b'-' => self.parse_number(visitor), b'"' => { try!(self.parse_string()); - let s = String::from_utf8(self.buf.clone()).unwrap(); - visitor.visit_string(self, s) + //let s = String::from_utf8(self.buf.clone()).unwrap(); + let s = str::from_utf8(self.buf.as_slice()).unwrap(); + visitor.visit_str(s) } b'[' => { self.bump(); - visitor.visit_seq(self, SeqVisitor { first: true }) + visitor.visit_seq(SeqVisitor { parser: self, first: true }) } b'{' => { self.bump(); - visitor.visit_map(self, MapVisitor { first: true }) + visitor.visit_map(MapVisitor { parser: self, first: true }) } _ => { Err(self.error(ErrorCode::ExpectedSomeValue)) @@ -160,9 +161,9 @@ impl> Parser { res = try!(self.parse_exponent(res)); } - visitor.visit_f64(self, neg * res) + visitor.visit_f64(neg * res) } else { - visitor.visit_i64(self, neg * res) + visitor.visit_i64(neg * res) } } @@ -389,114 +390,116 @@ impl> Deserializer for Parser { } } -struct SeqVisitor { +struct SeqVisitor<'a, Iter: 'a> { + parser: &'a mut Parser, first: bool, } -impl> de::SeqVisitor, Error> for SeqVisitor { +impl<'a, Iter: Iterator> de::SeqVisitor, Error> for SeqVisitor<'a, Iter> { fn visit< T: de::Deserialize, Error>, - >(&mut self, d: &mut Parser) -> Result, Error> { - d.parse_whitespace(); + >(&mut self) -> Result, Error> { + self.parser.parse_whitespace(); - if d.ch_is(b']') { - d.bump(); + if self.parser.ch_is(b']') { + self.parser.bump(); return Ok(None); } if self.first { self.first = false; } else { - if d.ch_is(b',') { - d.bump(); - } else if d.eof() { - return Err(d.error(ErrorCode::EOFWhileParsingList)); + if self.parser.ch_is(b',') { + self.parser.bump(); + } else if self.parser.eof() { + return Err(self.parser.error(ErrorCode::EOFWhileParsingList)); } else { - return Err(d.error(ErrorCode::ExpectedListCommaOrEnd)); + return Err(self.parser.error(ErrorCode::ExpectedListCommaOrEnd)); } } - let value = try!(de::Deserialize::deserialize(d)); + let value = try!(de::Deserialize::deserialize(self.parser)); Ok(Some(value)) } - fn end(&mut self, d: &mut Parser) -> Result<(), Error> { - if d.ch_is(b']') { - d.bump(); + fn end(&mut self) -> Result<(), Error> { + if self.parser.ch_is(b']') { + self.parser.bump(); Ok(()) - } else if d.eof() { - Err(d.error(ErrorCode::EOFWhileParsingList)) + } else if self.parser.eof() { + Err(self.parser.error(ErrorCode::EOFWhileParsingList)) } else { - Err(d.error(ErrorCode::TrailingCharacters)) + Err(self.parser.error(ErrorCode::TrailingCharacters)) } } } -struct MapVisitor { +struct MapVisitor<'a, Iter: 'a> { + parser: &'a mut Parser, first: bool, } -impl> de::MapVisitor, Error> for MapVisitor { +impl<'a, Iter: Iterator> de::MapVisitor, Error> for MapVisitor<'a, Iter> { fn visit_key< K: de::Deserialize, Error>, - >(&mut self, d: &mut Parser) -> Result, Error> { - d.parse_whitespace(); + >(&mut self) -> Result, Error> { + self.parser.parse_whitespace(); - if d.ch_is(b'}') { - d.bump(); + if self.parser.ch_is(b'}') { + self.parser.bump(); return Ok(None); } if self.first { self.first = false; } else { - if d.ch_is(b',') { - d.bump(); - d.parse_whitespace(); - } else if d.eof() { - return Err(d.error(ErrorCode::EOFWhileParsingObject)); + if self.parser.ch_is(b',') { + self.parser.bump(); + self.parser.parse_whitespace(); + } else if self.parser.eof() { + return Err(self.parser.error(ErrorCode::EOFWhileParsingObject)); } else { - return Err(d.error(ErrorCode::ExpectedObjectCommaOrEnd)); + return Err(self.parser.error(ErrorCode::ExpectedObjectCommaOrEnd)); } } - if d.eof() { - return Err(d.error(ErrorCode::EOFWhileParsingValue)); + if self.parser.eof() { + return Err(self.parser.error(ErrorCode::EOFWhileParsingValue)); } - if !d.ch_is(b'"') { - return Err(d.error(ErrorCode::KeyMustBeAString)); + if !self.parser.ch_is(b'"') { + return Err(self.parser.error(ErrorCode::KeyMustBeAString)); } - Ok(Some(try!(de::Deserialize::deserialize(d)))) + Ok(Some(try!(de::Deserialize::deserialize(self.parser)))) } fn visit_value< V: de::Deserialize, Error>, - >(&mut self, d: &mut Parser) -> Result { - d.parse_whitespace(); + >(&mut self) -> Result { + self.parser.parse_whitespace(); - if d.ch_is(b':') { - d.bump(); - } else if d.eof() { - return Err(d.error(ErrorCode::EOFWhileParsingObject)); + if self.parser.ch_is(b':') { + self.parser.bump(); + } else if self.parser.eof() { + return Err(self.parser.error(ErrorCode::EOFWhileParsingObject)); } else { - return Err(d.error(ErrorCode::ExpectedColon)); + return Err(self.parser.error(ErrorCode::ExpectedColon)); } - d.parse_whitespace(); + self.parser.parse_whitespace(); - Ok(try!(de::Deserialize::deserialize(d))) + Ok(try!(de::Deserialize::deserialize(self.parser))) } - fn end(&mut self, d: &mut Parser) -> Result<(), Error> { - if d.ch_is(b']') { - d.bump(); + fn end(&mut self) -> Result<(), Error> { + if self.parser.ch_is(b']') { + self.parser.bump(); Ok(()) - } else if d.eof() { - Err(d.error(ErrorCode::EOFWhileParsingList)) + } else if self.parser.eof() { + Err(self.parser.error(ErrorCode::EOFWhileParsingList)) } else { - Err(d.error(ErrorCode::TrailingCharacters)) + Err(self.parser.error(ErrorCode::TrailingCharacters)) } } }