Auto merge of #538 - serde-rs:notfalse, r=oli-obk

Remove `if !false { ... }` from generated serialization code

I don't think this negatively affects maintainability of the code in serde_codegen and I think there is some value in keeping our generated code relatively clear so that people can use it as a template when implementing Serialize manually with minor modifications.
This commit is contained in:
Homu 2016-09-05 19:52:43 +09:00
commit f3052c392e

View File

@ -532,19 +532,21 @@ fn serialize_tuple_struct_visitor(
}; };
let skip = field.attrs.skip_serializing_if() let skip = field.attrs.skip_serializing_if()
.map(|path| quote_expr!(cx, $path($field_expr))) .map(|path| quote_expr!(cx, $path($field_expr)));
.unwrap_or(quote_expr!(cx, false));
if let Some(path) = field.attrs.serialize_with() { if let Some(path) = field.attrs.serialize_with() {
field_expr = wrap_serialize_with(cx, builder, field_expr = wrap_serialize_with(cx, builder,
&structure_ty, generics, &field.ty, path, field_expr); &structure_ty, generics, &field.ty, path, field_expr);
} }
quote_stmt!(cx, let ser = quote_expr!(cx,
if !$skip { try!(_serializer.$func(&mut state, $field_expr));
try!(_serializer.$func(&mut state, $field_expr)); );
}
).unwrap() match skip {
None => quote_stmt!(cx, $ser).unwrap(),
Some(skip) => quote_stmt!(cx, if !$skip { $ser }).unwrap(),
}
}) })
.collect() .collect()
} }
@ -571,19 +573,21 @@ fn serialize_struct_visitor(
let key_expr = name_expr(builder, field.attrs.name()); let key_expr = name_expr(builder, field.attrs.name());
let skip = field.attrs.skip_serializing_if() let skip = field.attrs.skip_serializing_if()
.map(|path| quote_expr!(cx, $path($field_expr))) .map(|path| quote_expr!(cx, $path($field_expr)));
.unwrap_or(quote_expr!(cx, false));
if let Some(path) = field.attrs.serialize_with() { if let Some(path) = field.attrs.serialize_with() {
field_expr = wrap_serialize_with(cx, builder, field_expr = wrap_serialize_with(cx, builder,
&structure_ty, generics, &field.ty, path, field_expr) &structure_ty, generics, &field.ty, path, field_expr)
} }
quote_stmt!(cx, let ser = quote_expr!(cx,
if !$skip { try!(_serializer.$func(&mut state, $key_expr, $field_expr));
try!(_serializer.$func(&mut state, $key_expr, $field_expr)); );
}
).unwrap() match skip {
None => quote_stmt!(cx, $ser).unwrap(),
Some(skip) => quote_stmt!(cx, if !$skip { $ser }).unwrap(),
}
}) })
.collect() .collect()
} }