From 0f20aad8238dd02fe0ace0ba587b1a79c5f27704 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 28 Jun 2014 14:49:04 -0700 Subject: Prune more unused fields --- src/serialization.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/serialization.rs b/src/serialization.rs index 395e85f..9b1ba01 100644 --- a/src/serialization.rs +++ b/src/serialization.rs @@ -616,7 +616,13 @@ impl serialize::Decoder for Decoder { Some(Array(ref arr)) => arr.len(), ref found => return Err(self.mismatch("array", found)), }; - f(self, len) + let ret = try!(f(self, len)); + match self.toml { + Some(Array(ref arr)) if arr.len() == 0 => {} + _ => return Ok(ret) + } + self.toml.take(); + Ok(ret) } fn read_seq_elt(&mut self, idx: uint, f: |&mut Decoder| -> Result) @@ -636,7 +642,9 @@ impl serialize::Decoder for Decoder { Some(Table(ref table)) => table.len(), ref found => return Err(self.mismatch("table", found)), }; - f(self, len) + let ret = try!(f(self, len)); + self.toml.take(); + Ok(ret) } fn read_map_elt_key(&mut self, idx: uint, f: |&mut Decoder| -> Result) @@ -1043,4 +1051,34 @@ mod tests { assert_eq!(d.toml, None); } + + #[test] + fn unused_fields4() { + #[deriving(Encodable, Decodable, PartialEq, Show)] + struct Foo { a: HashMap } + + let v = Foo { a: map! { a: "foo".to_string() } }; + let mut d = Decoder::new(Table(map! { + a: Table(map! { + a: String("foo".to_string()) + }) + })); + assert_eq!(v, Decodable::decode(&mut d).unwrap()); + + assert_eq!(d.toml, None); + } + + #[test] + fn unused_fields5() { + #[deriving(Encodable, Decodable, PartialEq, Show)] + struct Foo { a: Vec } + + let v = Foo { a: vec![] }; + let mut d = Decoder::new(Table(map! { + a: Array(vec![]) + })); + assert_eq!(v, Decodable::decode(&mut d).unwrap()); + + assert_eq!(d.toml, None); + } } -- cgit v1.2.3