Less horrible logic for missing fields that unconditionally return error

This commit is contained in:
David Tolnay 2018-05-06 22:20:07 -07:00
parent 697234517d
commit 6475e73b05
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 13 additions and 7 deletions

View File

@ -2539,13 +2539,9 @@ fn deserialize_map_in_place(
.map(|&(field, ref name)| { .map(|&(field, ref name)| {
let missing_expr = expr_is_missing(field, cattrs); let missing_expr = expr_is_missing(field, cattrs);
// If missing_expr unconditionally returns an error, don't try // If missing_expr unconditionally returns an error, don't try
// to assign its value to self.place. Maybe this could be handled // to assign its value to self.place.
// more elegantly. if field.attrs.default().is_none() && cattrs.default().is_none()
if missing_expr && field.attrs.deserialize_with().is_some()
.as_ref()
.into_tokens()
.to_string()
.starts_with("return ")
{ {
let missing_expr = Stmts(missing_expr); let missing_expr = Stmts(missing_expr);
quote! { quote! {

View File

@ -738,6 +738,16 @@ pub enum Default {
Path(syn::ExprPath), 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 { impl Field {
/// Extract out the `#[serde(...)]` attributes from a struct field. /// Extract out the `#[serde(...)]` attributes from a struct field.
pub fn from_ast( pub fn from_ast(