diff --git a/README.md b/README.md index 80738372..64fa1da8 100644 --- a/README.md +++ b/README.md @@ -689,34 +689,34 @@ how types are serialized. Here are the supported annotations: Container Annotations: -| Annotation | Function | -| ---------- | -------- | -| `#[serde(rename="name")` | Serialize and deserialize this container with the given name | -| `#[serde(rename(serialize="name1"))` | Serialize this container with the given name | -| `#[serde(rename(deserialize="name1"))` | Deserialize this container with the given name | -| `#[serde(deny_unknown_fields)` | Always error during serialization when encountering unknown fields. When absent, unknown fields are ignored for self-describing formats like JSON. | +| Annotation | Function | +| ---------- | -------- | +| `#[serde(rename="name")]` | Serialize and deserialize this container with the given name | +| `#[serde(rename(serialize="name1"))]` | Serialize this container with the given name | +| `#[serde(rename(deserialize="name1"))]` | Deserialize this container with the given name | +| `#[serde(deny_unknown_fields)]` | Always error during serialization when encountering unknown fields. When absent, unknown fields are ignored for self-describing formats like JSON. | Variant Annotations: -| Annotation | Function | -| ---------- | -------- | -| `#[serde(rename="name")` | Serialize and deserialize this variant with the given name | -| `#[serde(rename(serialize="name1"))` | Serialize this variant with the given name | -| `#[serde(rename(deserialize="name1"))` | Deserialize this variant with the given name | +| Annotation | Function | +| ---------- | -------- | +| `#[serde(rename="name")]` | Serialize and deserialize this variant with the given name | +| `#[serde(rename(serialize="name1"))]` | Serialize this variant with the given name | +| `#[serde(rename(deserialize="name1"))]` | Deserialize this variant with the given name | Field Annotations: -| Annotation | Function | -| ---------- | -------- | -| `#[serde(rename="name")` | Serialize and deserialize this field with the given name | -| `#[serde(rename(serialize="name1"))` | Serialize this field with the given name | -| `#[serde(rename(deserialize="name1"))` | Deserialize this field with the given name | -| `#[serde(default)` | If the value is not specified, use the `Default::default()` | -| `#[serde(default="$path")` | Call the path to a function `fn() -> T` to build the value | -| `#[serde(skip_serializing)` | Do not serialize this value | -| `#[serde(skip_serializing_if="$path")` | Do not serialize this value if this function `fn(&T) -> bool` returns `false` | -| `#[serde(serialize_with="$path")` | Call a function `fn(&T, &mut S) -> Result<(), S::Error> where S: Serializer` to serialize this value | -| `#[serde(deserialize_with="$path")` | Call a function `fn(&mut D) -> Result where D: Deserializer` to deserialize this value | +| Annotation | Function | +| ---------- | -------- | +| `#[serde(rename="name")]` | Serialize and deserialize this field with the given name | +| `#[serde(rename(serialize="name1"))]` | Serialize this field with the given name | +| `#[serde(rename(deserialize="name1"))]` | Deserialize this field with the given name | +| `#[serde(default)]` | If the value is not specified, use the `Default::default()` | +| `#[serde(default="$path")]` | Call the path to a function `fn() -> T` to build the value | +| `#[serde(skip_serializing)]` | Do not serialize this value | +| `#[serde(skip_serializing_if="$path")]` | Do not serialize this value if this function `fn(&T) -> bool` returns `false` | +| `#[serde(serialize_with="$path")]` | Call a function `fn(&T, &mut S) -> Result<(), S::Error> where S: Serializer` to serialize this value | +| `#[serde(deserialize_with="$path")]` | Call a function `fn(&mut D) -> Result where D: Deserializer` to deserialize this value | Upgrading from Serde 0.6 ======================== diff --git a/serde/src/bytes.rs b/serde/src/bytes.rs index 7d80ec2c..21f6af0e 100644 --- a/serde/src/bytes.rs +++ b/serde/src/bytes.rs @@ -61,7 +61,7 @@ impl<'a> ser::Serialize for Bytes<'a> { /////////////////////////////////////////////////////////////////////////////// /// `ByteBuf` wraps a `Vec` and serializes as a byte array. -#[derive(Clone, Eq, Hash, PartialEq, PartialOrd, Ord)] +#[derive(Clone, Default, Eq, Hash, PartialEq, PartialOrd, Ord)] pub struct ByteBuf { bytes: Vec, } diff --git a/serde_codegen/Cargo.toml b/serde_codegen/Cargo.toml index 1c2226e1..9a5b6c0a 100644 --- a/serde_codegen/Cargo.toml +++ b/serde_codegen/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "serde_codegen" -version = "0.7.1" +version = "0.7.2" authors = ["Erick Tryzelaar "] license = "MIT/Apache-2.0" description = "Macros to auto-generate implementations for the serde framework" @@ -16,13 +16,13 @@ nightly-testing = ["clippy"] with-syntex = ["quasi/with-syntex", "quasi_codegen", "quasi_codegen/with-syntex", "syntex", "syntex_syntax"] [build-dependencies] -quasi_codegen = { version = "^0.8.0", optional = true } -syntex = { version = "^0.30.0", optional = true } +quasi_codegen = { version = "^0.9.0", optional = true } +syntex = { version = "^0.31.0", optional = true } [dependencies] -aster = { version = "^0.14.0", default-features = false } +aster = { version = "^0.15.0", default-features = false } clippy = { version = "^0.*", optional = true } -quasi = { version = "^0.8.0", default-features = false } -quasi_macros = { version = "^0.8.0", optional = true } -syntex = { version = "^0.30.0", optional = true } -syntex_syntax = { version = "^0.30.0", optional = true } +quasi = { version = "^0.9.0", default-features = false } +quasi_macros = { version = "^0.9.0", optional = true } +syntex = { version = "^0.31.0", optional = true } +syntex_syntax = { version = "^0.31.0", optional = true } diff --git a/serde_codegen/src/attr.rs b/serde_codegen/src/attr.rs index 8f41022a..2c019382 100644 --- a/serde_codegen/src/attr.rs +++ b/serde_codegen/src/attr.rs @@ -191,7 +191,7 @@ impl FieldAttrs { is_enum: bool) -> Result { let builder = AstBuilder::new(); - let field_ident = match field.node.ident() { + let field_ident = match field.ident { Some(ident) => ident, None => { cx.span_bug(field.span, "struct field has no name?") } }; @@ -205,7 +205,7 @@ impl FieldAttrs { deserialize_with: None, }; - for meta_items in field.node.attrs.iter().filter_map(get_serde_meta_items) { + for meta_items in field.attrs.iter().filter_map(get_serde_meta_items) { for meta_item in meta_items { match meta_item.node { // Parse `#[serde(rename="foo")]` @@ -273,7 +273,7 @@ impl FieldAttrs { ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"deserialize_with" => { let expr = wrap_deserialize_with( cx, - &field.node.ty, + &field.ty, generics, try!(parse_lit_into_path(cx, name, lit)), ); @@ -614,7 +614,7 @@ fn wrap_deserialize_with(cx: &ExtCtxt, } impl $generics ::serde::de::Deserialize for $ty_path $where_clause { - fn deserialize(deserializer: &mut D) -> Result + fn deserialize(deserializer: &mut D) -> ::std::result::Result where D: ::serde::de::Deserializer { let value = try!($path(deserializer)); diff --git a/serde_codegen/src/de.rs b/serde_codegen/src/de.rs index fc935809..c21c9677 100644 --- a/serde_codegen/src/de.rs +++ b/serde_codegen/src/de.rs @@ -148,7 +148,7 @@ fn deserialize_item_struct( ) } ast::VariantData::Tuple(ref fields, _) => { - if fields.iter().any(|field| !field.node.kind.is_unnamed()) { + if fields.iter().any(|field| field.ident.is_some()) { cx.span_bug(span, "tuple struct has named fields") } @@ -163,7 +163,7 @@ fn deserialize_item_struct( ) } ast::VariantData::Struct(ref fields, _) => { - if fields.iter().any(|field| field.node.kind.is_unnamed()) { + if fields.iter().any(|field| field.ident.is_none()) { cx.span_bug(span, "struct has unnamed fields") } @@ -452,9 +452,9 @@ fn deserialize_struct_as_seq( .enumerate() .map(|(i, field)| { ( - match field.node.kind { - ast::NamedField(name, _) => name.clone(), - ast::UnnamedField(_) => { + match field.ident { + Some(name) => name.clone(), + None => { cx.span_bug(field.span, "struct contains unnamed fields") } }, @@ -583,10 +583,10 @@ fn deserialize_item_enum( const VARIANTS: &'static [&'static str] = $variants_expr; ).unwrap(); - let ignored_arm = if !container_attrs.deny_unknown_fields() { - Some(quote_arm!(cx, __Field::__ignore => { Err(::serde::de::Error::end_of_stream()) })) - } else { + let ignored_arm = if container_attrs.deny_unknown_fields() { None + } else { + Some(quote_arm!(cx, __Field::__ignore => { Err(::serde::de::Error::end_of_stream()) })) }; // Match arms to extract a variant from a string @@ -816,11 +816,11 @@ fn deserialize_field_visitor( .map(|i| builder.id(format!("__field{}", i))) .collect(); - let ignore_variant = if !container_attrs.deny_unknown_fields() { + let ignore_variant = if container_attrs.deny_unknown_fields() { + None + } else { let skip_ident = builder.id("__ignore"); Some(builder.variant(skip_ident).unit()) - } else { - None }; let field_enum = builder.item() @@ -1008,9 +1008,9 @@ fn deserialize_struct_visitor( .with_exprs( fields.iter() .map(|field| { - match field.node.kind { - ast::NamedField(name, _) => builder.expr().str(name), - ast::UnnamedField(_) => { + match field.ident { + Some(name) => builder.expr().str(name), + None => { cx.span_bug(field.span, "struct contains unnamed fields") } } @@ -1053,12 +1053,12 @@ fn deserialize_map( // Visit ignored values to consume them - let ignored_arm = if !container_attrs.deny_unknown_fields() { + let ignored_arm = if container_attrs.deny_unknown_fields() { + None + } else { Some(quote_arm!(cx, _ => { try!(visitor.visit_value::<::serde::de::impls::IgnoredAny>()); } )) - } else { - None }; // Match arms to extract a value for a field. @@ -1099,9 +1099,9 @@ fn deserialize_map( .zip(field_names.iter()) .map(|(field, field_name)| { ( - match field.node.kind { - ast::NamedField(name, _) => name.clone(), - ast::UnnamedField(_) => { + match field.ident { + Some(name) => name.clone(), + None => { cx.span_bug(field.span, "struct contains unnamed fields") } }, diff --git a/serde_codegen/src/lib.rs b/serde_codegen/src/lib.rs index 8fb928e8..52ca7912 100644 --- a/serde_codegen/src/lib.rs +++ b/serde_codegen/src/lib.rs @@ -1,5 +1,6 @@ #![cfg_attr(feature = "nightly-testing", plugin(clippy))] #![cfg_attr(feature = "nightly-testing", feature(plugin))] +#![cfg_attr(feature = "nightly-testing", allow(too_many_arguments))] #![cfg_attr(feature = "nightly-testing", allow(used_underscore_binding))] #![cfg_attr(not(feature = "with-syntex"), feature(rustc_private, plugin))] #![cfg_attr(not(feature = "with-syntex"), plugin(quasi_macros))] diff --git a/serde_codegen/src/ser.rs b/serde_codegen/src/ser.rs index cb97640d..08288f35 100644 --- a/serde_codegen/src/ser.rs +++ b/serde_codegen/src/ser.rs @@ -80,7 +80,7 @@ fn serialize_item( Ok(quote_item!(cx, impl $impl_generics ::serde::ser::Serialize for $ty $where_clause { - fn serialize<__S>(&self, serializer: &mut __S) -> ::std::result::Result<(), __S::Error> + fn serialize<__S>(&self, _serializer: &mut __S) -> ::std::result::Result<(), __S::Error> where __S: ::serde::ser::Serializer, { $body @@ -151,7 +151,7 @@ fn serialize_item_struct( ) } ast::VariantData::Tuple(ref fields, _) => { - if fields.iter().any(|field| !field.node.kind.is_unnamed()) { + if fields.iter().any(|field| field.ident.is_some()) { cx.span_bug(span, "tuple struct has named fields") } @@ -165,7 +165,7 @@ fn serialize_item_struct( ) } ast::VariantData::Struct(ref fields, _) => { - if fields.iter().any(|field| field.node.kind.is_unnamed()) { + if fields.iter().any(|field| field.ident.is_none()) { cx.span_bug(span, "struct has unnamed fields") } @@ -188,7 +188,7 @@ fn serialize_unit_struct( let type_name = container_attrs.name().serialize_name_expr(); Ok(quote_expr!(cx, - serializer.serialize_unit_struct($type_name) + _serializer.serialize_unit_struct($type_name) )) } @@ -199,7 +199,7 @@ fn serialize_newtype_struct( let type_name = container_attrs.name().serialize_name_expr(); Ok(quote_expr!(cx, - serializer.serialize_newtype_struct($type_name, &self.0) + _serializer.serialize_newtype_struct($type_name, &self.0) )) } @@ -229,7 +229,7 @@ fn serialize_tuple_struct( Ok(quote_expr!(cx, { $visitor_struct $visitor_impl - serializer.serialize_tuple_struct($type_name, Visitor { + _serializer.serialize_tuple_struct($type_name, Visitor { value: self, state: 0, _structure_ty: ::std::marker::PhantomData::<&$ty>, @@ -264,7 +264,7 @@ fn serialize_struct( Ok(quote_expr!(cx, { $visitor_struct $visitor_impl - serializer.serialize_struct($type_name, Visitor { + _serializer.serialize_struct($type_name, Visitor { value: self, state: 0, _structure_ty: ::std::marker::PhantomData::<&$ty>, @@ -331,7 +331,7 @@ fn serialize_variant( Ok(quote_arm!(cx, $pat => { ::serde::ser::Serializer::serialize_unit_variant( - serializer, + _serializer, $type_name, $variant_index, $variant_name, @@ -350,7 +350,7 @@ fn serialize_variant( Ok(quote_arm!(cx, $pat => { ::serde::ser::Serializer::serialize_newtype_variant( - serializer, + _serializer, $type_name, $variant_index, $variant_name, @@ -399,9 +399,9 @@ fn serialize_variant( field_names.iter() .zip(fields.iter()) .map(|(id, field)| { - let name = match field.node.kind { - ast::NamedField(name, _) => name, - ast::UnnamedField(_) => { + let name = match field.ident { + Some(name) => name, + None => { cx.span_bug(field.span, "struct variant has unnamed fields") } }; @@ -447,7 +447,7 @@ fn serialize_tuple_variant( builder.ty() .ref_() .lifetime("'__a") - .build_ty(field.node.ty.clone()) + .build_ty(field.ty.clone()) }) ) .build(); @@ -473,7 +473,7 @@ fn serialize_tuple_variant( quote_expr!(cx, { $visitor_struct $visitor_impl - serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, Visitor { + _serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, Visitor { value: $value_expr, state: 0, _structure_ty: ::std::marker::PhantomData::<&$structure_ty>, @@ -502,12 +502,12 @@ fn serialize_struct_variant( .with_generics(variant_generics.clone()) .with_fields( fields.iter().map(|field| { - builder.struct_field(field.node.ident().expect("struct has unnamed fields")) - .with_attrs(field.node.attrs.iter().cloned()) + builder.struct_field(field.ident.expect("struct has unnamed fields")) + .with_attrs(field.attrs.iter().cloned()) .ty() .ref_() .lifetime("'__serde_variant") - .build_ty(field.node.ty.clone()) + .build_ty(field.ty.clone()) }) ) .field("__serde_container_ty") @@ -520,7 +520,7 @@ fn serialize_struct_variant( .zip(field_names.iter()) .map(|(field, field_name)| { ( - field.node.ident().expect("struct has unnamed fields"), + field.ident.expect("struct has unnamed fields"), builder.expr().id(field_name), ) }) @@ -557,7 +557,7 @@ fn serialize_struct_variant( $variant_struct $visitor_struct $visitor_impl - serializer.serialize_struct_variant( + _serializer.serialize_struct_variant( $container_name, $variant_index, $variant_name, @@ -582,7 +582,7 @@ fn serialize_tuple_struct_visitor( let arms: Vec = (0 .. fields) .map(|i| { let expr = builder.expr().method_call(serializer_method) - .id("serializer") + .id("_serializer") .arg().ref_().tup_field(i).field("value").self_() .build(); @@ -620,7 +620,7 @@ fn serialize_tuple_struct_visitor( for Visitor $visitor_generics $where_clause { #[inline] - fn visit(&mut self, serializer: &mut S) -> ::std::result::Result, S::Error> + fn visit(&mut self, _serializer: &mut S) -> ::std::result::Result, S::Error> where S: ::serde::ser::Serializer { match self.state { @@ -656,7 +656,7 @@ fn serialize_struct_visitor( .filter(|&(_, ref field_attr)| !field_attr.skip_serializing_field()) .enumerate() .map(|(i, (ref field, ref field_attr))| { - let name = field.node.ident().expect("struct has unnamed field"); + let name = field.ident.expect("struct has unnamed field"); let key_expr = field_attr.name().serialize_name_expr(); @@ -672,7 +672,7 @@ fn serialize_struct_visitor( }; let expr = quote_expr!(cx, - serializer.$serializer_method($key_expr, $field_expr) + _serializer.$serializer_method($key_expr, $field_expr) ); quote_arm!(cx, @@ -725,7 +725,7 @@ fn serialize_struct_visitor( for Visitor $visitor_generics $where_clause { #[inline] - fn visit(&mut self, serializer: &mut S) -> ::std::result::Result, S::Error> + fn visit(&mut self, _serializer: &mut S) -> ::std::result::Result, S::Error> where S: ::serde::ser::Serializer, { loop { diff --git a/serde_macros/Cargo.toml b/serde_macros/Cargo.toml index e0e7d106..72267fcc 100644 --- a/serde_macros/Cargo.toml +++ b/serde_macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "serde_macros" -version = "0.7.0" +version = "0.7.2" authors = ["Erick Tryzelaar "] license = "MIT/Apache-2.0" description = "Macros to auto-generate implementations for the serde framework" @@ -17,7 +17,7 @@ nightly-testing = ["clippy", "serde/nightly-testing", "serde_codegen/nightly-tes [dependencies] clippy = { version = "^0.*", optional = true } -serde_codegen = { version = "^0.7.0", path = "../serde_codegen", default-features = false, features = ["nightly"] } +serde_codegen = { version = "^0.7.2", path = "../serde_codegen", default-features = false, features = ["nightly"] } [dev-dependencies] compiletest_rs = "^0.1.1" diff --git a/serde_tests/Cargo.toml b/serde_tests/Cargo.toml index ee63b936..54418208 100644 --- a/serde_tests/Cargo.toml +++ b/serde_tests/Cargo.toml @@ -14,14 +14,14 @@ build = "build.rs" nightly-testing = ["clippy", "serde/nightly-testing", "serde_codegen/nightly-testing"] [build-dependencies] -syntex = { version = "^0.30.0" } -syntex_syntax = { version = "^0.30.0" } +syntex = { version = "^0.31.0" } +syntex_syntax = { version = "^0.31.0" } serde_codegen = { version = "*", path = "../serde_codegen", features = ["with-syntex"] } [dev-dependencies] rustc-serialize = "^0.3.16" serde = { version = "*", path = "../serde" } -syntex = "^0.30.0" +syntex = "^0.31.0" [dependencies] clippy = { version = "^0.*", optional = true } diff --git a/serde_tests/tests/test_macros.rs b/serde_tests/tests/test_macros.rs index 5feb7252..ad472395 100644 --- a/serde_tests/tests/test_macros.rs +++ b/serde_tests/tests/test_macros.rs @@ -6,6 +6,13 @@ trait Trait { } */ +// That tests that the derived Serialize implementation doesn't trigger +// any warning about `serializer` not being used, in case of empty enums. +#[derive(Serialize)] +#[allow(dead_code)] +#[deny(unused_variables)] +enum Void {} + #[derive(Debug, PartialEq, Serialize, Deserialize)] struct NamedUnit;