From 6b4f33444ad55b8264cf5ddb70b0bcdff57d42df Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 28 Jun 2014 14:42:30 -0700 Subject: Don't leave empty tables lying around --- src/serialization.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src/serialization.rs') diff --git a/src/serialization.rs b/src/serialization.rs index 26fc7ba..395e85f 100644 --- a/src/serialization.rs +++ b/src/serialization.rs @@ -536,7 +536,15 @@ impl serialize::Decoder for Decoder { -> Result { match self.toml { - Some(Table(..)) => f(self), + Some(Table(..)) => { + let ret = try!(f(self)); + match self.toml { + Some(Table(ref t)) if t.len() == 0 => {} + _ => return Ok(ret) + } + self.toml.take(); + Ok(ret) + } ref found => Err(self.mismatch("table", found)), } } @@ -1017,4 +1025,22 @@ mod tests { }) }))); } + + #[test] + fn unused_fields3() { + #[deriving(Encodable, Decodable, PartialEq, Show)] + struct Foo { a: Bar } + #[deriving(Encodable, Decodable, PartialEq, Show)] + struct Bar { a: int } + + let v = Foo { a: Bar { a: 2 } }; + let mut d = Decoder::new(Table(map! { + a: Table(map! { + a: Integer(2) + }) + })); + assert_eq!(v, Decodable::decode(&mut d).unwrap()); + + assert_eq!(d.toml, None); + } } -- cgit v1.2.3