Merge pull request #681 from serde-rs/deserialize_struct_bug

fix various serde(rename) bugs
This commit is contained in:
David Tolnay 2017-01-13 01:21:46 -08:00 committed by GitHub
commit c008c6d3a8
2 changed files with 9 additions and 13 deletions

View File

@ -489,7 +489,7 @@ fn deserialize_item_enum(
true,
);
let variant_names = variants.iter().map(|variant| variant.ident.to_string());
let variant_names = variants.iter().map(|variant| variant.attrs.name().deserialize_name());
let variants_stmt = quote! {
const VARIANTS: &'static [&'static str] = &[ #(#variant_names),* ];
@ -697,9 +697,10 @@ fn deserialize_struct_visitor(
fields: &[Field],
item_attrs: &attr::Item,
) -> (Tokens, Tokens, Tokens) {
let field_exprs = fields.iter()
let field_exprs: Vec<_> = fields.iter()
.map(|field| field.attrs.name().deserialize_name())
.collect();
let field_names = field_exprs.clone();
let field_visitor = deserialize_field_visitor(
field_exprs,
@ -715,10 +716,6 @@ fn deserialize_struct_visitor(
item_attrs,
);
let field_names = fields.iter().map(|field| {
field.ident.clone().expect("struct contains unnamed field").to_string()
});
let fields_stmt = quote! {
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
};

View File

@ -85,12 +85,12 @@ impl<I> Deserializer<I>
})
}
fn visit_struct<V>(&mut self, len: Option<usize>, mut visitor: V) -> Result<V::Value, Error>
fn visit_struct<V>(&mut self, fields: &'static [&'static str], mut visitor: V) -> Result<V::Value, Error>
where V: Visitor,
{
visitor.visit_map(DeserializerStructVisitor {
de: self,
len: len,
len: fields.len(),
})
}
@ -433,7 +433,7 @@ impl<I> de::Deserializer for Deserializer<I>
Some(&Token::StructStart(n, _)) => {
self.tokens.next();
if name == n {
self.visit_struct(Some(fields.len()), visitor)
self.visit_struct(fields, visitor)
} else {
Err(Error::InvalidName(n))
}
@ -725,7 +725,7 @@ impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I>
struct DeserializerStructVisitor<'a, I: 'a> where I: Iterator<Item=Token<'static>> {
de: &'a mut Deserializer<I>,
len: Option<usize>,
len: usize,
}
impl<'a, I> MapVisitor for DeserializerStructVisitor<'a, I>
@ -739,7 +739,7 @@ impl<'a, I> MapVisitor for DeserializerStructVisitor<'a, I>
match self.de.tokens.peek() {
Some(&Token::StructSep) => {
self.de.tokens.next();
self.len = self.len.map(|len| if len > 0 { len - 1} else { 0 });
self.len = self.len.saturating_sub(1);
Ok(Some(try!(Deserialize::deserialize(self.de))))
}
Some(&Token::StructEnd) => Ok(None),
@ -767,8 +767,7 @@ impl<'a, I> MapVisitor for DeserializerStructVisitor<'a, I>
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len.unwrap_or(0);
(len, self.len)
(self.len, Some(self.len))
}
}