Don't leave empty tables lying around

This commit is contained in:
Alex Crichton 2014-06-28 14:42:30 -07:00
parent 9c1806283c
commit 6b4f33444a

View file

@ -536,7 +536,15 @@ impl serialize::Decoder<DecodeError> for Decoder {
-> Result<T, DecodeError>
{
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);
}
}