diff --git a/serde_codegen/src/ser.rs b/serde_codegen/src/ser.rs index d3c91396..8fdc3576 100644 --- a/serde_codegen/src/ser.rs +++ b/serde_codegen/src/ser.rs @@ -4,6 +4,7 @@ use syntax::ast::{self, Ident, MetaItem}; use syntax::codemap::Span; use syntax::ext::base::{Annotatable, ExtCtxt}; use syntax::ptr::P; +use syntax::tokenstream::TokenTree; use bound; use span; @@ -226,9 +227,10 @@ fn serialize_tuple_struct( let type_name = name_expr(builder, item_attrs.name()); let len = serialize_stmts.len(); + let let_mut = mut_if(cx, len > 0); quote_block!(cx, { - let mut state = try!(_serializer.serialize_tuple_struct($type_name, $len)); + let $let_mut state = try!(_serializer.serialize_tuple_struct($type_name, $len)); $serialize_stmts _serializer.serialize_tuple_struct_end(state) }).unwrap() @@ -253,8 +255,14 @@ fn serialize_struct( ); let type_name = name_expr(builder, item_attrs.name()); - let len = fields.iter() + + let mut serialized_fields = fields.iter() .filter(|&field| !field.attrs.skip_serializing()) + .peekable(); + + let let_mut = mut_if(cx, serialized_fields.peek().is_some()); + + let len = serialized_fields .map(|field| { let ident = field.ident.expect("struct has unnamed fields"); let field_expr = quote_expr!(cx, &self.$ident); @@ -267,7 +275,7 @@ fn serialize_struct( .fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr)); quote_block!(cx, { - let mut state = try!(_serializer.serialize_struct($type_name, $len)); + let $let_mut state = try!(_serializer.serialize_struct($type_name, $len)); $serialize_fields _serializer.serialize_struct_end(state) }).unwrap() @@ -458,9 +466,10 @@ fn serialize_tuple_variant( ); let len = serialize_stmts.len(); + let let_mut = mut_if(cx, len > 0); quote_block!(cx, { - let mut state = try!(_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, $len)); + let $let_mut state = try!(_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, $len)); $serialize_stmts _serializer.serialize_tuple_variant_end(state) }).unwrap() @@ -488,8 +497,14 @@ fn serialize_struct_variant( ); let item_name = name_expr(builder, item_attrs.name()); - let len = fields.iter() + + let mut serialized_fields = fields.iter() .filter(|&field| !field.attrs.skip_serializing()) + .peekable(); + + let let_mut = mut_if(cx, serialized_fields.peek().is_some()); + + let len = serialized_fields .map(|field| { let ident = field.ident.expect("struct has unnamed fields"); let field_expr = quote_expr!(cx, $ident); @@ -502,7 +517,7 @@ fn serialize_struct_variant( .fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr)); quote_block!(cx, { - let mut state = try!(_serializer.serialize_struct_variant( + let $let_mut state = try!(_serializer.serialize_struct_variant( $item_name, $variant_index, $variant_name, @@ -641,3 +656,17 @@ fn name_expr( ) -> P { builder.expr().str(name.serialize_name()) } + +// Serialization of an empty struct results in code like: +// +// let mut state = try!(serializer.serialize_struct("S", 0)); +// serializer.serialize_struct_end(state) +// +// where we want to omit the `mut` to avoid a warning. +fn mut_if(cx: &ExtCtxt, is_mut: bool) -> Vec { + if is_mut { + quote_tokens!(cx, mut) + } else { + Vec::new() + } +} diff --git a/testing/tests/test_gen.rs b/testing/tests/test_gen.rs index 7d648f57..3e2fe382 100644 --- a/testing/tests/test_gen.rs +++ b/testing/tests/test_gen.rs @@ -187,6 +187,16 @@ fn test_gen() { #[serde(bound(deserialize = "T::Owned: Deserialize"))] struct CowT<'a, T: ?Sized + 'a + ToOwned>(Cow<'a, T>); assert::>(); + + #[derive(Serialize, Deserialize)] + struct EmptyStruct {} + assert::(); + + #[derive(Serialize, Deserialize)] + enum EmptyEnumVariant { + EmptyStruct {}, + } + assert::(); } //////////////////////////////////////////////////////////////////////////