diff --git a/serde2/src/de2.rs b/serde2/src/de2.rs index a7bb0543..b0a44b24 100644 --- a/serde2/src/de2.rs +++ b/serde2/src/de2.rs @@ -13,17 +13,13 @@ trait VisitorState { fn syntax_error(&mut self) -> E; fn visit< - V: Visitor, - T, - SeqState, - MapState, + V: Visitor, + T: Deserialize, >(&mut self, visitor: &mut V) -> Result; } trait Visitor< T, - SeqState, - MapState, S: VisitorState, E, > { @@ -39,31 +35,43 @@ trait Visitor< Err(state.syntax_error()) } - fn visit_seq(&mut self, state: &mut S, _len: uint) -> Result { + fn visit_seq< + V: SeqVisitor, + >(&mut self, state: &mut S, _visitor: V) -> Result { Err(state.syntax_error()) } - fn visit_seq_elt(&mut self, state: &mut S, _values: &mut SeqState) -> Result<(), E> { - Err(state.syntax_error()) - } - - fn visit_seq_end(&mut self, state: &mut S, _values: SeqState) -> Result { - Err(state.syntax_error()) - } - - fn visit_map(&mut self, state: &mut S, _len: uint) -> Result { - Err(state.syntax_error()) - } - - fn visit_map_elt(&mut self, state: &mut S, _values: &mut MapState) -> Result<(), E> { - Err(state.syntax_error()) - } - - fn visit_map_end(&mut self, state: &mut S, _values: MapState) -> Result { + fn visit_map< + V: MapVisitor, + >(&mut self, state: &mut S, _visitor: V) -> Result { Err(state.syntax_error()) } } +trait SeqVisitor { + fn next< + T: Deserialize, + >(&mut self, state: &mut S) -> Option>; + + #[inline] + fn size_hint(&self) -> (uint, Option) { + (0, None) + } +} + +trait MapVisitor { + fn next< + K: Deserialize, + V: Deserialize, + >(&mut self, state: &mut S) -> Option>; + + #[inline] + fn size_hint(&self) -> (uint, Option) { + (0, None) + } +} + + /////////////////////////////////////////////////////////////////////////////// impl< @@ -76,7 +84,7 @@ impl< impl< S: VisitorState, E, - > ::Visitor for Visitor { + > ::Visitor for Visitor { fn visit_int(&mut self, _state: &mut S, v: int) -> Result { Ok(v) } @@ -96,7 +104,7 @@ impl< impl< S: VisitorState, E, - > ::Visitor for Visitor { + > ::Visitor for Visitor { fn visit_string(&mut self, _state: &mut S, v: String) -> Result { Ok(v) } @@ -120,18 +128,27 @@ impl< T: Deserialize, S: VisitorState, E, - > ::Visitor, Vec, (), S, E> for Visitor { - fn visit_seq(&mut self, _state: &mut S, len: uint) -> Result, E> { - Ok(Vec::with_capacity(len)) - } + > ::Visitor, S, E> for Visitor { + fn visit_seq< + Visitor: SeqVisitor, + >(&mut self, state: &mut S, mut visitor: Visitor) -> Result, E> { + let (len, _) = visitor.size_hint(); + let mut values = Vec::with_capacity(len); - fn visit_seq_elt(&mut self, state: &mut S, values: &mut Vec) -> Result<(), E> { - let value = try!(Deserialize::deserialize(state)); - values.push(value); - Ok(()) - } + loop { + match visitor.next(state) { + Some(Ok(value)) => { + values.push(value); + } + Some(Err(err)) => { + return Err(err); + } + None => { + break; + } + } + } - fn visit_seq_end(&mut self, _state: &mut S, values: Vec) -> Result, E> { Ok(values) } } @@ -142,7 +159,6 @@ impl< /////////////////////////////////////////////////////////////////////////////// - impl< S: VisitorState, E @@ -153,7 +169,7 @@ impl< impl< S: VisitorState, E, - > ::Visitor<(), (), (), S, E> for Visitor { + > ::Visitor<(), S, E> for Visitor { fn visit_null(&mut self, _state: &mut S) -> Result<(), E> { Ok(()) } @@ -172,47 +188,57 @@ impl< E > Deserialize for (T0, T1) { fn deserialize(state: &mut S) -> Result<(T0, T1), E> { - struct Visitor { - state: uint, - } + struct Visitor; impl< T0: Deserialize, T1: Deserialize, S: VisitorState, E - > ::Visitor<(T0, T1), (Option, Option), (), S, E> for Visitor { - fn visit_seq(&mut self, _state: &mut S, _len: uint) -> Result<(Option, Option), E> { - Ok((None, None)) - } + > ::Visitor<(T0, T1), S, E> for Visitor { + fn visit_seq< + Visitor: SeqVisitor, + >(&mut self, visitor_state: &mut S, mut visitor: Visitor) -> Result<(T0, T1), E> { + let mut state = 0u; + let mut t0 = None; + let mut t1 = None; - fn visit_seq_elt(&mut self, state: &mut S, values: &mut (Option, Option)) -> Result<(), E> { - match self.state { - 0 => { - *values.mut0() = Some(try!(Deserialize::deserialize(state))); - self.state += 1; - Ok(()) - } - 1 => { - *values.mut1() = Some(try!(Deserialize::deserialize(state))); - self.state += 1; - Ok(()) - } - _ => { - Err(state.syntax_error()) + loop { + match state { + 0 => { + t0 = match visitor.next(visitor_state) { + Some(Ok(v)) => Some(v), + Some(Err(err)) => { return Err(err); } + None => { return Err(visitor_state.syntax_error()); } + }; + state += 1; + } + 1 => { + t1 = match visitor.next(visitor_state) { + Some(Ok(v)) => Some(v), + Some(Err(err)) => { return Err(err); } + None => { return Err(visitor_state.syntax_error()); } + }; + state += 1; + } + _ => { + match visitor.next(visitor_state) { + Some(Ok(())) => { return Err(visitor_state.syntax_error()); } + Some(Err(err)) => { return Err(err); } + None => { break; } + } + } } } - } - fn visit_seq_end(&mut self, state: &mut S, values: (Option, Option)) -> Result<(T0, T1), E> { - match values { + match (t0, t1) { (Some(t0), Some(t1)) => Ok((t0, t1)), - _ => Err(state.syntax_error()), + _ => Err(visitor_state.syntax_error()), } } } - state.visit(&mut Visitor { state: 0 }) + state.visit(&mut Visitor) } } @@ -232,19 +258,27 @@ impl< V: Deserialize, S: VisitorState, E, - > ::Visitor, (), HashMap, S, E> for Visitor { - fn visit_map(&mut self, _state: &mut S, len: uint) -> Result, E> { - Ok(HashMap::with_capacity(len)) - } + > ::Visitor, S, E> for Visitor { + fn visit_map< + Visitor: MapVisitor, + >(&mut self, state: &mut S, mut visitor: Visitor) -> Result, E> { + let (len, _) = visitor.size_hint(); + let mut values = HashMap::with_capacity(len); - fn visit_map_elt(&mut self, state: &mut S, values: &mut HashMap) -> Result<(), E> { - let key = try!(Deserialize::deserialize(state)); - let value = try!(Deserialize::deserialize(state)); - values.insert(key, value); - Ok(()) - } + loop { + match visitor.next(state) { + Some(Ok((key, value))) => { + values.insert(key, value); + } + Some(Err(err)) => { + return Err(err); + } + None => { + break; + } + } + } - fn visit_map_end(&mut self, _state: &mut S, values: HashMap) -> Result, E> { Ok(values) } } @@ -267,19 +301,26 @@ impl< V: Deserialize, S: VisitorState, E, - > ::Visitor, (), TreeMap, S, E> for Visitor { - fn visit_map(&mut self, _state: &mut S, _len: uint) -> Result, E> { - Ok(TreeMap::new()) - } + > ::Visitor, S, E> for Visitor { + fn visit_map< + Visitor: MapVisitor, + >(&mut self, state: &mut S, mut visitor: Visitor) -> Result, E> { + let mut values = TreeMap::new(); - fn visit_map_elt(&mut self, state: &mut S, values: &mut TreeMap) -> Result<(), E> { - let key = try!(Deserialize::deserialize(state)); - let value = try!(Deserialize::deserialize(state)); - values.insert(key, value); - Ok(()) - } + loop { + match visitor.next(state) { + Some(Ok((key, value))) => { + values.insert(key, value); + } + Some(Err(err)) => { + return Err(err); + } + None => { + break; + } + } + } - fn visit_map_end(&mut self, _state: &mut S, values: TreeMap) -> Result, E> { Ok(values) } } @@ -313,7 +354,7 @@ mod json { impl< S: super::VisitorState, E, - > super::Visitor, TreeMap, S, E> for Visitor { + > super::Visitor for Visitor { fn visit_null(&mut self, _state: &mut S) -> Result { Ok(Null) } @@ -326,32 +367,48 @@ mod json { Ok(String(v)) } - fn visit_seq(&mut self, _state: &mut S, len: uint) -> Result, E> { - Ok(Vec::with_capacity(len)) - } + fn visit_seq< + Visitor: ::SeqVisitor, + >(&mut self, state: &mut S, mut visitor: Visitor) -> Result { + let (len, _) = visitor.size_hint(); + let mut values = Vec::with_capacity(len); - fn visit_seq_elt(&mut self, state: &mut S, values: &mut Vec) -> Result<(), E> { - let value = try!(::Deserialize::deserialize(state)); - values.push(value); - Ok(()) - } + loop { + match visitor.next(state) { + Some(Ok(value)) => { + values.push(value); + } + Some(Err(err)) => { + return Err(err); + } + None => { + break; + } + } + } - fn visit_seq_end(&mut self, _state: &mut S, values: Vec) -> Result { Ok(Vec(values)) } - fn visit_map(&mut self, _state: &mut S, _len: uint) -> Result, E> { - Ok(TreeMap::new()) - } + fn visit_map< + Visitor: ::MapVisitor, + >(&mut self, state: &mut S, mut visitor: Visitor) -> Result { + let mut values = TreeMap::new(); - fn visit_map_elt(&mut self, state: &mut S, values: &mut TreeMap) -> Result<(), E> { - let key = try!(::Deserialize::deserialize(state)); - let value = try!(::Deserialize::deserialize(state)); - values.insert(key, value); - Ok(()) - } + loop { + match visitor.next(state) { + Some(Ok((key, value))) => { + values.insert(key, value); + } + Some(Err(err)) => { + return Err(err); + } + None => { + break; + } + } + } - fn visit_map_end(&mut self, _state: &mut S, values: TreeMap) -> Result { Ok(Map(values)) } } @@ -359,7 +416,6 @@ mod json { state.visit(&mut Visitor) } } - } /////////////////////////////////////////////////////////////////////////////// @@ -414,7 +470,6 @@ impl> MyDeserializerState { } impl< - 'a, Iter: Iterator, > VisitorState< (), @@ -424,10 +479,8 @@ impl< } fn visit< - V: Visitor, ()>, - T, - SeqState, - MapState, + V: Visitor, ()>, + T: Deserialize, ()>, >(&mut self, visitor: &mut V) -> Result { match self.next() { Some(Null) => { @@ -440,44 +493,10 @@ impl< visitor.visit_string(self, v) } Some(SeqStart(len)) => { - let mut state = try!(visitor.visit_seq(self, len)); - - loop { - match self.peek() { - Some(&End) => { - self.next(); - break; - } - Some(_) => { - try!(visitor.visit_seq_elt(self, &mut state)); - } - None => { - return Err(()); - } - } - } - - visitor.visit_seq_end(self, state) + visitor.visit_seq(self, MySeqVisitor { len: len }) } Some(MapStart(len)) => { - let mut state = try!(visitor.visit_map(self, len)); - - loop { - match self.peek() { - Some(&End) => { - self.next(); - break; - } - Some(_) => { - try!(visitor.visit_map_elt(self, &mut state)); - } - None => { - return Err(()); - } - } - } - - visitor.visit_map_end(self, state) + visitor.visit_map(self, MyMapVisitor { len: len }) } Some(End) => { Err(()) @@ -486,7 +505,78 @@ impl< Err(()) } } + } +} +struct MySeqVisitor { + len: uint, +} + +impl< + Iter: Iterator, +> SeqVisitor, ()> for MySeqVisitor { + fn next< + T: Deserialize, ()>, + >(&mut self, state: &mut MyDeserializerState) -> Option> { + match state.peek() { + Some(&End) => { + state.next(); + None + } + Some(_) => { + self.len -= 1; + Some(Deserialize::deserialize(state)) + } + None => { + Some(Err(state.syntax_error())) + } + } + } + + fn size_hint(&self) -> (uint, Option) { + (self.len, Some(self.len)) + } +} + +struct MyMapVisitor { + len: uint, +} + +impl< + Iter: Iterator, +> MapVisitor, ()> for MyMapVisitor { + fn next< + K: Deserialize, ()>, + V: Deserialize, ()>, + >(&mut self, state: &mut MyDeserializerState) -> Option> { + match state.peek() { + Some(&End) => { + state.next(); + None + } + Some(_) => { + self.len -= 1; + + let key = match Deserialize::deserialize(state) { + Ok(key) => key, + Err(err) => { return Some(Err(err)); } + }; + + let value = match Deserialize::deserialize(state) { + Ok(value) => value, + Err(err) => { return Some(Err(err)); } + }; + + Some(Ok((key, value))) + } + None => { + Some(Err(state.syntax_error())) + } + } + } + + fn size_hint(&self) -> (uint, Option) { + (self.len, Some(self.len)) } }