Skip to content

Commit

Permalink
Emit unwrap_or_default when appropriate for default fields
Browse files Browse the repository at this point in the history
  • Loading branch information
novafacing committed May 30, 2024
1 parent 05113d1 commit 91bc421
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions core/src/codegen/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,15 @@ impl<'a> ToTokens for Initializer<'a> {
quote!(#ident: #ident)
}
} else if let Some(ref expr) = field.default_expression {
quote_spanned!(expr.span()=> #ident: if let Some(__val) = #ident.1 {
__val
if matches!(expr, DefaultExpression::Trait { .. }) {
quote_spanned!(expr.span()=> #ident: #ident.1.unwrap_or_default())
} else {
#expr
})
quote_spanned!(expr.span()=> #ident: if let Some(__val) = #ident.1 {
__val
} else {
#expr
})
}
} else {
quote!(#ident: #ident.1.expect("Uninitialized fields without defaults were already checked"))
});
Expand Down

0 comments on commit 91bc421

Please sign in to comment.