From 6475e73b054b12c03a44da05aacc8e516e73b30c Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 6 May 2018 22:20:07 -0700 Subject: [PATCH] Less horrible logic for missing fields that unconditionally return error --- serde_derive/src/de.rs | 10 +++------- serde_derive/src/internals/attr.rs | 10 ++++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index e3197fcf..4cb89502 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -2539,13 +2539,9 @@ fn deserialize_map_in_place( .map(|&(field, ref name)| { let missing_expr = expr_is_missing(field, cattrs); // If missing_expr unconditionally returns an error, don't try - // to assign its value to self.place. Maybe this could be handled - // more elegantly. - if missing_expr - .as_ref() - .into_tokens() - .to_string() - .starts_with("return ") + // to assign its value to self.place. + if field.attrs.default().is_none() && cattrs.default().is_none() + && field.attrs.deserialize_with().is_some() { let missing_expr = Stmts(missing_expr); quote! { diff --git a/serde_derive/src/internals/attr.rs b/serde_derive/src/internals/attr.rs index bc60a6e3..29476f17 100644 --- a/serde_derive/src/internals/attr.rs +++ b/serde_derive/src/internals/attr.rs @@ -738,6 +738,16 @@ pub enum Default { Path(syn::ExprPath), } +impl Default { + #[cfg(feature = "deserialize_in_place")] + pub fn is_none(&self) -> bool { + match *self { + Default::None => true, + Default::Default | Default::Path(_) => false, + } + } +} + impl Field { /// Extract out the `#[serde(...)]` attributes from a struct field. pub fn from_ast(