diff --git a/.gitignore b/.gitignore index 7e8fe897..165eb22d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -Cargo.lock -target +target/ +**/*.rs.bk *.sw[po] +Cargo.lock diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index b1aedff4..b73511bb 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -1039,7 +1039,8 @@ impl<'de> Visitor<'de> for OsStringVisitor { match try!(data.variant()) { (OsStringKind::Windows, v) => { - v.newtype_variant::>().map(|vec| OsString::from_wide(&vec)) + v.newtype_variant::>() + .map(|vec| OsString::from_wide(&vec)) } (OsStringKind::Unix, _) => Err(Error::custom("cannot deserialize Unix OS string on Windows",),), } @@ -1151,7 +1152,7 @@ where impl<'de, T> Deserialize<'de> for RefCell where - T: Deserialize<'de> + T: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result, D::Error> where diff --git a/serde/src/de/value.rs b/serde/src/de/value.rs index f3bf0558..6e1d89f2 100644 --- a/serde/src/de/value.rs +++ b/serde/src/de/value.rs @@ -922,7 +922,8 @@ where Second: Debug, { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.debug_struct("MapDeserializer") + formatter + .debug_struct("MapDeserializer") .field("iter", &self.iter) .field("value", &self.value) .field("count", &self.count) diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 6666ada2..270e1df8 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -191,7 +191,8 @@ pub mod size_hint { use lib::*; pub fn from_bounds(iter: &I) -> Option - where I: Iterator + where + I: Iterator, { helper(iter.size_hint()) } @@ -202,9 +203,7 @@ pub mod size_hint { fn helper(bounds: (usize, Option)) -> Option { match bounds { - (lower, Some(upper)) if lower == upper => { - Some(upper) - } + (lower, Some(upper)) if lower == upper => Some(upper), _ => None, } } @@ -1026,10 +1025,7 @@ mod content { type Error = E; type Variant = VariantDeserializer; - fn variant_seed( - self, - seed: V, - ) -> Result<(V::Value, VariantDeserializer), Self::Error> + fn variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant), E> where V: de::DeserializeSeed<'de>, { diff --git a/serde_derive/src/bound.rs b/serde_derive/src/bound.rs index f77bede5..77e7824a 100644 --- a/serde_derive/src/bound.rs +++ b/serde_derive/src/bound.rs @@ -169,7 +169,11 @@ where generics } -pub fn with_self_bound(cont: &Container, generics: &syn::Generics, bound: &syn::Path) -> syn::Generics { +pub fn with_self_bound( + cont: &Container, + generics: &syn::Generics, + bound: &syn::Path, +) -> syn::Generics { let mut generics = generics.clone(); generics .where_clause diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index c0b32f36..fd546d69 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -288,7 +288,7 @@ fn deserialize_tuple( None }; - let visit_seq = Stmts(deserialize_seq(&type_path, params, fields, false, cattrs),); + let visit_seq = Stmts(deserialize_seq(&type_path, params, fields, false, cattrs)); let visitor_expr = quote! { __Visitor { @@ -488,7 +488,7 @@ fn deserialize_struct( None => format!("struct {}", params.type_name()), }; - let visit_seq = Stmts(deserialize_seq(&type_path, params, fields, true, cattrs),); + let visit_seq = Stmts(deserialize_seq(&type_path, params, fields, true, cattrs)); let (field_visitor, fields_stmt, visit_map) = deserialize_struct_visitor(type_path, params, fields, cattrs); @@ -618,23 +618,21 @@ fn deserialize_externally_tagged_enum( let variant_visitor = Stmts(deserialize_generated_identifier(variant_names_idents, cattrs, true),); // Match arms to extract a variant from a string - let variant_arms = - variants - .iter() - .enumerate() - .filter(|&(_, variant)| !variant.attrs.skip_deserializing()) - .map( - |(i, variant)| { - let variant_name = field_i(i); + let variant_arms = variants + .iter() + .enumerate() + .filter(|&(_, variant)| !variant.attrs.skip_deserializing()) + .map( + |(i, variant)| { + let variant_name = field_i(i); - let block = - Match(deserialize_externally_tagged_variant(params, variant, cattrs),); + let block = Match(deserialize_externally_tagged_variant(params, variant, cattrs),); - quote! { + quote! { (__Field::#variant_name, __variant) => #block } - }, - ); + }, + ); let all_skipped = variants .iter() @@ -1056,22 +1054,10 @@ fn deserialize_externally_tagged_variant( deserialize_externally_tagged_newtype_variant(variant_ident, params, &variant.fields[0]) } Style::Tuple => { - deserialize_tuple( - Some(variant_ident), - params, - &variant.fields, - cattrs, - None, - ) + deserialize_tuple(Some(variant_ident), params, &variant.fields, cattrs, None) } Style::Struct => { - deserialize_struct( - Some(variant_ident), - params, - &variant.fields, - cattrs, - None, - ) + deserialize_struct(Some(variant_ident), params, &variant.fields, cattrs, None) } } } @@ -1223,7 +1209,7 @@ fn deserialize_generated_identifier( (Some(ignore_variant), Some(fallthrough)) }; - let visitor_impl = Stmts(deserialize_identifier(this, &fields, is_variant, fallthrough)); + let visitor_impl = Stmts(deserialize_identifier(this, &fields, is_variant, fallthrough),); quote_block! { #[allow(non_camel_case_types)] @@ -1289,7 +1275,7 @@ fn deserialize_custom_identifier( let names_idents: Vec<_> = ordinary .iter() - .map(|variant| (variant.attrs.name().deserialize_name(), variant.ident.clone())) + .map(|variant| (variant.attrs.name().deserialize_name(), variant.ident.clone()),) .collect(); let names = names_idents.iter().map(|&(ref name, _)| name); @@ -1308,8 +1294,9 @@ fn deserialize_custom_identifier( Some(fields) }; - let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(params); - let visitor_impl = Stmts(deserialize_identifier(this.clone(), &names_idents, is_variant, fallthrough)); + let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(params,); + let visitor_impl = + Stmts(deserialize_identifier(this.clone(), &names_idents, is_variant, fallthrough),); quote_block! { #names_const @@ -1343,9 +1330,9 @@ fn deserialize_identifier( let field_bytes = fields.iter().map(|&(ref name, _)| quote::ByteStr(name)); let constructors: &Vec<_> = &fields - .iter() - .map(|&(_, ref ident)| quote!(#this::#ident)) - .collect(); + .iter() + .map(|&(_, ref ident)| quote!(#this::#ident)) + .collect(); let expecting = if is_variant { "variant identifier" diff --git a/serde_derive/src/ser.rs b/serde_derive/src/ser.rs index 087702a3..760f9eb9 100644 --- a/serde_derive/src/ser.rs +++ b/serde_derive/src/ser.rs @@ -273,11 +273,7 @@ fn serialize_struct(params: &Parameters, fields: &[Field], cattrs: &attr::Contai } } -fn serialize_enum( - params: &Parameters, - variants: &[Variant], - cattrs: &attr::Container, -) -> Fragment { +fn serialize_enum(params: &Parameters, variants: &[Variant], cattrs: &attr::Container) -> Fragment { assert!(variants.len() as u64 <= u32::MAX as u64); let self_var = ¶ms.self_var; diff --git a/serde_derive_internals/src/ast.rs b/serde_derive_internals/src/ast.rs index 7882334c..2eda55cd 100644 --- a/serde_derive_internals/src/ast.rs +++ b/serde_derive_internals/src/ast.rs @@ -87,8 +87,7 @@ impl<'a> Body<'a> { pub fn all_fields(&'a self) -> Box> + 'a> { match *self { Body::Enum(ref variants) => { - Box::new(variants.iter() - .flat_map(|variant| variant.fields.iter())) + Box::new(variants.iter().flat_map(|variant| variant.fields.iter())) } Body::Struct(_, ref fields) => Box::new(fields.iter()), } @@ -100,16 +99,19 @@ impl<'a> Body<'a> { } fn enum_from_ast<'a>(cx: &Ctxt, variants: &'a [syn::Variant]) -> Vec> { - variants.iter() - .map(|variant| { - let (style, fields) = struct_from_ast(cx, &variant.data); - Variant { - ident: variant.ident.clone(), - attrs: attr::Variant::from_ast(cx, variant), - style: style, - fields: fields, - } - }) + variants + .iter() + .map( + |variant| { + let (style, fields) = struct_from_ast(cx, &variant.data); + Variant { + ident: variant.ident.clone(), + attrs: attr::Variant::from_ast(cx, variant), + style: style, + fields: fields, + } + }, + ) .collect() } @@ -125,14 +127,17 @@ fn struct_from_ast<'a>(cx: &Ctxt, data: &'a syn::VariantData) -> (Style, Vec(cx: &Ctxt, fields: &'a [syn::Field]) -> Vec> { - fields.iter() + fields + .iter() .enumerate() - .map(|(i, field)| { - Field { - ident: field.ident.clone(), - attrs: attr::Field::from_ast(cx, i, field), - ty: &field.ty, - } - }) + .map( + |(i, field)| { + Field { + ident: field.ident.clone(), + attrs: attr::Field::from_ast(cx, i, field), + ty: &field.ty, + } + }, + ) .collect() } diff --git a/serde_derive_internals/src/attr.rs b/serde_derive_internals/src/attr.rs index fd43527b..0e7765d8 100644 --- a/serde_derive_internals/src/attr.rs +++ b/serde_derive_internals/src/attr.rs @@ -41,7 +41,8 @@ impl<'c, T> Attr<'c, T> { fn set(&mut self, value: T) { if self.value.is_some() { - self.cx.error(format!("duplicate serde attribute `{}`", self.name)); + self.cx + .error(format!("duplicate serde attribute `{}`", self.name)); } else { self.value = Some(value); } @@ -227,8 +228,10 @@ impl Container { default.set(Default::Default); } _ => { - cx.error("#[serde(default)] can only be used on structs \ - with named fields") + cx.error( + "#[serde(default)] can only be used on structs \ + with named fields", + ) } } } @@ -241,8 +244,10 @@ impl Container { default.set(Default::Path(path)); } _ => { - cx.error("#[serde(default = \"...\")] can only be used \ - on structs with named fields") + cx.error( + "#[serde(default = \"...\")] can only be used \ + on structs with named fields", + ) } } } @@ -299,8 +304,10 @@ impl Container { content.set(s); } syn::Body::Struct(_) => { - cx.error("#[serde(content = \"...\")] can only be used on \ - enums") + cx.error( + "#[serde(content = \"...\")] can only be used on \ + enums", + ) } } } @@ -431,8 +438,10 @@ fn decide_tag( syn::VariantData::Unit => {} syn::VariantData::Tuple(ref fields) => { if fields.len() != 1 { - cx.error("#[serde(tag = \"...\")] cannot be used with tuple \ - variants"); + cx.error( + "#[serde(tag = \"...\")] cannot be used with tuple \ + variants", + ); break; } } @@ -446,7 +455,7 @@ fn decide_tag( EnumTag::External // doesn't matter, will error } (false, None, Some(_)) => { - cx.error("#[serde(tag = \"...\", content = \"...\")] must be used together"); + cx.error("#[serde(tag = \"...\", content = \"...\")] must be used together",); EnumTag::External } (true, None, Some(_)) => { @@ -460,7 +469,7 @@ fn decide_tag( } } (true, Some(_), Some(_)) => { - cx.error("untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]"); + cx.error("untagged enum cannot have #[serde(tag = \"...\", content = \"...\")]",); EnumTag::External } } @@ -475,7 +484,7 @@ fn decide_identifier( match (&item.body, field_identifier.get(), variant_identifier.get()) { (_, false, false) => Identifier::No, (_, true, true) => { - cx.error("`field_identifier` and `variant_identifier` cannot both be set"); + cx.error("`field_identifier` and `variant_identifier` cannot both be set",); Identifier::No } (&syn::Body::Struct(_), true, false) => { @@ -773,7 +782,13 @@ impl Field { if let Ok(borrowable) = borrowable_lifetimes(cx, &ident, &field.ty) { for lifetime in &lifetimes { if !borrowable.contains(lifetime) { - cx.error(format!("field `{}` does not have lifetime {}", ident, lifetime.ident)); + cx.error( + format!( + "field `{}` does not have lifetime {}", + ident, + lifetime.ident + ), + ); } } borrowed_lifetimes.set(lifetimes); @@ -789,7 +804,7 @@ impl Field { } MetaItem(ref meta_item) => { - cx.error(format!("unknown serde field attribute `{}`", meta_item.name())); + cx.error(format!("unknown serde field attribute `{}`", meta_item.name()),); } Literal(_) => { @@ -909,12 +924,14 @@ impl Field { type SerAndDe = (Option, Option); -fn get_ser_and_de(cx: &Ctxt, - attr_name: &'static str, - items: &[syn::NestedMetaItem], - f: F) - -> Result, ()> - where F: Fn(&Ctxt, &str, &str, &syn::Lit) -> Result +fn get_ser_and_de( + cx: &Ctxt, + attr_name: &'static str, + items: &[syn::NestedMetaItem], + f: F, +) -> Result, ()> +where + F: Fn(&Ctxt, &str, &str, &syn::Lit) -> Result, { let mut ser_item = Attr::none(cx, attr_name); let mut de_item = Attr::none(cx, attr_name); @@ -934,9 +951,13 @@ fn get_ser_and_de(cx: &Ctxt, } _ => { - cx.error(format!("malformed {0} attribute, expected `{0}(serialize = ..., \ + cx.error( + format!( + "malformed {0} attribute, expected `{0}(serialize = ..., \ deserialize = ...)`", - attr_name)); + attr_name + ), + ); return Err(()); } } @@ -949,9 +970,10 @@ fn get_renames(cx: &Ctxt, items: &[syn::NestedMetaItem]) -> Result Result>, ()> { +fn get_where_predicates( + cx: &Ctxt, + items: &[syn::NestedMetaItem], +) -> Result>, ()> { get_ser_and_de(cx, "bound", items, parse_lit_into_where) } @@ -962,17 +984,22 @@ pub fn get_serde_meta_items(attr: &syn::Attribute) -> Option Result { +fn get_string_from_lit( + cx: &Ctxt, + attr_name: &str, + meta_item_name: &str, + lit: &syn::Lit, +) -> Result { if let syn::Lit::Str(ref s, _) = *lit { Ok(s.clone()) } else { - cx.error(format!("expected serde {} attribute to be a string: `{} = \"...\"`", - attr_name, - meta_item_name)); + cx.error( + format!( + "expected serde {} attribute to be a string: `{} = \"...\"`", + attr_name, + meta_item_name + ), + ); Err(()) } } @@ -982,11 +1009,12 @@ fn parse_lit_into_path(cx: &Ctxt, attr_name: &str, lit: &syn::Lit) -> Result Result, ()> { +fn parse_lit_into_where( + cx: &Ctxt, + attr_name: &str, + meta_item_name: &str, + lit: &syn::Lit, +) -> Result, ()> { let string = try!(get_string_from_lit(cx, attr_name, meta_item_name, lit)); if string.is_empty() { return Ok(Vec::new()); @@ -994,26 +1022,28 @@ fn parse_lit_into_where(cx: &Ctxt, let where_string = format!("where {}", string); - syn::parse_where_clause(&where_string).map(|wh| wh.predicates).map_err(|err| cx.error(err)) + syn::parse_where_clause(&where_string) + .map(|wh| wh.predicates) + .map_err(|err| cx.error(err)) } -fn parse_lit_into_ty(cx: &Ctxt, - attr_name: &str, - lit: &syn::Lit) - -> Result { +fn parse_lit_into_ty(cx: &Ctxt, attr_name: &str, lit: &syn::Lit) -> Result { let string = try!(get_string_from_lit(cx, attr_name, attr_name, lit)); - syn::parse_type(&string).map_err(|_| { - cx.error(format!("failed to parse type: {} = {:?}", attr_name, string)) - }) + syn::parse_type(&string).map_err( + |_| { + cx.error(format!("failed to parse type: {} = {:?}", attr_name, string),) + }, + ) } // Parses a string literal like "'a + 'b + 'c" containing a nonempty list of // lifetimes separated by `+`. -fn parse_lit_into_lifetimes(cx: &Ctxt, - attr_name: &str, - lit: &syn::Lit) - -> Result, ()> { +fn parse_lit_into_lifetimes( + cx: &Ctxt, + attr_name: &str, + lit: &syn::Lit, +) -> Result, ()> { let string = try!(get_string_from_lit(cx, attr_name, attr_name, lit)); if string.is_empty() { cx.error("at least one lifetime must be borrowed"); @@ -1035,7 +1065,7 @@ fn parse_lit_into_lifetimes(cx: &Ctxt, return Ok(set); } } - Err(cx.error(format!("failed to parse borrowed lifetimes: {:?}", string))) + Err(cx.error(format!("failed to parse borrowed lifetimes: {:?}", string)),) } // Whether the type looks like it might be `std::borrow::Cow` where elem="T". @@ -1079,10 +1109,8 @@ fn is_cow(ty: &syn::Ty, elem: &str) -> bool { return false; } }; - seg.ident == "Cow" - && params.lifetimes.len() == 1 - && params.types == vec![syn::parse_type(elem).unwrap()] - && params.bindings.is_empty() + seg.ident == "Cow" && params.lifetimes.len() == 1 && + params.types == vec![syn::parse_type(elem).unwrap()] && params.bindings.is_empty() } // Whether the type looks like it might be `&T` where elem="T". This can have @@ -1108,8 +1136,8 @@ fn is_cow(ty: &syn::Ty, elem: &str) -> bool { fn is_rptr(ty: &syn::Ty, elem: &str) -> bool { match *ty { syn::Ty::Rptr(Some(_), ref mut_ty) => { - mut_ty.mutability == syn::Mutability::Immutable - && mut_ty.ty == syn::parse_type(elem).unwrap() + mut_ty.mutability == syn::Mutability::Immutable && + mut_ty.ty == syn::parse_type(elem).unwrap() } _ => false, } @@ -1122,14 +1150,15 @@ fn is_rptr(ty: &syn::Ty, elem: &str) -> bool { // // This is used when there is an explicit or implicit `#[serde(borrow)]` // attribute on the field so there must be at least one borrowable lifetime. -fn borrowable_lifetimes(cx: &Ctxt, - name: &str, - ty: &syn::Ty) - -> Result, ()> { +fn borrowable_lifetimes( + cx: &Ctxt, + name: &str, + ty: &syn::Ty, +) -> Result, ()> { let mut lifetimes = BTreeSet::new(); collect_lifetimes(ty, &mut lifetimes); if lifetimes.is_empty() { - Err(cx.error(format!("field `{}` has no lifetimes to borrow", name))) + Err(cx.error(format!("field `{}` has no lifetimes to borrow", name)),) } else { Ok(lifetimes) } diff --git a/serde_derive_internals/src/case.rs b/serde_derive_internals/src/case.rs index e51f2e3e..0abb50a8 100644 --- a/serde_derive_internals/src/case.rs +++ b/serde_derive_internals/src/case.rs @@ -97,10 +97,12 @@ impl FromStr for RenameRule { #[test] fn rename_variants() { for &(original, lower, camel, snake, screaming, kebab) in - &[("Outcome", "outcome", "outcome", "outcome", "OUTCOME", "outcome"), - ("VeryTasty", "verytasty", "veryTasty", "very_tasty", "VERY_TASTY", "very-tasty"), - ("A", "a", "a", "a", "A", "a"), - ("Z42", "z42", "z42", "z42", "Z42", "z42")] { + &[ + ("Outcome", "outcome", "outcome", "outcome", "OUTCOME", "outcome"), + ("VeryTasty", "verytasty", "veryTasty", "very_tasty", "VERY_TASTY", "very-tasty"), + ("A", "a", "a", "a", "A", "a"), + ("Z42", "z42", "z42", "z42", "Z42", "z42"), + ] { assert_eq!(None.apply_to_variant(original), original); assert_eq!(LowerCase.apply_to_variant(original), lower); assert_eq!(PascalCase.apply_to_variant(original), original); @@ -114,10 +116,12 @@ fn rename_variants() { #[test] fn rename_fields() { for &(original, pascal, camel, screaming, kebab) in - &[("outcome", "Outcome", "outcome", "OUTCOME", "outcome"), - ("very_tasty", "VeryTasty", "veryTasty", "VERY_TASTY", "very-tasty"), - ("a", "A", "a", "A", "a"), - ("z42", "Z42", "z42", "Z42", "z42")] { + &[ + ("outcome", "Outcome", "outcome", "OUTCOME", "outcome"), + ("very_tasty", "VeryTasty", "veryTasty", "VERY_TASTY", "very-tasty"), + ("a", "A", "a", "A", "a"), + ("z42", "Z42", "z42", "Z42", "z42"), + ] { assert_eq!(None.apply_to_field(original), original); assert_eq!(PascalCase.apply_to_field(original), pascal); assert_eq!(CamelCase.apply_to_field(original), camel); diff --git a/serde_derive_internals/src/check.rs b/serde_derive_internals/src/check.rs index 5a58fbaa..84c958ea 100644 --- a/serde_derive_internals/src/check.rs +++ b/serde_derive_internals/src/check.rs @@ -28,8 +28,10 @@ fn check_getter(cx: &Ctxt, cont: &Container) { } Body::Struct(_, _) => { if cont.body.has_getter() && cont.attrs.remote().is_none() { - cx.error("#[serde(getter = \"...\")] can only be used in structs \ - that have #[serde(remote = \"...\")]"); + cx.error( + "#[serde(getter = \"...\")] can only be used in structs \ + that have #[serde(remote = \"...\")]", + ); } } } @@ -52,7 +54,8 @@ fn check_identifier(cx: &Ctxt, cont: &Container) { for (i, variant) in variants.iter().enumerate() { match (variant.style, cont.attrs.identifier(), variant.attrs.other()) { // The `other` attribute may only be used in a field_identifier. - (_, Identifier::Variant, true) | (_, Identifier::No, true) => { + (_, Identifier::Variant, true) | + (_, Identifier::No, true) => { cx.error("#[serde(other)] may only be used inside a field_identifier"); } diff --git a/serde_derive_internals/src/ctxt.rs b/serde_derive_internals/src/ctxt.rs index 80aec476..fa8c57ad 100644 --- a/serde_derive_internals/src/ctxt.rs +++ b/serde_derive_internals/src/ctxt.rs @@ -20,7 +20,11 @@ impl Ctxt { } pub fn error(&self, msg: T) { - self.errors.borrow_mut().as_mut().unwrap().push(msg.to_string()); + self.errors + .borrow_mut() + .as_mut() + .unwrap() + .push(msg.to_string()); } pub fn check(self) -> Result<(), String> {