diff options
Diffstat (limited to 'third_party/rust/derive_more/src')
27 files changed, 5504 insertions, 0 deletions
diff --git a/third_party/rust/derive_more/src/add_assign_like.rs b/third_party/rust/derive_more/src/add_assign_like.rs new file mode 100644 index 0000000000..60a74328ee --- /dev/null +++ b/third_party/rust/derive_more/src/add_assign_like.rs @@ -0,0 +1,41 @@ +use crate::add_helpers::{struct_exprs, tuple_exprs}; +use crate::utils::{add_extra_ty_param_bound_op, named_to_vec, unnamed_to_vec}; +use proc_macro2::{Span, TokenStream}; +use quote::quote; +use syn::{Data, DeriveInput, Fields, Ident}; + +pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream { + let trait_ident = Ident::new(trait_name, Span::call_site()); + let method_name = trait_name.to_string(); + let method_name = method_name.trim_end_matches("Assign"); + let method_name = method_name.to_lowercase(); + let method_ident = Ident::new(&(method_name + "_assign"), Span::call_site()); + let input_type = &input.ident; + + let generics = add_extra_ty_param_bound_op(&input.generics, &trait_ident); + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + + let exprs = match input.data { + Data::Struct(ref data_struct) => match data_struct.fields { + Fields::Unnamed(ref fields) => { + tuple_exprs(&unnamed_to_vec(fields), &method_ident) + } + Fields::Named(ref fields) => { + struct_exprs(&named_to_vec(fields), &method_ident) + } + _ => panic!("Unit structs cannot use derive({})", trait_name), + }, + + _ => panic!("Only structs can use derive({})", trait_name), + }; + + quote!( + impl#impl_generics ::core::ops::#trait_ident for #input_type#ty_generics #where_clause { + #[inline] + fn #method_ident(&mut self, rhs: #input_type#ty_generics) { + #(#exprs; + )* + } + } + ) +} diff --git a/third_party/rust/derive_more/src/add_helpers.rs b/third_party/rust/derive_more/src/add_helpers.rs new file mode 100644 index 0000000000..34f3d2ad5d --- /dev/null +++ b/third_party/rust/derive_more/src/add_helpers.rs @@ -0,0 +1,28 @@ +use proc_macro2::TokenStream; +use quote::quote; +use syn::{Field, Ident, Index}; + +pub fn tuple_exprs(fields: &[&Field], method_ident: &Ident) -> Vec<TokenStream> { + let mut exprs = vec![]; + + for i in 0..fields.len() { + let i = Index::from(i); + // generates `self.0.add(rhs.0)` + let expr = quote!(self.#i.#method_ident(rhs.#i)); + exprs.push(expr); + } + exprs +} + +pub fn struct_exprs(fields: &[&Field], method_ident: &Ident) -> Vec<TokenStream> { + let mut exprs = vec![]; + + for field in fields { + // It's safe to unwrap because struct fields always have an identifier + let field_id = field.ident.as_ref().unwrap(); + // generates `x: self.x.add(rhs.x)` + let expr = quote!(self.#field_id.#method_ident(rhs.#field_id)); + exprs.push(expr) + } + exprs +} diff --git a/third_party/rust/derive_more/src/add_like.rs b/third_party/rust/derive_more/src/add_like.rs new file mode 100644 index 0000000000..5d435f6ec3 --- /dev/null +++ b/third_party/rust/derive_more/src/add_like.rs @@ -0,0 +1,139 @@ +use crate::add_helpers::{struct_exprs, tuple_exprs}; +use crate::utils::{ + add_extra_type_param_bound_op_output, field_idents, named_to_vec, numbered_vars, + unnamed_to_vec, +}; +use proc_macro2::{Span, TokenStream}; +use quote::{quote, ToTokens}; +use std::iter; +use syn::{Data, DataEnum, DeriveInput, Field, Fields, Ident}; + +pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream { + let trait_name = trait_name.trim_end_matches("Self"); + let trait_ident = Ident::new(trait_name, Span::call_site()); + let method_name = trait_name.to_lowercase(); + let method_ident = Ident::new(&method_name, Span::call_site()); + let input_type = &input.ident; + + let generics = add_extra_type_param_bound_op_output(&input.generics, &trait_ident); + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + + let (output_type, block) = match input.data { + Data::Struct(ref data_struct) => match data_struct.fields { + Fields::Unnamed(ref fields) => ( + quote!(#input_type#ty_generics), + tuple_content(input_type, &unnamed_to_vec(fields), &method_ident), + ), + Fields::Named(ref fields) => ( + quote!(#input_type#ty_generics), + struct_content(input_type, &named_to_vec(fields), &method_ident), + ), + _ => panic!("Unit structs cannot use derive({})", trait_name), + }, + Data::Enum(ref data_enum) => ( + quote!(::core::result::Result<#input_type#ty_generics, &'static str>), + enum_content(input_type, data_enum, &method_ident), + ), + + _ => panic!("Only structs and enums can use derive({})", trait_name), + }; + + quote!( + impl#impl_generics ::core::ops::#trait_ident for #input_type#ty_generics #where_clause { + type Output = #output_type; + #[inline] + fn #method_ident(self, rhs: #input_type#ty_generics) -> #output_type { + #block + } + } + ) +} + +fn tuple_content<T: ToTokens>( + input_type: &T, + fields: &[&Field], + method_ident: &Ident, +) -> TokenStream { + let exprs = tuple_exprs(fields, method_ident); + quote!(#input_type(#(#exprs),*)) +} + +fn struct_content( + input_type: &Ident, + fields: &[&Field], + method_ident: &Ident, +) -> TokenStream { + // It's safe to unwrap because struct fields always have an identifier + let exprs = struct_exprs(fields, method_ident); + let field_names = field_idents(fields); + + quote!(#input_type{#(#field_names: #exprs),*}) +} + +#[allow(clippy::cognitive_complexity)] +fn enum_content( + input_type: &Ident, + data_enum: &DataEnum, + method_ident: &Ident, +) -> TokenStream { + let mut matches = vec![]; + let mut method_iter = iter::repeat(method_ident); + + for variant in &data_enum.variants { + let subtype = &variant.ident; + let subtype = quote!(#input_type::#subtype); + + match variant.fields { + Fields::Unnamed(ref fields) => { + // The patern that is outputted should look like this: + // (Subtype(left_vars), TypePath(right_vars)) => Ok(TypePath(exprs)) + let size = unnamed_to_vec(fields).len(); + let l_vars = &numbered_vars(size, "l_"); + let r_vars = &numbered_vars(size, "r_"); + let method_iter = method_iter.by_ref(); + let matcher = quote! { + (#subtype(#(#l_vars),*), + #subtype(#(#r_vars),*)) => { + ::core::result::Result::Ok(#subtype(#(#l_vars.#method_iter(#r_vars)),*)) + } + }; + matches.push(matcher); + } + Fields::Named(ref fields) => { + // The patern that is outputted should look like this: + // (Subtype{a: __l_a, ...}, Subtype{a: __r_a, ...} => { + // Ok(Subtype{a: __l_a.add(__r_a), ...}) + // } + let field_vec = named_to_vec(fields); + let size = field_vec.len(); + let field_names = &field_idents(&field_vec); + let l_vars = &numbered_vars(size, "l_"); + let r_vars = &numbered_vars(size, "r_"); + let method_iter = method_iter.by_ref(); + let matcher = quote! { + (#subtype{#(#field_names: #l_vars),*}, + #subtype{#(#field_names: #r_vars),*}) => { + ::core::result::Result::Ok(#subtype{#(#field_names: #l_vars.#method_iter(#r_vars)),*}) + } + }; + matches.push(matcher); + } + Fields::Unit => { + let message = format!("Cannot {}() unit variants", method_ident); + matches.push(quote!((#subtype, #subtype) => ::core::result::Result::Err(#message))); + } + } + } + + if data_enum.variants.len() > 1 { + // In the strange case where there's only one enum variant this is would be an unreachable + // match. + let message = format!("Trying to {} mismatched enum variants", method_ident); + matches.push(quote!(_ => ::core::result::Result::Err(#message))); + } + quote!( + match (self, rhs) { + #(#matches),* + } + ) +} diff --git a/third_party/rust/derive_more/src/as_mut.rs b/third_party/rust/derive_more/src/as_mut.rs new file mode 100644 index 0000000000..83944648f5 --- /dev/null +++ b/third_party/rust/derive_more/src/as_mut.rs @@ -0,0 +1,83 @@ +use crate::utils::{ + add_where_clauses_for_new_ident, AttrParams, MultiFieldData, State, +}; +use proc_macro2::{Span, TokenStream}; +use quote::quote; +use syn::{parse::Result, DeriveInput, Ident}; + +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let as_mut_type = &Ident::new("__AsMutT", Span::call_site()); + let state = State::with_type_bound( + input, + trait_name, + quote!(::core::convert), + String::from("as_mut"), + AttrParams::ignore_and_forward(), + false, + )?; + let MultiFieldData { + fields, + input_type, + members, + infos, + trait_path, + impl_generics, + ty_generics, + where_clause, + .. + } = state.enabled_fields_data(); + let sub_items: Vec<_> = infos + .iter() + .zip(members.iter()) + .zip(fields) + .map(|((info, member), field)| { + let field_type = &field.ty; + if info.forward { + let trait_path = quote!(#trait_path<#as_mut_type>); + let type_where_clauses = quote! { + where #field_type: #trait_path + }; + let new_generics = add_where_clauses_for_new_ident( + &input.generics, + &[field], + as_mut_type, + type_where_clauses, + false, + ); + let (impl_generics, _, where_clause) = new_generics.split_for_impl(); + let casted_trait = quote!(<#field_type as #trait_path>); + ( + quote!(#casted_trait::as_mut(&mut #member)), + quote!(#impl_generics), + quote!(#where_clause), + quote!(#trait_path), + quote!(#as_mut_type), + ) + } else { + ( + quote!(&mut #member), + quote!(#impl_generics), + quote!(#where_clause), + quote!(#trait_path<#field_type>), + quote!(#field_type), + ) + } + }) + .collect(); + let bodies = sub_items.iter().map(|i| &i.0); + let impl_genericses = sub_items.iter().map(|i| &i.1); + let where_clauses = sub_items.iter().map(|i| &i.2); + let trait_paths = sub_items.iter().map(|i| &i.3); + let return_types = sub_items.iter().map(|i| &i.4); + + Ok(quote! {#( + impl#impl_genericses #trait_paths for #input_type#ty_generics + #where_clauses + { + #[inline] + fn as_mut(&mut self) -> &mut #return_types { + #bodies + } + } + )*}) +} diff --git a/third_party/rust/derive_more/src/as_ref.rs b/third_party/rust/derive_more/src/as_ref.rs new file mode 100644 index 0000000000..4b8ba851d5 --- /dev/null +++ b/third_party/rust/derive_more/src/as_ref.rs @@ -0,0 +1,83 @@ +use crate::utils::{ + add_where_clauses_for_new_ident, AttrParams, MultiFieldData, State, +}; +use proc_macro2::{Span, TokenStream}; +use quote::quote; +use syn::{parse::Result, DeriveInput, Ident}; + +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let as_ref_type = &Ident::new("__AsRefT", Span::call_site()); + let state = State::with_type_bound( + input, + trait_name, + quote!(::core::convert), + String::from("as_ref"), + AttrParams::ignore_and_forward(), + false, + )?; + let MultiFieldData { + fields, + input_type, + members, + infos, + trait_path, + impl_generics, + ty_generics, + where_clause, + .. + } = state.enabled_fields_data(); + let sub_items: Vec<_> = infos + .iter() + .zip(members.iter()) + .zip(fields) + .map(|((info, member), field)| { + let field_type = &field.ty; + if info.forward { + let trait_path = quote!(#trait_path<#as_ref_type>); + let type_where_clauses = quote! { + where #field_type: #trait_path + }; + let new_generics = add_where_clauses_for_new_ident( + &input.generics, + &[field], + as_ref_type, + type_where_clauses, + false, + ); + let (impl_generics, _, where_clause) = new_generics.split_for_impl(); + let casted_trait = quote!(<#field_type as #trait_path>); + ( + quote!(#casted_trait::as_ref(&#member)), + quote!(#impl_generics), + quote!(#where_clause), + quote!(#trait_path), + quote!(#as_ref_type), + ) + } else { + ( + quote!(&#member), + quote!(#impl_generics), + quote!(#where_clause), + quote!(#trait_path<#field_type>), + quote!(#field_type), + ) + } + }) + .collect(); + let bodies = sub_items.iter().map(|i| &i.0); + let impl_generics = sub_items.iter().map(|i| &i.1); + let where_clauses = sub_items.iter().map(|i| &i.2); + let trait_paths = sub_items.iter().map(|i| &i.3); + let return_types = sub_items.iter().map(|i| &i.4); + + Ok(quote! {#( + impl#impl_generics #trait_paths for #input_type#ty_generics + #where_clauses + { + #[inline] + fn as_ref(&self) -> &#return_types { + #bodies + } + } + )*}) +} diff --git a/third_party/rust/derive_more/src/constructor.rs b/third_party/rust/derive_more/src/constructor.rs new file mode 100644 index 0000000000..14d488bfd4 --- /dev/null +++ b/third_party/rust/derive_more/src/constructor.rs @@ -0,0 +1,49 @@ +use crate::utils::{ + field_idents, get_field_types, named_to_vec, numbered_vars, unnamed_to_vec, +}; +use proc_macro2::TokenStream; +use quote::quote; +use syn::{Data, DeriveInput, Field, Fields, Ident}; + +/// Provides the hook to expand `#[derive(Constructor)]` into an implementation of `Constructor` +pub fn expand(input: &DeriveInput, _: &str) -> TokenStream { + let input_type = &input.ident; + let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); + let ((body, vars), fields) = match input.data { + Data::Struct(ref data_struct) => match data_struct.fields { + Fields::Unnamed(ref fields) => { + let field_vec = unnamed_to_vec(fields); + (tuple_body(input_type, &field_vec), field_vec) + } + Fields::Named(ref fields) => { + let field_vec = named_to_vec(fields); + (struct_body(input_type, &field_vec), field_vec) + } + Fields::Unit => (struct_body(input_type, &[]), vec![]), + }, + _ => panic!("Only structs can derive a constructor"), + }; + let original_types = &get_field_types(&fields); + quote! { + #[allow(missing_docs)] + impl#impl_generics #input_type#ty_generics #where_clause { + #[inline] + pub fn new(#(#vars: #original_types),*) -> #input_type#ty_generics { + #body + } + } + } +} + +fn tuple_body(return_type: &Ident, fields: &[&Field]) -> (TokenStream, Vec<Ident>) { + let vars = &numbered_vars(fields.len(), ""); + (quote!(#return_type(#(#vars),*)), vars.clone()) +} + +fn struct_body(return_type: &Ident, fields: &[&Field]) -> (TokenStream, Vec<Ident>) { + let field_names: &Vec<Ident> = + &field_idents(fields).iter().map(|f| (**f).clone()).collect(); + let vars = field_names; + let ret_vars = field_names.clone(); + (quote!(#return_type{#(#field_names: #vars),*}), ret_vars) +} diff --git a/third_party/rust/derive_more/src/deref.rs b/third_party/rust/derive_more/src/deref.rs new file mode 100644 index 0000000000..60e49a76a9 --- /dev/null +++ b/third_party/rust/derive_more/src/deref.rs @@ -0,0 +1,55 @@ +use crate::utils::{add_extra_where_clauses, SingleFieldData, State}; +use proc_macro2::TokenStream; +use quote::quote; +use syn::{parse::Result, DeriveInput}; + +/// Provides the hook to expand `#[derive(Deref)]` into an implementation of `Deref` +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let state = State::with_field_ignore_and_forward( + input, + trait_name, + quote!(::core::ops), + trait_name.to_lowercase(), + )?; + let SingleFieldData { + input_type, + field_type, + trait_path, + casted_trait, + ty_generics, + member, + info, + .. + } = state.assert_single_enabled_field(); + + let (target, body, generics) = if info.forward { + ( + quote!(#casted_trait::Target), + quote!(#casted_trait::deref(&#member)), + add_extra_where_clauses( + &input.generics, + quote! { + where #field_type: #trait_path + }, + ), + ) + } else { + ( + quote!(#field_type), + quote!(&#member), + input.generics.clone(), + ) + }; + let (impl_generics, _, where_clause) = generics.split_for_impl(); + + Ok(quote! { + impl#impl_generics #trait_path for #input_type#ty_generics #where_clause + { + type Target = #target; + #[inline] + fn deref(&self) -> &Self::Target { + #body + } + } + }) +} diff --git a/third_party/rust/derive_more/src/deref_mut.rs b/third_party/rust/derive_more/src/deref_mut.rs new file mode 100644 index 0000000000..85d65121b5 --- /dev/null +++ b/third_party/rust/derive_more/src/deref_mut.rs @@ -0,0 +1,48 @@ +use crate::utils::{add_extra_where_clauses, SingleFieldData, State}; +use proc_macro2::TokenStream; +use quote::quote; +use syn::{parse::Result, DeriveInput}; + +/// Provides the hook to expand `#[derive(DerefMut)]` into an implementation of `DerefMut` +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let state = State::with_field_ignore_and_forward( + input, + trait_name, + quote!(::core::ops), + String::from("deref_mut"), + )?; + let SingleFieldData { + input_type, + trait_path, + casted_trait, + ty_generics, + field_type, + member, + info, + .. + } = state.assert_single_enabled_field(); + let (body, generics) = if info.forward { + ( + quote!(#casted_trait::deref_mut(&mut #member)), + add_extra_where_clauses( + &input.generics, + quote! { + where #field_type: #trait_path + }, + ), + ) + } else { + (quote!(&mut #member), input.generics.clone()) + }; + let (impl_generics, _, where_clause) = generics.split_for_impl(); + + Ok(quote! { + impl#impl_generics #trait_path for #input_type#ty_generics #where_clause + { + #[inline] + fn deref_mut(&mut self) -> &mut Self::Target { + #body + } + } + }) +} diff --git a/third_party/rust/derive_more/src/display.rs b/third_party/rust/derive_more/src/display.rs new file mode 100644 index 0000000000..127531d088 --- /dev/null +++ b/third_party/rust/derive_more/src/display.rs @@ -0,0 +1,888 @@ +use std::{fmt::Display, str::FromStr as _}; + +use proc_macro2::{Ident, Span, TokenStream}; +use quote::{quote, quote_spanned}; +use syn::{ + parse::Parser as _, punctuated::Punctuated, spanned::Spanned as _, Error, Result, +}; + +use crate::utils; +use utils::{HashMap, HashSet}; + +/// Provides the hook to expand `#[derive(Display)]` into an implementation of `From` +pub fn expand(input: &syn::DeriveInput, trait_name: &str) -> Result<TokenStream> { + let trait_name = trait_name.trim_end_matches("Custom"); + let trait_ident = syn::Ident::new(trait_name, Span::call_site()); + let trait_path = "e!(::core::fmt::#trait_ident); + let trait_attr = trait_name_to_attribute_name(trait_name); + let type_params = input + .generics + .type_params() + .map(|t| t.ident.clone()) + .collect(); + + let ParseResult { + arms, + bounds, + requires_helper, + } = State { + trait_path, + trait_attr, + input, + type_params, + } + .get_match_arms_and_extra_bounds()?; + + let generics = if !bounds.is_empty() { + let bounds: Vec<_> = bounds + .into_iter() + .map(|(ty, trait_names)| { + let bounds: Vec<_> = trait_names + .into_iter() + .map(|bound| quote!(#bound)) + .collect(); + quote!(#ty: #(#bounds)+*) + }) + .collect(); + let where_clause = quote_spanned!(input.span()=> where #(#bounds),*); + utils::add_extra_where_clauses(&input.generics, where_clause) + } else { + input.generics.clone() + }; + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + let name = &input.ident; + + let helper_struct = if requires_helper { + display_as_helper_struct() + } else { + TokenStream::new() + }; + + Ok(quote! { + impl #impl_generics #trait_path for #name #ty_generics #where_clause + { + #[allow(unused_variables)] + #[inline] + fn fmt(&self, _derive_more_display_formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + #helper_struct + + match self { + #arms + _ => Ok(()) // This is needed for empty enums + } + } + } + }) +} + +fn trait_name_to_attribute_name(trait_name: &str) -> &'static str { + match trait_name { + "Display" => "display", + "Binary" => "binary", + "Octal" => "octal", + "LowerHex" => "lower_hex", + "UpperHex" => "upper_hex", + "LowerExp" => "lower_exp", + "UpperExp" => "upper_exp", + "Pointer" => "pointer", + "Debug" => "debug", + _ => unimplemented!(), + } +} + +fn attribute_name_to_trait_name(attribute_name: &str) -> &'static str { + match attribute_name { + "display" => "Display", + "binary" => "Binary", + "octal" => "Octal", + "lower_hex" => "LowerHex", + "upper_hex" => "UpperHex", + "lower_exp" => "LowerExp", + "upper_exp" => "UpperExp", + "pointer" => "Pointer", + _ => unreachable!(), + } +} + +fn trait_name_to_trait_bound(trait_name: &str) -> syn::TraitBound { + let path_segments_iterator = vec!["core", "fmt", trait_name] + .into_iter() + .map(|segment| syn::PathSegment::from(Ident::new(segment, Span::call_site()))); + + syn::TraitBound { + lifetimes: None, + modifier: syn::TraitBoundModifier::None, + paren_token: None, + path: syn::Path { + leading_colon: Some(syn::Token![::](Span::call_site())), + segments: path_segments_iterator.collect(), + }, + } +} + +/// Create a helper struct that is required by some `Display` impls. +/// +/// The struct is necessary in cases where `Display` is derived for an enum +/// with an outer `#[display(fmt = "...")]` attribute and if that outer +/// format-string contains a single placeholder. In that case, we have to +/// format twice: +/// +/// - we need to format each variant according to its own, optional +/// format-string, +/// - we then need to insert this formatted variant into the outer +/// format-string. +/// +/// This helper struct solves this as follows: +/// - formatting the whole object inserts the helper struct into the outer +/// format string, +/// - upon being formatted, the helper struct calls an inner closure to produce +/// its formatted result, +/// - the closure in turn uses the inner, optional format-string to produce its +/// result. If there is no inner format-string, it falls back to plain +/// `$trait::fmt()`. +fn display_as_helper_struct() -> TokenStream { + quote! { + struct _derive_more_DisplayAs<F>(F) + where + F: ::core::ops::Fn(&mut ::core::fmt::Formatter) -> ::core::fmt::Result; + + const _derive_more_DisplayAs_impl: () = { + impl<F> ::core::fmt::Display for _derive_more_DisplayAs<F> + where + F: ::core::ops::Fn(&mut ::core::fmt::Formatter) -> ::core::fmt::Result + { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + (self.0)(f) + } + } + }; + } +} + +/// Result type of `State::get_match_arms_and_extra_bounds()`. +#[derive(Default)] +struct ParseResult { + /// The match arms destructuring `self`. + arms: TokenStream, + /// Any trait bounds that may be required. + bounds: HashMap<syn::Type, HashSet<syn::TraitBound>>, + /// `true` if the Display impl requires the `DisplayAs` helper struct. + requires_helper: bool, +} + +struct State<'a, 'b> { + trait_path: &'b TokenStream, + trait_attr: &'static str, + input: &'a syn::DeriveInput, + type_params: HashSet<Ident>, +} + +impl<'a, 'b> State<'a, 'b> { + fn get_proper_fmt_syntax(&self) -> impl Display { + format!( + r#"Proper syntax: #[{}(fmt = "My format", "arg1", "arg2")]"#, + self.trait_attr + ) + } + fn get_proper_bound_syntax(&self) -> impl Display { + format!( + "Proper syntax: #[{}(bound = \"T, U: Trait1 + Trait2, V: Trait3\")]", + self.trait_attr + ) + } + + fn get_matcher(&self, fields: &syn::Fields) -> TokenStream { + match fields { + syn::Fields::Unit => TokenStream::new(), + syn::Fields::Unnamed(fields) => { + let fields: TokenStream = (0..fields.unnamed.len()) + .map(|n| { + let i = Ident::new(&format!("_{}", n), Span::call_site()); + quote!(#i,) + }) + .collect(); + quote!((#fields)) + } + syn::Fields::Named(fields) => { + let fields: TokenStream = fields + .named + .iter() + .map(|f| { + let i = f.ident.as_ref().unwrap(); + quote!(#i,) + }) + .collect(); + quote!({#fields}) + } + } + } + fn find_meta( + &self, + attrs: &[syn::Attribute], + meta_key: &str, + ) -> Result<Option<syn::Meta>> { + let mut metas = Vec::new(); + for meta in attrs.iter().filter_map(|attr| attr.parse_meta().ok()) { + let meta_list = match &meta { + syn::Meta::List(meta) => meta, + _ => continue, + }; + + if !meta_list.path.is_ident(self.trait_attr) { + continue; + } + + use syn::{Meta, NestedMeta}; + let meta_nv = match meta_list.nested.first() { + Some(NestedMeta::Meta(Meta::NameValue(meta_nv))) => meta_nv, + _ => { + // If the given attribute is not MetaNameValue, it most likely implies that the + // user is writing an incorrect format. For example: + // - `#[display()]` + // - `#[display("foo")]` + // - `#[display(foo)]` + return Err(Error::new( + meta.span(), + format!( + r#"The format for this attribute cannot be parsed. Correct format: `#[{}({} = "...")]`"#, + self.trait_attr, meta_key + ), + )); + } + }; + + if meta_nv.path.is_ident(meta_key) { + metas.push(meta); + } + } + + let mut iter = metas.into_iter(); + let meta = iter.next(); + if iter.next().is_none() { + Ok(meta) + } else { + Err(Error::new(meta.span(), "Too many attributes specified")) + } + } + fn parse_meta_bounds( + &self, + bounds: &syn::LitStr, + ) -> Result<HashMap<syn::Type, HashSet<syn::TraitBound>>> { + let span = bounds.span(); + + let input = bounds.value(); + let tokens = TokenStream::from_str(&input)?; + let parser = Punctuated::<syn::GenericParam, syn::Token![,]>::parse_terminated; + + let generic_params = parser + .parse2(tokens) + .map_err(|error| Error::new(span, error.to_string()))?; + + if generic_params.is_empty() { + return Err(Error::new(span, "No bounds specified")); + } + + let mut bounds = HashMap::default(); + + for generic_param in generic_params { + let type_param = match generic_param { + syn::GenericParam::Type(type_param) => type_param, + _ => return Err(Error::new(span, "Only trait bounds allowed")), + }; + + if !self.type_params.contains(&type_param.ident) { + return Err(Error::new( + span, + "Unknown generic type argument specified", + )); + } else if !type_param.attrs.is_empty() { + return Err(Error::new(span, "Attributes aren't allowed")); + } else if type_param.eq_token.is_some() || type_param.default.is_some() { + return Err(Error::new(span, "Default type parameters aren't allowed")); + } + + let ident = type_param.ident.to_string(); + + let ty = syn::Type::Path(syn::TypePath { + qself: None, + path: type_param.ident.into(), + }); + let bounds = bounds.entry(ty).or_insert_with(HashSet::default); + + for bound in type_param.bounds { + let bound = match bound { + syn::TypeParamBound::Trait(bound) => bound, + _ => return Err(Error::new(span, "Only trait bounds allowed")), + }; + + if bound.lifetimes.is_some() { + return Err(Error::new( + span, + "Higher-rank trait bounds aren't allowed", + )); + } + + bounds.insert(bound); + } + + if bounds.is_empty() { + return Err(Error::new( + span, + format!("No bounds specified for type parameter {}", ident), + )); + } + } + + Ok(bounds) + } + fn parse_meta_fmt( + &self, + meta: &syn::Meta, + outer_enum: bool, + ) -> Result<(TokenStream, bool)> { + let list = match meta { + syn::Meta::List(list) => list, + _ => { + return Err(Error::new(meta.span(), self.get_proper_fmt_syntax())); + } + }; + + match &list.nested[0] { + syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { + path, + lit: syn::Lit::Str(fmt), + .. + })) => match path { + op if op.segments.first().expect("path shouldn't be empty").ident + == "fmt" => + { + let expected_affix_usage = "outer `enum` `fmt` is an affix spec that expects no args and at most 1 placeholder for inner variant display"; + if outer_enum { + if list.nested.iter().skip(1).count() != 0 { + return Err(Error::new( + list.nested[1].span(), + expected_affix_usage, + )); + } + // TODO: Check for a single `Display` group? + let fmt_string = match &list.nested[0] { + syn::NestedMeta::Meta(syn::Meta::NameValue( + syn::MetaNameValue { + path, + lit: syn::Lit::Str(s), + .. + }, + )) if path + .segments + .first() + .expect("path shouldn't be empty") + .ident + == "fmt" => + { + s.value() + } + // This one has been checked already in get_meta_fmt() method. + _ => unreachable!(), + }; + + let num_placeholders = + Placeholder::parse_fmt_string(&fmt_string).len(); + if num_placeholders > 1 { + return Err(Error::new( + list.nested[1].span(), + expected_affix_usage, + )); + } + if num_placeholders == 1 { + return Ok((quote_spanned!(fmt.span()=> #fmt), true)); + } + } + let args = list + .nested + .iter() + .skip(1) // skip fmt = "..." + .try_fold(TokenStream::new(), |args, arg| { + let arg = match arg { + syn::NestedMeta::Lit(syn::Lit::Str(s)) => s, + syn::NestedMeta::Meta(syn::Meta::Path(i)) => { + return Ok(quote_spanned!(list.span()=> #args #i,)); + } + _ => { + return Err(Error::new( + arg.span(), + self.get_proper_fmt_syntax(), + )) + } + }; + let arg: TokenStream = + arg.parse().map_err(|e| Error::new(arg.span(), e))?; + Ok(quote_spanned!(list.span()=> #args #arg,)) + })?; + + Ok(( + quote_spanned!(meta.span()=> write!(_derive_more_display_formatter, #fmt, #args)), + false, + )) + } + _ => Err(Error::new( + list.nested[0].span(), + self.get_proper_fmt_syntax(), + )), + }, + _ => Err(Error::new( + list.nested[0].span(), + self.get_proper_fmt_syntax(), + )), + } + } + fn infer_fmt(&self, fields: &syn::Fields, name: &Ident) -> Result<TokenStream> { + let fields = match fields { + syn::Fields::Unit => { + return Ok(quote!( + _derive_more_display_formatter.write_str(stringify!(#name)) + )) + } + syn::Fields::Named(fields) => &fields.named, + syn::Fields::Unnamed(fields) => &fields.unnamed, + }; + if fields.is_empty() { + return Ok(quote!( + _derive_more_display_formatter.write_str(stringify!(#name)) + )); + } else if fields.len() > 1 { + return Err(Error::new( + fields.span(), + "Cannot automatically infer format for types with more than 1 field", + )); + } + + let trait_path = self.trait_path; + if let Some(ident) = &fields.iter().next().as_ref().unwrap().ident { + Ok(quote!(#trait_path::fmt(#ident, _derive_more_display_formatter))) + } else { + Ok(quote!(#trait_path::fmt(_0, _derive_more_display_formatter))) + } + } + fn get_match_arms_and_extra_bounds(&self) -> Result<ParseResult> { + let result: Result<_> = match &self.input.data { + syn::Data::Enum(e) => { + match self + .find_meta(&self.input.attrs, "fmt") + .and_then(|m| m.map(|m| self.parse_meta_fmt(&m, true)).transpose())? + { + // #[display(fmt = "no placeholder")] on whole enum. + Some((fmt, false)) => { + e.variants.iter().try_for_each(|v| { + if let Some(meta) = self.find_meta(&v.attrs, "fmt")? { + Err(Error::new( + meta.span(), + "`fmt` cannot be used on variant when the whole enum has a format string without a placeholder, maybe you want to add a placeholder?", + )) + } else { + Ok(()) + } + })?; + + Ok(ParseResult { + arms: quote_spanned!(self.input.span()=> _ => #fmt,), + bounds: HashMap::default(), + requires_helper: false, + }) + } + // #[display(fmt = "one placeholder: {}")] on whole enum. + Some((outer_fmt, true)) => { + let fmt: Result<TokenStream> = e.variants.iter().try_fold(TokenStream::new(), |arms, v| { + let matcher = self.get_matcher(&v.fields); + let fmt = if let Some(meta) = self.find_meta(&v.attrs, "fmt")? { + self.parse_meta_fmt(&meta, false)?.0 + } else { + self.infer_fmt(&v.fields, &v.ident)? + }; + let name = &self.input.ident; + let v_name = &v.ident; + Ok(quote_spanned!(fmt.span()=> #arms #name::#v_name #matcher => write!( + _derive_more_display_formatter, + #outer_fmt, + _derive_more_DisplayAs(|_derive_more_display_formatter| #fmt) + ),)) + }); + let fmt = fmt?; + Ok(ParseResult { + arms: quote_spanned!(self.input.span()=> #fmt), + bounds: HashMap::default(), + requires_helper: true, + }) + } + // No format attribute on whole enum. + None => e.variants.iter().try_fold(ParseResult::default(), |result, v| { + let ParseResult{ arms, mut bounds, requires_helper } = result; + let matcher = self.get_matcher(&v.fields); + let name = &self.input.ident; + let v_name = &v.ident; + let fmt: TokenStream; + let these_bounds: HashMap<_, _>; + + if let Some(meta) = self.find_meta(&v.attrs, "fmt")? { + fmt = self.parse_meta_fmt(&meta, false)?.0; + these_bounds = self.get_used_type_params_bounds(&v.fields, &meta); + } else { + fmt = self.infer_fmt(&v.fields, v_name)?; + these_bounds = self.infer_type_params_bounds(&v.fields); + }; + these_bounds.into_iter().for_each(|(ty, trait_names)| { + bounds.entry(ty).or_default().extend(trait_names) + }); + let arms = quote_spanned!(self.input.span()=> #arms #name::#v_name #matcher => #fmt,); + + Ok(ParseResult{ arms, bounds, requires_helper }) + }), + } + } + syn::Data::Struct(s) => { + let matcher = self.get_matcher(&s.fields); + let name = &self.input.ident; + let fmt: TokenStream; + let bounds: HashMap<_, _>; + + if let Some(meta) = self.find_meta(&self.input.attrs, "fmt")? { + fmt = self.parse_meta_fmt(&meta, false)?.0; + bounds = self.get_used_type_params_bounds(&s.fields, &meta); + } else { + fmt = self.infer_fmt(&s.fields, name)?; + bounds = self.infer_type_params_bounds(&s.fields); + } + + Ok(ParseResult { + arms: quote_spanned!(self.input.span()=> #name #matcher => #fmt,), + bounds, + requires_helper: false, + }) + } + syn::Data::Union(_) => { + let meta = + self.find_meta(&self.input.attrs, "fmt")?.ok_or_else(|| { + Error::new( + self.input.span(), + "Cannot automatically infer format for unions", + ) + })?; + let fmt = self.parse_meta_fmt(&meta, false)?.0; + + Ok(ParseResult { + arms: quote_spanned!(self.input.span()=> _ => #fmt,), + bounds: HashMap::default(), + requires_helper: false, + }) + } + }; + + let mut result = result?; + + let meta = match self.find_meta(&self.input.attrs, "bound")? { + Some(meta) => meta, + _ => return Ok(result), + }; + + let span = meta.span(); + + let meta = match meta { + syn::Meta::List(meta) => meta.nested, + _ => return Err(Error::new(span, self.get_proper_bound_syntax())), + }; + + if meta.len() != 1 { + return Err(Error::new(span, self.get_proper_bound_syntax())); + } + + let meta = match &meta[0] { + syn::NestedMeta::Meta(syn::Meta::NameValue(meta)) => meta, + _ => return Err(Error::new(span, self.get_proper_bound_syntax())), + }; + + let extra_bounds = match &meta.lit { + syn::Lit::Str(extra_bounds) => extra_bounds, + _ => return Err(Error::new(span, self.get_proper_bound_syntax())), + }; + + let extra_bounds = self.parse_meta_bounds(extra_bounds)?; + + extra_bounds.into_iter().for_each(|(ty, trait_names)| { + result.bounds.entry(ty).or_default().extend(trait_names) + }); + + Ok(result) + } + fn get_used_type_params_bounds( + &self, + fields: &syn::Fields, + meta: &syn::Meta, + ) -> HashMap<syn::Type, HashSet<syn::TraitBound>> { + if self.type_params.is_empty() { + return HashMap::default(); + } + + let fields_type_params: HashMap<syn::Path, _> = fields + .iter() + .enumerate() + .filter_map(|(i, field)| { + utils::get_if_type_parameter_used_in_type(&self.type_params, &field.ty) + .map(|ty| { + ( + field + .ident + .clone() + .unwrap_or_else(|| { + Ident::new(&format!("_{}", i), Span::call_site()) + }) + .into(), + ty, + ) + }) + }) + .collect(); + if fields_type_params.is_empty() { + return HashMap::default(); + } + + let list = match meta { + syn::Meta::List(list) => list, + // This one has been checked already in get_meta_fmt() method. + _ => unreachable!(), + }; + let fmt_args: HashMap<_, _> = list + .nested + .iter() + .skip(1) // skip fmt = "..." + .enumerate() + .filter_map(|(i, arg)| match arg { + syn::NestedMeta::Lit(syn::Lit::Str(ref s)) => { + syn::parse_str(&s.value()).ok().map(|id| (i, id)) + } + syn::NestedMeta::Meta(syn::Meta::Path(ref id)) => Some((i, id.clone())), + // This one has been checked already in get_meta_fmt() method. + _ => unreachable!(), + }) + .collect(); + if fmt_args.is_empty() { + return HashMap::default(); + } + let fmt_string = match &list.nested[0] { + syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { + path, + lit: syn::Lit::Str(s), + .. + })) if path + .segments + .first() + .expect("path shouldn't be empty") + .ident + == "fmt" => + { + s.value() + } + // This one has been checked already in get_meta_fmt() method. + _ => unreachable!(), + }; + + Placeholder::parse_fmt_string(&fmt_string).into_iter().fold( + HashMap::default(), + |mut bounds, pl| { + if let Some(arg) = fmt_args.get(&pl.position) { + if fields_type_params.contains_key(arg) { + bounds + .entry(fields_type_params[arg].clone()) + .or_insert_with(HashSet::default) + .insert(trait_name_to_trait_bound(pl.trait_name)); + } + } + bounds + }, + ) + } + fn infer_type_params_bounds( + &self, + fields: &syn::Fields, + ) -> HashMap<syn::Type, HashSet<syn::TraitBound>> { + if self.type_params.is_empty() { + return HashMap::default(); + } + if let syn::Fields::Unit = fields { + return HashMap::default(); + } + // infer_fmt() uses only first field. + fields + .iter() + .take(1) + .filter_map(|field| { + utils::get_if_type_parameter_used_in_type(&self.type_params, &field.ty) + .map(|ty| { + ( + ty, + [trait_name_to_trait_bound(attribute_name_to_trait_name( + self.trait_attr, + ))] + .iter() + .cloned() + .collect(), + ) + }) + }) + .collect() + } +} + +/// Representation of formatting placeholder. +#[derive(Debug, PartialEq)] +struct Placeholder { + /// Position of formatting argument to be used for this placeholder. + position: usize, + /// Name of [`std::fmt`] trait to be used for rendering this placeholder. + trait_name: &'static str, +} + +impl Placeholder { + /// Parses [`Placeholder`]s from a given formatting string. + fn parse_fmt_string(s: &str) -> Vec<Placeholder> { + let mut n = 0; + crate::parsing::all_placeholders(s) + .into_iter() + .flatten() + .map(|m| { + let (maybe_arg, maybe_typ) = crate::parsing::format(m).unwrap(); + let position = maybe_arg.unwrap_or_else(|| { + // Assign "the next argument". + // https://doc.rust-lang.org/stable/std/fmt/index.html#positional-parameters + n += 1; + n - 1 + }); + let typ = maybe_typ.unwrap_or_default(); + let trait_name = match typ { + "" => "Display", + "?" | "x?" | "X?" => "Debug", + "o" => "Octal", + "x" => "LowerHex", + "X" => "UpperHex", + "p" => "Pointer", + "b" => "Binary", + "e" => "LowerExp", + "E" => "UpperExp", + _ => unreachable!(), + }; + Placeholder { + position, + trait_name, + } + }) + .collect() + } +} + +#[cfg(test)] +mod regex_maybe_placeholder_spec { + + #[test] + fn parses_placeholders_and_omits_escaped() { + let fmt_string = "{}, {:?}, {{}}, {{{1:0$}}}"; + let placeholders: Vec<_> = crate::parsing::all_placeholders(&fmt_string) + .into_iter() + .flatten() + .collect(); + assert_eq!(placeholders, vec!["{}", "{:?}", "{1:0$}"]); + } +} + +#[cfg(test)] +mod regex_placeholder_format_spec { + + #[test] + fn detects_type() { + for (p, expected) in vec![ + ("{}", ""), + ("{:?}", "?"), + ("{:x?}", "x?"), + ("{:X?}", "X?"), + ("{:o}", "o"), + ("{:x}", "x"), + ("{:X}", "X"), + ("{:p}", "p"), + ("{:b}", "b"), + ("{:e}", "e"), + ("{:E}", "E"), + ("{:.*}", ""), + ("{8}", ""), + ("{:04}", ""), + ("{1:0$}", ""), + ("{:width$}", ""), + ("{9:>8.*}", ""), + ("{2:.1$x}", "x"), + ] { + let typ = crate::parsing::format(p).unwrap().1.unwrap_or_default(); + assert_eq!(typ, expected); + } + } + + #[test] + fn detects_arg() { + for (p, expected) in vec![ + ("{}", ""), + ("{0:?}", "0"), + ("{12:x?}", "12"), + ("{3:X?}", "3"), + ("{5:o}", "5"), + ("{6:x}", "6"), + ("{:X}", ""), + ("{8}", "8"), + ("{:04}", ""), + ("{1:0$}", "1"), + ("{:width$}", ""), + ("{9:>8.*}", "9"), + ("{2:.1$x}", "2"), + ] { + let arg = crate::parsing::format(p) + .unwrap() + .0 + .map(|s| s.to_string()) + .unwrap_or_default(); + assert_eq!(arg, String::from(expected)); + } + } +} + +#[cfg(test)] +mod placeholder_parse_fmt_string_spec { + use super::*; + + #[test] + fn indicates_position_and_trait_name_for_each_fmt_placeholder() { + let fmt_string = "{},{:?},{{}},{{{1:0$}}}-{2:.1$x}{0:#?}{:width$}"; + assert_eq!( + Placeholder::parse_fmt_string(&fmt_string), + vec![ + Placeholder { + position: 0, + trait_name: "Display", + }, + Placeholder { + position: 1, + trait_name: "Debug", + }, + Placeholder { + position: 1, + trait_name: "Display", + }, + Placeholder { + position: 2, + trait_name: "LowerHex", + }, + Placeholder { + position: 0, + trait_name: "Debug", + }, + Placeholder { + position: 2, + trait_name: "Display", + }, + ], + ) + } +} diff --git a/third_party/rust/derive_more/src/error.rs b/third_party/rust/derive_more/src/error.rs new file mode 100644 index 0000000000..5767290523 --- /dev/null +++ b/third_party/rust/derive_more/src/error.rs @@ -0,0 +1,439 @@ +use proc_macro2::TokenStream; +use quote::quote; +use syn::{spanned::Spanned as _, Error, Result}; + +use crate::utils::{ + self, AttrParams, DeriveType, FullMetaInfo, HashSet, MetaInfo, MultiFieldData, + State, +}; + +pub fn expand( + input: &syn::DeriveInput, + trait_name: &'static str, +) -> Result<TokenStream> { + let syn::DeriveInput { + ident, generics, .. + } = input; + + let state = State::with_attr_params( + input, + trait_name, + quote!(::std::error), + trait_name.to_lowercase(), + allowed_attr_params(), + )?; + + let type_params: HashSet<_> = generics + .params + .iter() + .filter_map(|generic| match generic { + syn::GenericParam::Type(ty) => Some(ty.ident.clone()), + _ => None, + }) + .collect(); + + let (bounds, source, backtrace) = match state.derive_type { + DeriveType::Named | DeriveType::Unnamed => render_struct(&type_params, &state)?, + DeriveType::Enum => render_enum(&type_params, &state)?, + }; + + let source = source.map(|source| { + quote! { + fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> { + #source + } + } + }); + + let backtrace = backtrace.map(|backtrace| { + quote! { + fn backtrace(&self) -> Option<&::std::backtrace::Backtrace> { + #backtrace + } + } + }); + + let mut generics = generics.clone(); + + if !type_params.is_empty() { + let generic_parameters = generics.params.iter(); + generics = utils::add_extra_where_clauses( + &generics, + quote! { + where + #ident<#(#generic_parameters),*>: ::std::fmt::Debug + ::std::fmt::Display + }, + ); + } + + if !bounds.is_empty() { + let bounds = bounds.iter(); + generics = utils::add_extra_where_clauses( + &generics, + quote! { + where + #(#bounds: ::std::fmt::Debug + ::std::fmt::Display + ::std::error::Error + 'static),* + }, + ); + } + + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + + let render = quote! { + impl#impl_generics ::std::error::Error for #ident#ty_generics #where_clause { + #source + #backtrace + } + }; + + Ok(render) +} + +fn render_struct( + type_params: &HashSet<syn::Ident>, + state: &State, +) -> Result<(HashSet<syn::Type>, Option<TokenStream>, Option<TokenStream>)> { + let parsed_fields = parse_fields(type_params, state)?; + + let source = parsed_fields.render_source_as_struct(); + let backtrace = parsed_fields.render_backtrace_as_struct(); + + Ok((parsed_fields.bounds, source, backtrace)) +} + +fn render_enum( + type_params: &HashSet<syn::Ident>, + state: &State, +) -> Result<(HashSet<syn::Type>, Option<TokenStream>, Option<TokenStream>)> { + let mut bounds = HashSet::default(); + let mut source_match_arms = Vec::new(); + let mut backtrace_match_arms = Vec::new(); + + for variant in state.enabled_variant_data().variants { + let default_info = FullMetaInfo { + enabled: true, + ..FullMetaInfo::default() + }; + + let state = State::from_variant( + state.input, + state.trait_name, + state.trait_module.clone(), + state.trait_attr.clone(), + allowed_attr_params(), + variant, + default_info, + )?; + + let parsed_fields = parse_fields(type_params, &state)?; + + if let Some(expr) = parsed_fields.render_source_as_enum_variant_match_arm() { + source_match_arms.push(expr); + } + + if let Some(expr) = parsed_fields.render_backtrace_as_enum_variant_match_arm() { + backtrace_match_arms.push(expr); + } + + bounds.extend(parsed_fields.bounds.into_iter()); + } + + let render = |match_arms: &mut Vec<TokenStream>| { + if !match_arms.is_empty() && match_arms.len() < state.variants.len() { + match_arms.push(quote!(_ => None)); + } + + if !match_arms.is_empty() { + let expr = quote! { + match self { + #(#match_arms),* + } + }; + + Some(expr) + } else { + None + } + }; + + let source = render(&mut source_match_arms); + let backtrace = render(&mut backtrace_match_arms); + + Ok((bounds, source, backtrace)) +} + +fn allowed_attr_params() -> AttrParams { + AttrParams { + enum_: vec!["ignore"], + struct_: vec!["ignore"], + variant: vec!["ignore"], + field: vec!["ignore", "source", "backtrace"], + } +} + +struct ParsedFields<'input, 'state> { + data: MultiFieldData<'input, 'state>, + source: Option<usize>, + backtrace: Option<usize>, + bounds: HashSet<syn::Type>, +} + +impl<'input, 'state> ParsedFields<'input, 'state> { + fn new(data: MultiFieldData<'input, 'state>) -> Self { + Self { + data, + source: None, + backtrace: None, + bounds: HashSet::default(), + } + } +} + +impl<'input, 'state> ParsedFields<'input, 'state> { + fn render_source_as_struct(&self) -> Option<TokenStream> { + let source = self.source?; + let ident = &self.data.members[source]; + Some(render_some(quote!(&#ident))) + } + + fn render_source_as_enum_variant_match_arm(&self) -> Option<TokenStream> { + let source = self.source?; + let pattern = self.data.matcher(&[source], &[quote!(source)]); + let expr = render_some(quote!(source)); + Some(quote!(#pattern => #expr)) + } + + fn render_backtrace_as_struct(&self) -> Option<TokenStream> { + let backtrace = self.backtrace?; + let backtrace_expr = &self.data.members[backtrace]; + Some(quote!(Some(&#backtrace_expr))) + } + + fn render_backtrace_as_enum_variant_match_arm(&self) -> Option<TokenStream> { + let backtrace = self.backtrace?; + let pattern = self.data.matcher(&[backtrace], &[quote!(backtrace)]); + Some(quote!(#pattern => Some(backtrace))) + } +} + +fn render_some<T>(expr: T) -> TokenStream +where + T: quote::ToTokens, +{ + quote!(Some(#expr as &(dyn ::std::error::Error + 'static))) +} + +fn parse_fields<'input, 'state>( + type_params: &HashSet<syn::Ident>, + state: &'state State<'input>, +) -> Result<ParsedFields<'input, 'state>> { + let mut parsed_fields = match state.derive_type { + DeriveType::Named => { + parse_fields_impl(state, |attr, field, _| { + // Unwrapping is safe, cause fields in named struct + // always have an ident + let ident = field.ident.as_ref().unwrap(); + + match attr { + "source" => ident == "source", + "backtrace" => { + ident == "backtrace" + || is_type_path_ends_with_segment(&field.ty, "Backtrace") + } + _ => unreachable!(), + } + }) + } + + DeriveType::Unnamed => { + let mut parsed_fields = + parse_fields_impl(state, |attr, field, len| match attr { + "source" => { + len == 1 + && !is_type_path_ends_with_segment(&field.ty, "Backtrace") + } + "backtrace" => { + is_type_path_ends_with_segment(&field.ty, "Backtrace") + } + _ => unreachable!(), + })?; + + parsed_fields.source = parsed_fields + .source + .or_else(|| infer_source_field(&state.fields, &parsed_fields)); + + Ok(parsed_fields) + } + + _ => unreachable!(), + }?; + + if let Some(source) = parsed_fields.source { + add_bound_if_type_parameter_used_in_type( + &mut parsed_fields.bounds, + type_params, + &state.fields[source].ty, + ); + } + + Ok(parsed_fields) +} + +/// Checks if `ty` is [`syn::Type::Path`] and ends with segment matching `tail` +/// and doesn't contain any generic parameters. +fn is_type_path_ends_with_segment(ty: &syn::Type, tail: &str) -> bool { + let ty = match ty { + syn::Type::Path(ty) => ty, + _ => return false, + }; + + // Unwrapping is safe, cause 'syn::TypePath.path.segments' + // have to have at least one segment + let segment = ty.path.segments.last().unwrap(); + + match segment.arguments { + syn::PathArguments::None => (), + _ => return false, + }; + + segment.ident == tail +} + +fn infer_source_field( + fields: &[&syn::Field], + parsed_fields: &ParsedFields, +) -> Option<usize> { + // if we have exactly two fields + if fields.len() != 2 { + return None; + } + + // no source field was specified/inferred + if parsed_fields.source.is_some() { + return None; + } + + // but one of the fields was specified/inferred as backtrace field + if let Some(backtrace) = parsed_fields.backtrace { + // then infer *other field* as source field + let source = (backtrace + 1) % 2; + // unless it was explicitly marked as non-source + if parsed_fields.data.infos[source].info.source != Some(false) { + return Some(source); + } + } + + None +} + +fn parse_fields_impl<'input, 'state, P>( + state: &'state State<'input>, + is_valid_default_field_for_attr: P, +) -> Result<ParsedFields<'input, 'state>> +where + P: Fn(&str, &syn::Field, usize) -> bool, +{ + let MultiFieldData { fields, infos, .. } = state.enabled_fields_data(); + + let iter = fields + .iter() + .zip(infos.iter().map(|info| &info.info)) + .enumerate() + .map(|(index, (field, info))| (index, *field, info)); + + let source = parse_field_impl( + &is_valid_default_field_for_attr, + state.fields.len(), + iter.clone(), + "source", + |info| info.source, + )?; + + let backtrace = parse_field_impl( + &is_valid_default_field_for_attr, + state.fields.len(), + iter.clone(), + "backtrace", + |info| info.backtrace, + )?; + + let mut parsed_fields = ParsedFields::new(state.enabled_fields_data()); + + if let Some((index, _, _)) = source { + parsed_fields.source = Some(index); + } + + if let Some((index, _, _)) = backtrace { + parsed_fields.backtrace = Some(index); + } + + Ok(parsed_fields) +} + +fn parse_field_impl<'a, P, V>( + is_valid_default_field_for_attr: &P, + len: usize, + iter: impl Iterator<Item = (usize, &'a syn::Field, &'a MetaInfo)> + Clone, + attr: &str, + value: V, +) -> Result<Option<(usize, &'a syn::Field, &'a MetaInfo)>> +where + P: Fn(&str, &syn::Field, usize) -> bool, + V: Fn(&MetaInfo) -> Option<bool>, +{ + let explicit_fields = iter.clone().filter(|(_, _, info)| match value(info) { + Some(true) => true, + _ => false, + }); + + let inferred_fields = iter.filter(|(_, field, info)| match value(info) { + None => is_valid_default_field_for_attr(attr, field, len), + _ => false, + }); + + let field = assert_iter_contains_zero_or_one_item( + explicit_fields, + &format!( + "Multiple `{}` attributes specified. \ + Single attribute per struct/enum variant allowed.", + attr + ), + )?; + + let field = match field { + field @ Some(_) => field, + None => assert_iter_contains_zero_or_one_item( + inferred_fields, + "Conflicting fields found. Consider specifying some \ + `#[error(...)]` attributes to resolve conflict.", + )?, + }; + + Ok(field) +} + +fn assert_iter_contains_zero_or_one_item<'a>( + mut iter: impl Iterator<Item = (usize, &'a syn::Field, &'a MetaInfo)>, + error_msg: &str, +) -> Result<Option<(usize, &'a syn::Field, &'a MetaInfo)>> { + let item = match iter.next() { + Some(item) => item, + None => return Ok(None), + }; + + if let Some((_, field, _)) = iter.next() { + return Err(Error::new(field.span(), error_msg)); + } + + Ok(Some(item)) +} + +fn add_bound_if_type_parameter_used_in_type( + bounds: &mut HashSet<syn::Type>, + type_params: &HashSet<syn::Ident>, + ty: &syn::Type, +) { + if let Some(ty) = utils::get_if_type_parameter_used_in_type(type_params, ty) { + bounds.insert(ty); + } +} diff --git a/third_party/rust/derive_more/src/from.rs b/third_party/rust/derive_more/src/from.rs new file mode 100644 index 0000000000..56ff5bcf91 --- /dev/null +++ b/third_party/rust/derive_more/src/from.rs @@ -0,0 +1,142 @@ +use std::iter; + +use proc_macro2::{Span, TokenStream}; +use quote::{quote, ToTokens}; +use syn::{parse::Result, DeriveInput, Ident, Index}; + +use crate::utils::{ + add_where_clauses_for_new_ident, AttrParams, DeriveType, HashMap, MultiFieldData, + RefType, State, +}; + +/// Provides the hook to expand `#[derive(From)]` into an implementation of `From` +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let state = State::with_attr_params( + input, + trait_name, + quote!(::core::convert), + trait_name.to_lowercase(), + AttrParams { + enum_: vec!["forward", "ignore"], + variant: vec!["forward", "ignore", "types"], + struct_: vec!["forward", "types"], + field: vec!["forward"], + }, + )?; + if state.derive_type == DeriveType::Enum { + Ok(enum_from(input, state)) + } else { + Ok(struct_from(input, &state)) + } +} + +pub fn struct_from(input: &DeriveInput, state: &State) -> TokenStream { + let multi_field_data = state.enabled_fields_data(); + let MultiFieldData { + fields, + variant_info, + infos, + input_type, + trait_path, + .. + } = multi_field_data.clone(); + + let additional_types = variant_info.additional_types(RefType::No); + let mut impls = Vec::with_capacity(additional_types.len() + 1); + for explicit_type in iter::once(None).chain(additional_types.iter().map(Some)) { + let mut new_generics = input.generics.clone(); + + let mut initializers = Vec::with_capacity(infos.len()); + let mut from_types = Vec::with_capacity(infos.len()); + for (i, (info, field)) in infos.iter().zip(fields.iter()).enumerate() { + let field_type = &field.ty; + let variable = if fields.len() == 1 { + quote! { original } + } else { + let tuple_index = Index::from(i); + quote! { original.#tuple_index } + }; + if let Some(type_) = explicit_type { + initializers.push(quote! { + <#field_type as #trait_path<#type_>>::from(#variable) + }); + from_types.push(quote! { #type_ }); + } else if info.forward { + let type_param = + &Ident::new(&format!("__FromT{}", i), Span::call_site()); + let sub_trait_path = quote! { #trait_path<#type_param> }; + let type_where_clauses = quote! { + where #field_type: #sub_trait_path + }; + new_generics = add_where_clauses_for_new_ident( + &new_generics, + &[field], + type_param, + type_where_clauses, + true, + ); + let casted_trait = quote! { <#field_type as #sub_trait_path> }; + initializers.push(quote! { #casted_trait::from(#variable) }); + from_types.push(quote! { #type_param }); + } else { + initializers.push(variable); + from_types.push(quote! { #field_type }); + } + } + + let body = multi_field_data.initializer(&initializers); + let (impl_generics, _, where_clause) = new_generics.split_for_impl(); + let (_, ty_generics, _) = input.generics.split_for_impl(); + + impls.push(quote! { + #[automatically_derived] + impl#impl_generics #trait_path<(#(#from_types),*)> for + #input_type#ty_generics #where_clause { + + #[inline] + fn from(original: (#(#from_types),*)) -> #input_type#ty_generics { + #body + } + } + }); + } + + quote! { #( #impls )* } +} + +fn enum_from(input: &DeriveInput, state: State) -> TokenStream { + let mut tokens = TokenStream::new(); + + let mut variants_per_types = HashMap::default(); + for variant_state in state.enabled_variant_data().variant_states { + let multi_field_data = variant_state.enabled_fields_data(); + let MultiFieldData { field_types, .. } = multi_field_data.clone(); + variants_per_types + .entry(field_types.clone()) + .or_insert_with(Vec::new) + .push(variant_state); + } + for (ref field_types, ref variant_states) in variants_per_types { + for variant_state in variant_states { + let multi_field_data = variant_state.enabled_fields_data(); + let MultiFieldData { + variant_info, + infos, + .. + } = multi_field_data.clone(); + // If there would be a conflict on a empty tuple derive, ignore the + // variants that are not explicitly enabled or have explicitly enabled + // or disabled fields + if field_types.is_empty() + && variant_states.len() > 1 + && !std::iter::once(variant_info) + .chain(infos) + .any(|info| info.info.enabled.is_some()) + { + continue; + } + struct_from(input, variant_state).to_tokens(&mut tokens); + } + } + tokens +} diff --git a/third_party/rust/derive_more/src/from_str.rs b/third_party/rust/derive_more/src/from_str.rs new file mode 100644 index 0000000000..5f26fdd18c --- /dev/null +++ b/third_party/rust/derive_more/src/from_str.rs @@ -0,0 +1,49 @@ +use crate::utils::{SingleFieldData, State}; +use proc_macro2::TokenStream; +use quote::quote; +use syn::{parse::Result, DeriveInput}; + +/// Provides the hook to expand `#[derive(FromStr)]` into an implementation of `FromStr` +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let state = State::new( + input, + trait_name, + quote!(::core::str), + trait_name.to_lowercase(), + )?; + + // We cannot set defaults for fields, once we do we can remove this check + if state.fields.len() != 1 || state.enabled_fields().len() != 1 { + panic_one_field(trait_name); + } + + let single_field_data = state.assert_single_enabled_field(); + let SingleFieldData { + input_type, + field_type, + trait_path, + casted_trait, + impl_generics, + ty_generics, + where_clause, + .. + } = single_field_data.clone(); + + let initializers = [quote!(#casted_trait::from_str(src)?)]; + let body = single_field_data.initializer(&initializers); + + Ok(quote! { + impl#impl_generics #trait_path for #input_type#ty_generics #where_clause + { + type Err = <#field_type as #trait_path>::Err; + #[inline] + fn from_str(src: &str) -> ::core::result::Result<Self, Self::Err> { + Ok(#body) + } + } + }) +} + +fn panic_one_field(trait_name: &str) -> ! { + panic!("Only structs with one field can derive({})", trait_name) +} diff --git a/third_party/rust/derive_more/src/index.rs b/third_party/rust/derive_more/src/index.rs new file mode 100644 index 0000000000..5aea597693 --- /dev/null +++ b/third_party/rust/derive_more/src/index.rs @@ -0,0 +1,50 @@ +use crate::utils::{add_where_clauses_for_new_ident, SingleFieldData, State}; +use proc_macro2::{Span, TokenStream}; +use quote::quote; +use syn::{parse::Result, DeriveInput, Ident}; + +/// Provides the hook to expand `#[derive(Index)]` into an implementation of `Index` +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let index_type = &Ident::new("__IdxT", Span::call_site()); + let mut state = State::with_field_ignore( + input, + trait_name, + quote!(::core::ops), + trait_name.to_lowercase(), + )?; + state.add_trait_path_type_param(quote!(#index_type)); + let SingleFieldData { + field, + field_type, + input_type, + trait_path_with_params, + casted_trait, + member, + .. + } = state.assert_single_enabled_field(); + + let type_where_clauses = quote! { + where #field_type: #trait_path_with_params + }; + + let new_generics = add_where_clauses_for_new_ident( + &input.generics, + &[field], + index_type, + type_where_clauses, + true, + ); + + let (impl_generics, _, where_clause) = new_generics.split_for_impl(); + let (_, ty_generics, _) = input.generics.split_for_impl(); + Ok(quote! { + impl#impl_generics #trait_path_with_params for #input_type#ty_generics #where_clause + { + type Output = #casted_trait::Output; + #[inline] + fn index(&self, idx: #index_type) -> &Self::Output { + #casted_trait::index(&#member, idx) + } + } + }) +} diff --git a/third_party/rust/derive_more/src/index_mut.rs b/third_party/rust/derive_more/src/index_mut.rs new file mode 100644 index 0000000000..d2c3688051 --- /dev/null +++ b/third_party/rust/derive_more/src/index_mut.rs @@ -0,0 +1,49 @@ +use crate::utils::{add_where_clauses_for_new_ident, SingleFieldData, State}; +use proc_macro2::{Span, TokenStream}; +use quote::quote; +use syn::{parse::Result, DeriveInput, Ident}; + +/// Provides the hook to expand `#[derive(IndexMut)]` into an implementation of `IndexMut` +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let index_type = &Ident::new("__IdxT", Span::call_site()); + let mut state = State::with_field_ignore( + input, + trait_name, + quote!(::core::ops), + String::from("index_mut"), + )?; + state.add_trait_path_type_param(quote!(#index_type)); + let SingleFieldData { + field, + field_type, + input_type, + trait_path_with_params, + casted_trait, + member, + .. + } = state.assert_single_enabled_field(); + + let type_where_clauses = quote! { + where #field_type: #trait_path_with_params + }; + + let new_generics = add_where_clauses_for_new_ident( + &input.generics, + &[field], + index_type, + type_where_clauses, + true, + ); + + let (impl_generics, _, where_clause) = new_generics.split_for_impl(); + let (_, ty_generics, _) = input.generics.split_for_impl(); + Ok(quote! { + impl#impl_generics #trait_path_with_params for #input_type#ty_generics #where_clause + { + #[inline] + fn index_mut(&mut self, idx: #index_type) -> &mut Self::Output { + #casted_trait::index_mut(&mut #member, idx) + } + } + }) +} diff --git a/third_party/rust/derive_more/src/into.rs b/third_party/rust/derive_more/src/into.rs new file mode 100644 index 0000000000..61d820e9b8 --- /dev/null +++ b/third_party/rust/derive_more/src/into.rs @@ -0,0 +1,83 @@ +use std::iter; + +use proc_macro2::TokenStream; +use quote::{quote, ToTokens}; +use syn::{parse::Result, DeriveInput}; + +use crate::utils::{add_extra_generic_param, AttrParams, MultiFieldData, State}; + +/// Provides the hook to expand `#[derive(Into)]` into an implementation of `Into` +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let state = State::with_attr_params( + input, + trait_name, + quote!(::core::convert), + trait_name.to_lowercase(), + AttrParams { + enum_: vec!["ignore", "owned", "ref", "ref_mut"], + variant: vec!["ignore", "owned", "ref", "ref_mut"], + struct_: vec!["ignore", "owned", "ref", "ref_mut", "types"], + field: vec!["ignore"], + }, + )?; + let MultiFieldData { + variant_info, + field_types, + field_idents, + input_type, + .. + } = state.enabled_fields_data(); + + let mut tokens = TokenStream::new(); + + for ref_type in variant_info.ref_types() { + let reference = ref_type.reference(); + let lifetime = ref_type.lifetime(); + let reference_with_lifetime = ref_type.reference_with_lifetime(); + + let generics_impl; + let (_, ty_generics, where_clause) = input.generics.split_for_impl(); + let (impl_generics, _, _) = if ref_type.is_ref() { + generics_impl = add_extra_generic_param(&input.generics, lifetime); + generics_impl.split_for_impl() + } else { + input.generics.split_for_impl() + }; + + let additional_types = variant_info.additional_types(ref_type); + for explicit_type in iter::once(None).chain(additional_types.iter().map(Some)) { + let into_types: Vec<_> = field_types + .iter() + .map(|field_type| { + // No, `.unwrap_or()` won't work here, because we use different types. + if let Some(type_) = explicit_type { + quote! { #reference_with_lifetime #type_ } + } else { + quote! { #reference_with_lifetime #field_type } + } + }) + .collect(); + + let initializers = field_idents.iter().map(|field_ident| { + if let Some(type_) = explicit_type { + quote! { <#reference #type_>::from(#reference original.#field_ident) } + } else { + quote! { #reference original.#field_ident } + } + }); + + (quote! { + #[automatically_derived] + impl#impl_generics ::core::convert::From<#reference_with_lifetime #input_type#ty_generics> for + (#(#into_types),*) #where_clause { + + #[inline] + fn from(original: #reference_with_lifetime #input_type#ty_generics) -> Self { + (#(#initializers),*) + } + } + }).to_tokens(&mut tokens); + } + } + Ok(tokens) +} diff --git a/third_party/rust/derive_more/src/into_iterator.rs b/third_party/rust/derive_more/src/into_iterator.rs new file mode 100644 index 0000000000..45183d4588 --- /dev/null +++ b/third_party/rust/derive_more/src/into_iterator.rs @@ -0,0 +1,59 @@ +use crate::utils::{ + add_extra_generic_param, add_extra_ty_param_bound_ref, SingleFieldData, State, +}; +use proc_macro2::TokenStream; +use quote::{quote, ToTokens}; +use syn::{parse::Result, DeriveInput}; + +/// Provides the hook to expand `#[derive(IntoIterator)]` into an implementation of `IntoIterator` +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let state = State::with_field_ignore_and_refs( + input, + trait_name, + quote!(::core::iter), + String::from("into_iterator"), + )?; + let SingleFieldData { + input_type, + info, + field_type, + member, + trait_path, + .. + } = state.assert_single_enabled_field(); + + let mut tokens = TokenStream::new(); + + for ref_type in info.ref_types() { + let reference = ref_type.reference(); + let lifetime = ref_type.lifetime(); + let reference_with_lifetime = ref_type.reference_with_lifetime(); + + let generics_impl; + let generics = + add_extra_ty_param_bound_ref(&input.generics, trait_path, ref_type); + let (_, ty_generics, where_clause) = generics.split_for_impl(); + let (impl_generics, _, _) = if ref_type.is_ref() { + generics_impl = add_extra_generic_param(&generics, lifetime.clone()); + generics_impl.split_for_impl() + } else { + generics.split_for_impl() + }; + // let generics = add_extra_ty_param_bound(&input.generics, trait_path); + let casted_trait = + "e!(<#reference_with_lifetime #field_type as #trait_path>); + let into_iterator = quote! { + impl#impl_generics #trait_path for #reference_with_lifetime #input_type#ty_generics #where_clause + { + type Item = #casted_trait::Item; + type IntoIter = #casted_trait::IntoIter; + #[inline] + fn into_iter(self) -> Self::IntoIter { + #casted_trait::into_iter(#reference #member) + } + } + }; + into_iterator.to_tokens(&mut tokens); + } + Ok(tokens) +} diff --git a/third_party/rust/derive_more/src/is_variant.rs b/third_party/rust/derive_more/src/is_variant.rs new file mode 100644 index 0000000000..3f19298294 --- /dev/null +++ b/third_party/rust/derive_more/src/is_variant.rs @@ -0,0 +1,62 @@ +use crate::utils::{AttrParams, DeriveType, State}; +use convert_case::{Case, Casing}; +use proc_macro2::TokenStream; +use quote::{format_ident, quote}; +use syn::{DeriveInput, Fields, Ident, Result}; + +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let state = State::with_attr_params( + input, + trait_name, + quote!(), + String::from("is_variant"), + AttrParams { + enum_: vec!["ignore"], + variant: vec!["ignore"], + struct_: vec!["ignore"], + field: vec!["ignore"], + }, + )?; + assert!( + state.derive_type == DeriveType::Enum, + "IsVariant can only be derived for enums" + ); + + let enum_name = &input.ident; + let (imp_generics, type_generics, where_clause) = input.generics.split_for_impl(); + + let mut funcs = vec![]; + for variant_state in state.enabled_variant_data().variant_states { + let variant = variant_state.variant.unwrap(); + let fn_name = Ident::new( + &format_ident!("is_{}", variant.ident) + .to_string() + .to_case(Case::Snake), + variant.ident.span(), + ); + let variant_ident = &variant.ident; + + let data_pattern = match variant.fields { + Fields::Named(_) => quote! { {..} }, + Fields::Unnamed(_) => quote! { (..) }, + Fields::Unit => quote! {}, + }; + let func = quote! { + pub fn #fn_name(&self) -> bool { + match self { + #enum_name ::#variant_ident #data_pattern => true, + _ => false + } + } + }; + funcs.push(func); + } + + let imp = quote! { + impl #imp_generics #enum_name #type_generics #where_clause{ + #(#funcs)* + } + }; + + Ok(imp) +} diff --git a/third_party/rust/derive_more/src/lib.rs b/third_party/rust/derive_more/src/lib.rs new file mode 100644 index 0000000000..339e75040d --- /dev/null +++ b/third_party/rust/derive_more/src/lib.rs @@ -0,0 +1,422 @@ +//! # `derive_more` +//! +//! [![Build Status](https://github.com/JelteF/derive_more/workflows/CI/badge.svg)](https://github.com/JelteF/derive_more/actions) +//! [![Latest Version](https://img.shields.io/crates/v/derive_more.svg)](https://crates.io/crates/derive_more) +//! [![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://jeltef.github.io/derive_more/derive_more/) +//! [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/JelteF/derive_more/master/LICENSE) +//! [![Rust 1.36+](https://img.shields.io/badge/rustc-1.36+-lightgray.svg)](https://blog.rust-lang.org/2019/07/04/Rust-1.36.0.html) +//! +//! Rust has lots of builtin traits that are implemented for its basic types, such +//! as `Add`, `Not`, `From` or `Display`. +//! However, when wrapping these types inside your own structs or enums you lose the +//! implementations of these traits and are required to recreate them. +//! This is especially annoying when your own structures are very simple, such as +//! when using the commonly advised newtype pattern (e.g. `MyInt(i32)`). +//! +//! This library tries to remove these annoyances and the corresponding boilerplate code. +//! It does this by allowing you to derive lots of commonly used traits for both structs and enums. +//! +//! ## Example code +//! +//! By using this library the following code just works: +//! +//! ```rust +//! extern crate derive_more; +//! use derive_more::{Add, Display, From, Into}; +//! +//! #[derive(PartialEq, From, Add)] +//! struct MyInt(i32); +//! +//! #[derive(PartialEq, From, Into)] +//! struct Point2D { +//! x: i32, +//! y: i32, +//! } +//! +//! #[derive(PartialEq, From, Add, Display)] +//! enum MyEnum { +//! #[display(fmt = "int: {}", _0)] +//! Int(i32), +//! Uint(u32), +//! #[display(fmt = "nothing")] +//! Nothing, +//! } +//! +//! assert!(MyInt(11) == MyInt(5) + 6.into()); +//! assert!((5, 6) == Point2D { x: 5, y: 6 }.into()); +//! assert!(MyEnum::Int(15) == (MyEnum::Int(8) + 7.into()).unwrap()); +//! assert!(MyEnum::Int(15).to_string() == "int: 15"); +//! assert!(MyEnum::Uint(42).to_string() == "42"); +//! assert!(MyEnum::Nothing.to_string() == "nothing"); +//! ``` +//! +//! ## The derivable traits +//! +//! Below are all the traits that you can derive using this library. +//! Some trait derivations are so similar that the further documentation will only show a single one +//! of them. +//! You can recognize these by the "-like" suffix in their name. +//! The trait name before that will be the only one that is used throughout the further +//! documentation. +//! +//! It is important to understand what code gets generated when using one of the +//! derives from this crate. +//! That is why the links below explain what code gets generated for a trait for +//! each group from before. +//! +//! You can use the [`cargo-expand`] utility to see the exact code that is generated +//! for your specific type. +//! This will show you your code with all macros and derives expanded. +//! +//! **NOTE**: You still have to derive each trait separately. So `#[derive(Mul)]` doesn't +//! automatically derive `Div` as well. To derive both you should do `#[derive(Mul, Div)]` +//! +//! ### Conversion traits +//! +//! These are traits that are used to convert automatically between types. +//! +//! 1. [`From`] +//! 2. [`Into`] +//! 3. [`FromStr`] +//! 4. [`TryInto`] +//! 5. [`IntoIterator`] +//! 6. [`AsRef`] +//! 7. [`AsMut`] +//! +//! ### Formatting traits +//! +//! These traits are used for converting a struct to a string in different ways. +//! +//! 1. [`Display`-like], contains `Display`, `Binary`, `Octal`, `LowerHex`, +//! `UpperHex`, `LowerExp`, `UpperExp`, `Pointer` +//! +//! ### Error-handling traits +//! These traits are used to define error-types. +//! +//! 1. [`Error`] +//! +//! ### Operators +//! +//! These are traits that can be used for operator overloading. +//! +//! 1. [`Index`] +//! 2. [`Deref`] +//! 3. [`Not`-like], contains `Not` and `Neg` +//! 4. [`Add`-like], contains `Add`, `Sub`, `BitAnd`, `BitOr`, `BitXor` +//! 5. [`Mul`-like], contains `Mul`, `Div`, `Rem`, `Shr` and `Shl` +//! 3. [`Sum`-like], contains `Sum` and `Product` +//! 6. [`IndexMut`] +//! 7. [`DerefMut`] +//! 8. [`AddAssign`-like], contains `AddAssign`, `SubAssign`, `BitAndAssign`, +//! `BitOrAssign` and `BitXorAssign` +//! 9. [`MulAssign`-like], contains `MulAssign`, `DivAssign`, `RemAssign`, +//! `ShrAssign` and `ShlAssign` +//! +//! ### Static methods +//! +//! These don't derive traits, but derive static methods instead. +//! +//! 1. [`Constructor`], this derives a `new` method that can be used as a constructor. +//! This is very basic if you need more customization for your constructor, check +//! out the [`derive-new`] crate. +//! 2. [`IsVariant`], for each variant `foo` of an enum type, derives a `is_foo` method. +//! 3. [`Unwrap`], for each variant `foo` of an enum type, derives an `unwrap_foo` method. +//! +//! ## Generated code +//! +//! ## Installation +//! +//! This library requires Rust 1.36 or higher and it supports `no_std` out of the box. +//! Then add the following to `Cargo.toml`: +//! +//! ```toml +//! [dependencies] +//! derive_more = "0.99.0" +//! # You can specifiy the types of derives that you need for less time spent +//! # compiling. For the full list of features see this crate its Cargo.toml. +//! default-features = false +//! features = ["from", "add", "iterator"] +//! ``` +//! +//! And this to the top of your Rust file for Rust 2018: +//! +//! ```rust +//! extern crate derive_more; +//! // use the derives that you want in the file +//! use derive_more::{Add, Display, From}; +//! ``` +//! If you're still using Rust 2015 you should add this instead: +//! ```rust +//! extern crate core; +//! #[macro_use] +//! extern crate derive_more; +//! # fn main(){} +//! ``` +//! +//! [`cargo-expand`]: https://github.com/dtolnay/cargo-expand +//! [`derive-new`]: https://github.com/nrc/derive-new +//! +//! [`From`]: https://jeltef.github.io/derive_more/derive_more/from.html +//! [`Into`]: https://jeltef.github.io/derive_more/derive_more/into.html +//! [`FromStr`]: https://jeltef.github.io/derive_more/derive_more/from_str.html +//! [`TryInto`]: https://jeltef.github.io/derive_more/derive_more/try_into.html +//! [`IntoIterator`]: https://jeltef.github.io/derive_more/derive_more/into_iterator.html +//! [`AsRef`]: https://jeltef.github.io/derive_more/derive_more/as_ref.html +//! [`AsMut`]: https://jeltef.github.io/derive_more/derive_more/as_mut.html +//! +//! [`Display`-like]: https://jeltef.github.io/derive_more/derive_more/display.html +//! +//! [`Error`]: https://jeltef.github.io/derive_more/derive_more/error.html +//! +//! [`Index`]: https://jeltef.github.io/derive_more/derive_more/index_op.html +//! [`Deref`]: https://jeltef.github.io/derive_more/derive_more/deref.html +//! [`Not`-like]: https://jeltef.github.io/derive_more/derive_more/not.html +//! [`Add`-like]: https://jeltef.github.io/derive_more/derive_more/add.html +//! [`Mul`-like]: https://jeltef.github.io/derive_more/derive_more/mul.html +//! [`Sum`-like]: https://jeltef.github.io/derive_more/derive_more/sum.html +//! [`IndexMut`]: https://jeltef.github.io/derive_more/derive_more/index_mut.html +//! [`DerefMut`]: https://jeltef.github.io/derive_more/derive_more/deref_mut.html +//! [`AddAssign`-like]: https://jeltef.github.io/derive_more/derive_more/add_assign.html +//! [`MulAssign`-like]: https://jeltef.github.io/derive_more/derive_more/mul_assign.html +//! +//! [`Constructor`]: https://jeltef.github.io/derive_more/derive_more/constructor.html +//! [`IsVariant`]: https://jeltef.github.io/derive_more/derive_more/is_variant.html +//! [`Unwrap`]: https://jeltef.github.io/derive_more/derive_more/unwrap.html + +#![recursion_limit = "128"] + +extern crate proc_macro; + +use proc_macro::TokenStream; +use syn::parse::Error as ParseError; + +mod utils; + +#[cfg(any(feature = "add_assign", feature = "mul_assign"))] +mod add_assign_like; +#[cfg(any( + feature = "add", + feature = "add_assign", + feature = "mul", + feature = "mul_assign", +))] +mod add_helpers; +#[cfg(any(feature = "add", feature = "mul"))] +mod add_like; +#[cfg(feature = "as_mut")] +mod as_mut; +#[cfg(feature = "as_ref")] +mod as_ref; +#[cfg(feature = "constructor")] +mod constructor; +#[cfg(feature = "deref")] +mod deref; +#[cfg(feature = "deref_mut")] +mod deref_mut; +#[cfg(feature = "display")] +mod display; +#[cfg(feature = "error")] +mod error; +#[cfg(feature = "from")] +mod from; +#[cfg(feature = "from_str")] +mod from_str; +#[cfg(feature = "index")] +mod index; +#[cfg(feature = "index_mut")] +mod index_mut; +#[cfg(feature = "into")] +mod into; +#[cfg(feature = "into_iterator")] +mod into_iterator; +#[cfg(feature = "is_variant")] +mod is_variant; +#[cfg(feature = "mul_assign")] +mod mul_assign_like; +#[cfg(any(feature = "mul", feature = "mul_assign"))] +mod mul_helpers; +#[cfg(feature = "mul")] +mod mul_like; +#[cfg(feature = "not")] +mod not_like; +#[cfg(feature = "display")] +#[allow(ellipsis_inclusive_range_patterns)] +#[allow(clippy::all)] +mod parsing; +#[cfg(feature = "sum")] +mod sum_like; +#[cfg(feature = "try_into")] +mod try_into; +#[cfg(feature = "unwrap")] +mod unwrap; + +// This trait describes the possible return types of +// the derives. A derive can generally be infallible and +// return a TokenStream, or it can be fallible and return +// a Result<TokenStream, syn::parse::Error>. +trait Output { + fn process(self) -> TokenStream; +} + +impl Output for proc_macro2::TokenStream { + fn process(self) -> TokenStream { + self.into() + } +} + +impl Output for Result<proc_macro2::TokenStream, ParseError> { + fn process(self) -> TokenStream { + match self { + Ok(ts) => ts.into(), + Err(e) => e.to_compile_error().into(), + } + } +} + +macro_rules! create_derive( + ($feature:literal, $mod_:ident, $trait_:ident, $fn_name: ident $(,$attribute:ident)* $(,)?) => { + #[cfg(feature = $feature)] + #[proc_macro_derive($trait_, attributes($($attribute),*))] + #[doc(hidden)] + pub fn $fn_name(input: TokenStream) -> TokenStream { + let ast = syn::parse(input).unwrap(); + Output::process($mod_::expand(&ast, stringify!($trait_))) + } + } +); + +create_derive!("from", from, From, from_derive, from); + +create_derive!("into", into, Into, into_derive, into); + +create_derive!("constructor", constructor, Constructor, constructor_derive); + +create_derive!("not", not_like, Not, not_derive); +create_derive!("not", not_like, Neg, neg_derive); + +create_derive!("add", add_like, Add, add_derive); +create_derive!("add", add_like, Sub, sub_derive); +create_derive!("add", add_like, BitAnd, bit_and_derive); +create_derive!("add", add_like, BitOr, bit_or_derive); +create_derive!("add", add_like, BitXor, bit_xor_derive); + +create_derive!("mul", mul_like, Mul, mul_derive, mul); +create_derive!("mul", mul_like, Div, div_derive, div); +create_derive!("mul", mul_like, Rem, rem_derive, rem); +create_derive!("mul", mul_like, Shr, shr_derive, shr); +create_derive!("mul", mul_like, Shl, shl_derive, shl); + +create_derive!("add_assign", add_assign_like, AddAssign, add_assign_derive,); +create_derive!("add_assign", add_assign_like, SubAssign, sub_assign_derive,); +create_derive!( + "add_assign", + add_assign_like, + BitAndAssign, + bit_and_assign_derive, +); +create_derive!( + "add_assign", + add_assign_like, + BitOrAssign, + bit_or_assign_derive, +); +create_derive!( + "add_assign", + add_assign_like, + BitXorAssign, + bit_xor_assign_derive, +); + +create_derive!( + "mul_assign", + mul_assign_like, + MulAssign, + mul_assign_derive, + mul_assign, +); +create_derive!( + "mul_assign", + mul_assign_like, + DivAssign, + div_assign_derive, + div_assign, +); +create_derive!( + "mul_assign", + mul_assign_like, + RemAssign, + rem_assign_derive, + rem_assign, +); +create_derive!( + "mul_assign", + mul_assign_like, + ShrAssign, + shr_assign_derive, + shr_assign, +); +create_derive!( + "mul_assign", + mul_assign_like, + ShlAssign, + shl_assign_derive, + shl_assign, +); + +create_derive!("sum", sum_like, Sum, sum_derive); +create_derive!("sum", sum_like, Product, product_derive); + +create_derive!("error", error, Error, error_derive, error); + +create_derive!("from_str", from_str, FromStr, from_str_derive); + +create_derive!("display", display, Display, display_derive, display); +create_derive!("display", display, Binary, binary_derive, binary); +create_derive!("display", display, Octal, octal_derive, octal); +create_derive!("display", display, LowerHex, lower_hex_derive, lower_hex); +create_derive!("display", display, UpperHex, upper_hex_derive, upper_hex); +create_derive!("display", display, LowerExp, lower_exp_derive, lower_exp); +create_derive!("display", display, UpperExp, upper_exp_derive, upper_exp); +create_derive!("display", display, Pointer, pointer_derive, pointer); +create_derive!("display", display, DebugCustom, debug_custom_derive, debug); + +create_derive!("index", index, Index, index_derive, index); +create_derive!( + "index_mut", + index_mut, + IndexMut, + index_mut_derive, + index_mut, +); + +create_derive!( + "into_iterator", + into_iterator, + IntoIterator, + into_iterator_derive, + into_iterator, +); + +create_derive!("try_into", try_into, TryInto, try_into_derive, try_into); + +create_derive!("deref", deref, Deref, deref_derive, deref); +create_derive!( + "deref_mut", + deref_mut, + DerefMut, + deref_mut_derive, + deref_mut, +); + +create_derive!("as_ref", as_ref, AsRef, as_ref_derive, as_ref); +create_derive!("as_mut", as_mut, AsMut, as_mut_derive, as_mut); + +create_derive!( + "is_variant", + is_variant, + IsVariant, + is_variant_derive, + is_variant +); + +create_derive!("unwrap", unwrap, Unwrap, unwrap_derive, unwrap); diff --git a/third_party/rust/derive_more/src/mul_assign_like.rs b/third_party/rust/derive_more/src/mul_assign_like.rs new file mode 100644 index 0000000000..eaba70daaf --- /dev/null +++ b/third_party/rust/derive_more/src/mul_assign_like.rs @@ -0,0 +1,65 @@ +use crate::add_assign_like; +use crate::mul_helpers::generics_and_exprs; +use crate::utils::{AttrParams, MultiFieldData, RefType, State}; +use proc_macro2::{Span, TokenStream}; +use quote::quote; +use std::collections::HashSet; +use std::iter; +use syn::{DeriveInput, Ident, Result}; + +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let method_name = trait_name + .to_lowercase() + .trim_end_matches("assign") + .to_string() + + "_assign"; + + let mut state = State::with_attr_params( + input, + trait_name, + quote!(::core::ops), + method_name, + AttrParams::struct_(vec!["forward"]), + )?; + if state.default_info.forward { + return Ok(add_assign_like::expand(input, trait_name)); + } + let scalar_ident = &Ident::new("__RhsT", Span::call_site()); + state.add_trait_path_type_param(quote!(#scalar_ident)); + let multi_field_data = state.enabled_fields_data(); + let MultiFieldData { + input_type, + field_types, + ty_generics, + trait_path, + trait_path_with_params, + method_ident, + .. + } = multi_field_data.clone(); + + let tys = field_types.iter().collect::<HashSet<_>>(); + let tys = tys.iter(); + let trait_path_iter = iter::repeat(trait_path_with_params); + + let type_where_clauses = quote! { + where #(#tys: #trait_path_iter),* + }; + + let (generics, exprs) = generics_and_exprs( + multi_field_data.clone(), + scalar_ident, + type_where_clauses, + RefType::Mut, + ); + let (impl_generics, _, where_clause) = generics.split_for_impl(); + + Ok(quote!( + impl#impl_generics #trait_path<#scalar_ident> for #input_type#ty_generics #where_clause{ + #[inline] + fn #method_ident(&mut self, rhs: #scalar_ident) { + #(#exprs; + )* + } + } + )) +} diff --git a/third_party/rust/derive_more/src/mul_helpers.rs b/third_party/rust/derive_more/src/mul_helpers.rs new file mode 100644 index 0000000000..985d64fe8c --- /dev/null +++ b/third_party/rust/derive_more/src/mul_helpers.rs @@ -0,0 +1,36 @@ +use crate::utils::{add_where_clauses_for_new_ident, MultiFieldData, RefType}; +use proc_macro2::TokenStream; +use quote::quote; +use syn::{Generics, Ident}; + +pub fn generics_and_exprs( + multi_field_data: MultiFieldData, + scalar_ident: &Ident, + type_where_clauses: TokenStream, + ref_type: RefType, +) -> (Generics, Vec<TokenStream>) { + let MultiFieldData { + fields, + casted_traits, + members, + method_ident, + .. + } = multi_field_data; + let reference = ref_type.reference(); + let exprs: Vec<_> = casted_traits + .iter() + .zip(members) + .map( + |(casted_trait, member)| quote!(#casted_trait::#method_ident(#reference #member, rhs)), + ) + .collect(); + + let new_generics = add_where_clauses_for_new_ident( + &multi_field_data.state.input.generics, + &fields, + scalar_ident, + type_where_clauses, + true, + ); + (new_generics, exprs) +} diff --git a/third_party/rust/derive_more/src/mul_like.rs b/third_party/rust/derive_more/src/mul_like.rs new file mode 100644 index 0000000000..734dc0f117 --- /dev/null +++ b/third_party/rust/derive_more/src/mul_like.rs @@ -0,0 +1,62 @@ +use crate::add_like; +use crate::mul_helpers::generics_and_exprs; +use crate::utils::{AttrParams, MultiFieldData, RefType, State}; +use proc_macro2::{Span, TokenStream}; +use quote::quote; +use std::collections::HashSet; +use std::iter; +use syn::{DeriveInput, Ident, Result}; + +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let mut state = State::with_attr_params( + input, + trait_name, + quote!(::core::ops), + trait_name.to_lowercase(), + AttrParams::struct_(vec!["forward"]), + )?; + if state.default_info.forward { + return Ok(add_like::expand(input, trait_name)); + } + + let scalar_ident = &Ident::new("__RhsT", Span::call_site()); + state.add_trait_path_type_param(quote!(#scalar_ident)); + let multi_field_data = state.enabled_fields_data(); + let MultiFieldData { + input_type, + field_types, + ty_generics, + trait_path, + trait_path_with_params, + method_ident, + .. + } = multi_field_data.clone(); + + let tys = field_types.iter().collect::<HashSet<_>>(); + let tys = tys.iter(); + let scalar_iter = iter::repeat(scalar_ident); + let trait_path_iter = iter::repeat(trait_path); + + let type_where_clauses = quote! { + where #(#tys: #trait_path_iter<#scalar_iter, Output=#tys>),* + }; + + let (generics, initializers) = generics_and_exprs( + multi_field_data.clone(), + scalar_ident, + type_where_clauses, + RefType::No, + ); + let body = multi_field_data.initializer(&initializers); + let (impl_generics, _, where_clause) = generics.split_for_impl(); + Ok(quote!( + impl#impl_generics #trait_path_with_params for #input_type#ty_generics #where_clause { + type Output = #input_type#ty_generics; + #[inline] + fn #method_ident(self, rhs: #scalar_ident) -> #input_type#ty_generics { + #body + } + } + + )) +} diff --git a/third_party/rust/derive_more/src/not_like.rs b/third_party/rust/derive_more/src/not_like.rs new file mode 100644 index 0000000000..2639c3b675 --- /dev/null +++ b/third_party/rust/derive_more/src/not_like.rs @@ -0,0 +1,166 @@ +use crate::utils::{ + add_extra_type_param_bound_op_output, named_to_vec, unnamed_to_vec, +}; +use proc_macro2::{Span, TokenStream}; +use quote::{quote, ToTokens}; +use std::iter; +use syn::{Data, DataEnum, DeriveInput, Field, Fields, Ident, Index}; + +pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream { + let trait_ident = Ident::new(trait_name, Span::call_site()); + let method_name = trait_name.to_lowercase(); + let method_ident = &Ident::new(&method_name, Span::call_site()); + let input_type = &input.ident; + + let generics = add_extra_type_param_bound_op_output(&input.generics, &trait_ident); + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + + let (output_type, block) = match input.data { + Data::Struct(ref data_struct) => match data_struct.fields { + Fields::Unnamed(ref fields) => ( + quote!(#input_type#ty_generics), + tuple_content(input_type, &unnamed_to_vec(fields), method_ident), + ), + Fields::Named(ref fields) => ( + quote!(#input_type#ty_generics), + struct_content(input_type, &named_to_vec(fields), method_ident), + ), + _ => panic!("Unit structs cannot use derive({})", trait_name), + }, + Data::Enum(ref data_enum) => { + enum_output_type_and_content(input, data_enum, method_ident) + } + + _ => panic!("Only structs and enums can use derive({})", trait_name), + }; + + quote!( + impl#impl_generics ::core::ops::#trait_ident for #input_type#ty_generics #where_clause { + type Output = #output_type; + #[inline] + fn #method_ident(self) -> #output_type { + #block + } + } + ) +} + +fn tuple_content<T: ToTokens>( + input_type: &T, + fields: &[&Field], + method_ident: &Ident, +) -> TokenStream { + let mut exprs = vec![]; + + for i in 0..fields.len() { + let i = Index::from(i); + // generates `self.0.add()` + let expr = quote!(self.#i.#method_ident()); + exprs.push(expr); + } + + quote!(#input_type(#(#exprs),*)) +} + +fn struct_content( + input_type: &Ident, + fields: &[&Field], + method_ident: &Ident, +) -> TokenStream { + let mut exprs = vec![]; + + for field in fields { + // It's safe to unwrap because struct fields always have an identifier + let field_id = field.ident.as_ref(); + // generates `x: self.x.not()` + let expr = quote!(#field_id: self.#field_id.#method_ident()); + exprs.push(expr) + } + + quote!(#input_type{#(#exprs),*}) +} + +fn enum_output_type_and_content( + input: &DeriveInput, + data_enum: &DataEnum, + method_ident: &Ident, +) -> (TokenStream, TokenStream) { + let input_type = &input.ident; + let (_, ty_generics, _) = input.generics.split_for_impl(); + let mut matches = vec![]; + let mut method_iter = iter::repeat(method_ident); + // If the enum contains unit types that means it can error. + let has_unit_type = data_enum.variants.iter().any(|v| v.fields == Fields::Unit); + + for variant in &data_enum.variants { + let subtype = &variant.ident; + let subtype = quote!(#input_type::#subtype); + + match variant.fields { + Fields::Unnamed(ref fields) => { + // The patern that is outputted should look like this: + // (Subtype(vars)) => Ok(TypePath(exprs)) + let size = unnamed_to_vec(fields).len(); + let vars: &Vec<_> = &(0..size) + .map(|i| Ident::new(&format!("__{}", i), Span::call_site())) + .collect(); + let method_iter = method_iter.by_ref(); + let mut body = quote!(#subtype(#(#vars.#method_iter()),*)); + if has_unit_type { + body = quote!(::core::result::Result::Ok(#body)) + } + let matcher = quote! { + #subtype(#(#vars),*) => { + #body + } + }; + matches.push(matcher); + } + Fields::Named(ref fields) => { + // The patern that is outputted should look like this: + // (Subtype{a: __l_a, ...} => { + // Ok(Subtype{a: __l_a.neg(__r_a), ...}) + // } + let field_vec = named_to_vec(fields); + let size = field_vec.len(); + let field_names: &Vec<_> = &field_vec + .iter() + .map(|f| f.ident.as_ref().unwrap()) + .collect(); + let vars: &Vec<_> = &(0..size) + .map(|i| Ident::new(&format!("__{}", i), Span::call_site())) + .collect(); + let method_iter = method_iter.by_ref(); + let mut body = + quote!(#subtype{#(#field_names: #vars.#method_iter()),*}); + if has_unit_type { + body = quote!(::core::result::Result::Ok(#body)) + } + let matcher = quote! { + #subtype{#(#field_names: #vars),*} => { + #body + } + }; + matches.push(matcher); + } + Fields::Unit => { + let message = format!("Cannot {}() unit variants", method_ident); + matches.push(quote!(#subtype => ::core::result::Result::Err(#message))); + } + } + } + + let body = quote!( + match self { + #(#matches),* + } + ); + + let output_type = if has_unit_type { + quote!(::core::result::Result<#input_type#ty_generics, &'static str>) + } else { + quote!(#input_type#ty_generics) + }; + + (output_type, body) +} diff --git a/third_party/rust/derive_more/src/parsing.rs b/third_party/rust/derive_more/src/parsing.rs new file mode 100644 index 0000000000..528d4382a2 --- /dev/null +++ b/third_party/rust/derive_more/src/parsing.rs @@ -0,0 +1,883 @@ +use self::RuleResult::{Failed, Matched}; +fn escape_default(s: &str) -> String { + s.chars().flat_map(|c| c.escape_default()).collect() +} +fn char_range_at(s: &str, pos: usize) -> (char, usize) { + let c = &s[pos..].chars().next().unwrap(); + let next_pos = pos + c.len_utf8(); + (*c, next_pos) +} +#[derive(Clone)] +enum RuleResult<T> { + Matched(usize, T), + Failed, +} +#[derive(PartialEq, Eq, Debug, Clone)] +pub struct ParseError { + pub line: usize, + pub column: usize, + pub offset: usize, + pub expected: ::std::collections::HashSet<&'static str>, +} +pub type ParseResult<T> = Result<T, ParseError>; +impl ::std::fmt::Display for ParseError { + fn fmt( + &self, + fmt: &mut ::std::fmt::Formatter, + ) -> ::std::result::Result<(), ::std::fmt::Error> { + write!(fmt, "error at {}:{}: expected ", self.line, self.column)?; + if self.expected.len() == 0 { + write!(fmt, "EOF")?; + } else if self.expected.len() == 1 { + write!( + fmt, + "`{}`", + escape_default(self.expected.iter().next().unwrap()) + )?; + } else { + let mut iter = self.expected.iter(); + write!(fmt, "one of `{}`", escape_default(iter.next().unwrap()))?; + for elem in iter { + write!(fmt, ", `{}`", escape_default(elem))?; + } + } + Ok(()) + } +} +impl ::std::error::Error for ParseError { + fn description(&self) -> &str { + "parse error" + } +} +fn slice_eq( + input: &str, + state: &mut ParseState, + pos: usize, + m: &'static str, +) -> RuleResult<()> { + #![inline] + #![allow(dead_code)] + let l = m.len(); + if input.len() >= pos + l && &input.as_bytes()[pos..pos + l] == m.as_bytes() { + Matched(pos + l, ()) + } else { + state.mark_failure(pos, m) + } +} +fn slice_eq_case_insensitive( + input: &str, + state: &mut ParseState, + pos: usize, + m: &'static str, +) -> RuleResult<()> { + #![inline] + #![allow(dead_code)] + let mut used = 0usize; + let mut input_iter = input[pos..].chars().flat_map(|x| x.to_uppercase()); + for m_char_upper in m.chars().flat_map(|x| x.to_uppercase()) { + used += m_char_upper.len_utf8(); + let input_char_result = input_iter.next(); + if input_char_result.is_none() || input_char_result.unwrap() != m_char_upper { + return state.mark_failure(pos, m); + } + } + Matched(pos + used, ()) +} +fn any_char(input: &str, state: &mut ParseState, pos: usize) -> RuleResult<()> { + #![inline] + #![allow(dead_code)] + if input.len() > pos { + let (_, next) = char_range_at(input, pos); + Matched(next, ()) + } else { + state.mark_failure(pos, "<character>") + } +} +fn pos_to_line(input: &str, pos: usize) -> (usize, usize) { + let before = &input[..pos]; + let line = before.as_bytes().iter().filter(|&&c| c == b'\n').count() + 1; + let col = before.chars().rev().take_while(|&c| c != '\n').count() + 1; + (line, col) +} +impl<'input> ParseState<'input> { + fn mark_failure(&mut self, pos: usize, expected: &'static str) -> RuleResult<()> { + if self.suppress_fail == 0 { + if pos > self.max_err_pos { + self.max_err_pos = pos; + self.expected.clear(); + } + if pos == self.max_err_pos { + self.expected.insert(expected); + } + } + Failed + } +} +struct ParseState<'input> { + max_err_pos: usize, + suppress_fail: usize, + expected: ::std::collections::HashSet<&'static str>, + _phantom: ::std::marker::PhantomData<&'input ()>, +} +impl<'input> ParseState<'input> { + fn new() -> ParseState<'input> { + ParseState { + max_err_pos: 0, + suppress_fail: 0, + expected: ::std::collections::HashSet::new(), + _phantom: ::std::marker::PhantomData, + } + } +} + +fn __parse_discard_doubles<'input>( + __input: &'input str, + __state: &mut ParseState<'input>, + __pos: usize, +) -> RuleResult<Option<&'input str>> { + #![allow(non_snake_case, unused)] + { + let __seq_res = { + let __choice_res = { + let __seq_res = slice_eq(__input, __state, __pos, "{"); + match __seq_res { + Matched(__pos, _) => slice_eq(__input, __state, __pos, "{"), + Failed => Failed, + } + }; + match __choice_res { + Matched(__pos, __value) => Matched(__pos, __value), + Failed => { + let __seq_res = slice_eq(__input, __state, __pos, "}"); + match __seq_res { + Matched(__pos, _) => slice_eq(__input, __state, __pos, "}"), + Failed => Failed, + } + } + } + }; + match __seq_res { + Matched(__pos, _) => Matched(__pos, { None }), + Failed => Failed, + } + } +} + +fn __parse_placeholder_inner<'input>( + __input: &'input str, + __state: &mut ParseState<'input>, + __pos: usize, +) -> RuleResult<Option<&'input str>> { + #![allow(non_snake_case, unused)] + { + let __seq_res = { + let str_start = __pos; + match { + let __seq_res = if __input.len() > __pos { + let (__ch, __next) = char_range_at(__input, __pos); + match __ch { + '{' => Matched(__next, ()), + _ => __state.mark_failure(__pos, "[{]"), + } + } else { + __state.mark_failure(__pos, "[{]") + }; + match __seq_res { + Matched(__pos, _) => { + let __seq_res = { + let mut __repeat_pos = __pos; + loop { + let __pos = __repeat_pos; + let __step_res = { + let __seq_res = { + __state.suppress_fail += 1; + let __assert_res = if __input.len() > __pos { + let (__ch, __next) = + char_range_at(__input, __pos); + match __ch { + '{' | '}' => Matched(__next, ()), + _ => { + __state.mark_failure(__pos, "[{}]") + } + } + } else { + __state.mark_failure(__pos, "[{}]") + }; + __state.suppress_fail -= 1; + match __assert_res { + Failed => Matched(__pos, ()), + Matched(..) => Failed, + } + }; + match __seq_res { + Matched(__pos, _) => { + any_char(__input, __state, __pos) + } + Failed => Failed, + } + }; + match __step_res { + Matched(__newpos, __value) => { + __repeat_pos = __newpos; + } + Failed => { + break; + } + } + } + Matched(__repeat_pos, ()) + }; + match __seq_res { + Matched(__pos, _) => { + if __input.len() > __pos { + let (__ch, __next) = char_range_at(__input, __pos); + match __ch { + '}' => Matched(__next, ()), + _ => __state.mark_failure(__pos, "[}]"), + } + } else { + __state.mark_failure(__pos, "[}]") + } + } + Failed => Failed, + } + } + Failed => Failed, + } + } { + Matched(__newpos, _) => { + Matched(__newpos, &__input[str_start..__newpos]) + } + Failed => Failed, + } + }; + match __seq_res { + Matched(__pos, n) => Matched(__pos, { Some(n) }), + Failed => Failed, + } + } +} + +fn __parse_discard_any<'input>( + __input: &'input str, + __state: &mut ParseState<'input>, + __pos: usize, +) -> RuleResult<Option<&'input str>> { + #![allow(non_snake_case, unused)] + { + let __seq_res = any_char(__input, __state, __pos); + match __seq_res { + Matched(__pos, _) => Matched(__pos, { None }), + Failed => Failed, + } + } +} + +fn __parse_arg<'input>( + __input: &'input str, + __state: &mut ParseState<'input>, + __pos: usize, +) -> RuleResult<usize> { + #![allow(non_snake_case, unused)] + { + let __seq_res = { + let str_start = __pos; + match { + let mut __repeat_pos = __pos; + let mut __repeat_value = vec![]; + loop { + let __pos = __repeat_pos; + let __step_res = if __input.len() > __pos { + let (__ch, __next) = char_range_at(__input, __pos); + match __ch { + '0'...'9' => Matched(__next, ()), + _ => __state.mark_failure(__pos, "[0-9]"), + } + } else { + __state.mark_failure(__pos, "[0-9]") + }; + match __step_res { + Matched(__newpos, __value) => { + __repeat_pos = __newpos; + __repeat_value.push(__value); + } + Failed => { + break; + } + } + } + if __repeat_value.len() >= 1 { + Matched(__repeat_pos, ()) + } else { + Failed + } + } { + Matched(__newpos, _) => { + Matched(__newpos, &__input[str_start..__newpos]) + } + Failed => Failed, + } + }; + match __seq_res { + Matched(__pos, n) => Matched(__pos, { n.parse().unwrap() }), + Failed => Failed, + } + } +} + +fn __parse_ty<'input>( + __input: &'input str, + __state: &mut ParseState<'input>, + __pos: usize, +) -> RuleResult<&'input str> { + #![allow(non_snake_case, unused)] + { + let __seq_res = { + let str_start = __pos; + match { + let __choice_res = { + let __choice_res = slice_eq(__input, __state, __pos, "x?"); + match __choice_res { + Matched(__pos, __value) => Matched(__pos, __value), + Failed => slice_eq(__input, __state, __pos, "X?"), + } + }; + match __choice_res { + Matched(__pos, __value) => Matched(__pos, __value), + Failed => { + let __choice_res = slice_eq(__input, __state, __pos, "o"); + match __choice_res { + Matched(__pos, __value) => Matched(__pos, __value), + Failed => { + let __choice_res = + slice_eq(__input, __state, __pos, "x"); + match __choice_res { + Matched(__pos, __value) => Matched(__pos, __value), + Failed => { + let __choice_res = + slice_eq(__input, __state, __pos, "X"); + match __choice_res { + Matched(__pos, __value) => { + Matched(__pos, __value) + } + Failed => { + let __choice_res = slice_eq( + __input, __state, __pos, "p", + ); + match __choice_res { + Matched(__pos, __value) => { + Matched(__pos, __value) + } + Failed => { + let __choice_res = slice_eq( + __input, __state, __pos, + "b", + ); + match __choice_res { + Matched(__pos, __value) => { + Matched(__pos, __value) + } + Failed => { + let __choice_res = + slice_eq( + __input, + __state, __pos, + "e", + ); + match __choice_res { + Matched( + __pos, + __value, + ) => Matched( + __pos, __value, + ), + Failed => { + let __choice_res = + slice_eq( + __input, + __state, + __pos, + "E", + ); + match __choice_res { Matched ( __pos , __value ) => Matched ( __pos , __value ) , Failed => slice_eq ( __input , __state , __pos , "?" ) } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } { + Matched(__newpos, _) => { + Matched(__newpos, &__input[str_start..__newpos]) + } + Failed => Failed, + } + }; + match __seq_res { + Matched(__pos, n) => Matched(__pos, { n }), + Failed => Failed, + } + } +} + +fn __parse_format_spec<'input>( + __input: &'input str, + __state: &mut ParseState<'input>, + __pos: usize, +) -> RuleResult<Option<&'input str>> { + #![allow(non_snake_case, unused)] + { + let __seq_res = slice_eq(__input, __state, __pos, ":"); + match __seq_res { + Matched(__pos, _) => { + let __seq_res = match { + let __seq_res = match { + let __seq_res = { + __state.suppress_fail += 1; + let __assert_res = if __input.len() > __pos { + let (__ch, __next) = char_range_at(__input, __pos); + match __ch { + '<' | '^' | '>' => Matched(__next, ()), + _ => __state.mark_failure(__pos, "[<^>]"), + } + } else { + __state.mark_failure(__pos, "[<^>]") + }; + __state.suppress_fail -= 1; + match __assert_res { + Failed => Matched(__pos, ()), + Matched(..) => Failed, + } + }; + match __seq_res { + Matched(__pos, _) => any_char(__input, __state, __pos), + Failed => Failed, + } + } { + Matched(__newpos, _) => Matched(__newpos, ()), + Failed => Matched(__pos, ()), + }; + match __seq_res { + Matched(__pos, _) => { + if __input.len() > __pos { + let (__ch, __next) = char_range_at(__input, __pos); + match __ch { + '<' | '^' | '>' => Matched(__next, ()), + _ => __state.mark_failure(__pos, "[<^>]"), + } + } else { + __state.mark_failure(__pos, "[<^>]") + } + } + Failed => Failed, + } + } { + Matched(__newpos, _) => Matched(__newpos, ()), + Failed => Matched(__pos, ()), + }; + match __seq_res { + Matched(__pos, _) => { + let __seq_res = match { + let __choice_res = slice_eq(__input, __state, __pos, "+"); + match __choice_res { + Matched(__pos, __value) => Matched(__pos, __value), + Failed => slice_eq(__input, __state, __pos, "-"), + } + } { + Matched(__newpos, _) => Matched(__newpos, ()), + Failed => Matched(__pos, ()), + }; + match __seq_res { + Matched(__pos, _) => { + let __seq_res = + match slice_eq(__input, __state, __pos, "#") { + Matched(__newpos, _) => Matched(__newpos, ()), + Failed => Matched(__pos, ()), + }; + match __seq_res { + Matched(__pos, _) => { + let __seq_res = match { + let __choice_res = { + let __seq_res = { + let mut __repeat_pos = __pos; + let mut __repeat_value = vec![]; + loop { + let __pos = __repeat_pos; + let __step_res = + if __input.len() > __pos { + let (__ch, __next) = + char_range_at( + __input, __pos, + ); + match __ch { + 'A'...'Z' + | 'a'...'z' + | '0'...'9' + | '_' => Matched( + __next, + (), + ), + _ => __state + .mark_failure( + __pos, + "[A-Za-z0-9_]", + ), + } + } else { + __state.mark_failure( + __pos, + "[A-Za-z0-9_]", + ) + }; + match __step_res { + Matched( + __newpos, + __value, + ) => { + __repeat_pos = __newpos; + __repeat_value + .push(__value); + } + Failed => { + break; + } + } + } + if __repeat_value.len() >= 1 { + Matched(__repeat_pos, ()) + } else { + Failed + } + }; + match __seq_res { + Matched(__pos, _) => slice_eq( + __input, __state, __pos, "$", + ), + Failed => Failed, + } + }; + match __choice_res { + Matched(__pos, __value) => { + Matched(__pos, __value) + } + Failed => { + let mut __repeat_pos = __pos; + let mut __repeat_value = vec![]; + loop { + let __pos = __repeat_pos; + let __step_res = if __input + .len() + > __pos + { + let (__ch, __next) = + char_range_at( + __input, __pos, + ); + match __ch { + '0'...'9' => { + Matched(__next, ()) + } + _ => __state + .mark_failure( + __pos, "[0-9]", + ), + } + } else { + __state.mark_failure( + __pos, "[0-9]", + ) + }; + match __step_res { + Matched( + __newpos, + __value, + ) => { + __repeat_pos = __newpos; + __repeat_value + .push(__value); + } + Failed => { + break; + } + } + } + if __repeat_value.len() >= 1 { + Matched(__repeat_pos, ()) + } else { + Failed + } + } + } + } { + Matched(__newpos, _) => { + Matched(__newpos, ()) + } + Failed => Matched(__pos, ()), + }; + match __seq_res { + Matched(__pos, _) => { + let __seq_res = match slice_eq( + __input, __state, __pos, "0", + ) { + Matched(__newpos, _) => { + Matched(__newpos, ()) + } + Failed => Matched(__pos, ()), + }; + match __seq_res { + Matched(__pos, _) => { + let __seq_res = match { + let __seq_res = slice_eq( + __input, __state, + __pos, ".", + ); + match __seq_res { + Matched(__pos, _) => { + let __choice_res = { + let __seq_res = { + let mut + __repeat_pos = + __pos; + let mut + __repeat_value = + vec![]; + loop { + let __pos = __repeat_pos ; + let __step_res = if __input . len ( ) > __pos { let ( __ch , __next ) = char_range_at ( __input , __pos ) ; match __ch { 'A' ... 'Z' | 'a' ... 'z' | '0' ... '9' | '_' => Matched ( __next , ( ) ) , _ => __state . mark_failure ( __pos , "[A-Za-z0-9_]" ) , } } else { __state . mark_failure ( __pos , "[A-Za-z0-9_]" ) } ; + match __step_res { Matched ( __newpos , __value ) => { __repeat_pos = __newpos ; __repeat_value . push ( __value ) ; } , Failed => { break ; } } + } + if __repeat_value . len ( ) >= 1 { Matched ( __repeat_pos , ( ) ) } else { Failed } + }; + match __seq_res { Matched ( __pos , _ ) => { slice_eq ( __input , __state , __pos , "$" ) } Failed => Failed , } + }; + match __choice_res { + Matched( + __pos, + __value, + ) => Matched( + __pos, + __value, + ), + Failed => { + let __choice_res = { + let mut __repeat_pos = __pos ; + let mut + __repeat_value = vec![]; + loop { + let __pos = __repeat_pos ; + let __step_res = if __input . len ( ) > __pos { let ( __ch , __next ) = char_range_at ( __input , __pos ) ; match __ch { '0' ... '9' => Matched ( __next , ( ) ) , _ => __state . mark_failure ( __pos , "[0-9]" ) , } } else { __state . mark_failure ( __pos , "[0-9]" ) } ; + match __step_res { Matched ( __newpos , __value ) => { __repeat_pos = __newpos ; __repeat_value . push ( __value ) ; } , Failed => { break ; } } + } + if __repeat_value . len ( ) >= 1 { Matched ( __repeat_pos , ( ) ) } else { Failed } + }; + match __choice_res { Matched ( __pos , __value ) => Matched ( __pos , __value ) , Failed => slice_eq ( __input , __state , __pos , "*" ) } + } + } + } + Failed => Failed, + } + } { + Matched(__newpos, _) => { + Matched(__newpos, ()) + } + Failed => { + Matched(__pos, ()) + } + }; + match __seq_res { + Matched(__pos, _) => { + let __seq_res = + match __parse_ty( + __input, + __state, __pos, + ) { + Matched( + __newpos, + __value, + ) => Matched( + __newpos, + Some( + __value, + ), + ), + Failed => { + Matched( + __pos, + None, + ) + } + }; + match __seq_res { + Matched( + __pos, + n, + ) => Matched( + __pos, + { n }, + ), + Failed => Failed, + } + } + Failed => Failed, + } + } + Failed => Failed, + } + } + Failed => Failed, + } + } + Failed => Failed, + } + } + Failed => Failed, + } + } + Failed => Failed, + } + } + Failed => Failed, + } + } +} + +fn __parse_all_placeholders<'input>( + __input: &'input str, + __state: &mut ParseState<'input>, + __pos: usize, +) -> RuleResult<Vec<&'input str>> { + #![allow(non_snake_case, unused)] + { + let __seq_res = { + let mut __repeat_pos = __pos; + let mut __repeat_value = vec![]; + loop { + let __pos = __repeat_pos; + let __step_res = { + let __choice_res = __parse_discard_doubles(__input, __state, __pos); + match __choice_res { + Matched(__pos, __value) => Matched(__pos, __value), + Failed => { + let __choice_res = + __parse_placeholder_inner(__input, __state, __pos); + match __choice_res { + Matched(__pos, __value) => Matched(__pos, __value), + Failed => __parse_discard_any(__input, __state, __pos), + } + } + } + }; + match __step_res { + Matched(__newpos, __value) => { + __repeat_pos = __newpos; + __repeat_value.push(__value); + } + Failed => { + break; + } + } + } + Matched(__repeat_pos, __repeat_value) + }; + match __seq_res { + Matched(__pos, x) => { + Matched(__pos, { x.into_iter().flat_map(|x| x).collect() }) + } + Failed => Failed, + } + } +} + +fn __parse_format<'input>( + __input: &'input str, + __state: &mut ParseState<'input>, + __pos: usize, +) -> RuleResult<(Option<usize>, Option<&'input str>)> { + #![allow(non_snake_case, unused)] + { + let __seq_res = slice_eq(__input, __state, __pos, "{"); + match __seq_res { + Matched(__pos, _) => { + let __seq_res = match __parse_arg(__input, __state, __pos) { + Matched(__newpos, __value) => Matched(__newpos, Some(__value)), + Failed => Matched(__pos, None), + }; + match __seq_res { + Matched(__pos, n) => { + let __seq_res = + match __parse_format_spec(__input, __state, __pos) { + Matched(__newpos, __value) => { + Matched(__newpos, Some(__value)) + } + Failed => Matched(__pos, None), + }; + match __seq_res { + Matched(__pos, o) => { + let __seq_res = slice_eq(__input, __state, __pos, "}"); + match __seq_res { + Matched(__pos, _) => { + Matched(__pos, { (n, o.and_then(|x| x)) }) + } + Failed => Failed, + } + } + Failed => Failed, + } + } + Failed => Failed, + } + } + Failed => Failed, + } + } +} + +pub fn all_placeholders<'input>(__input: &'input str) -> ParseResult<Vec<&'input str>> { + #![allow(non_snake_case, unused)] + let mut __state = ParseState::new(); + match __parse_all_placeholders(__input, &mut __state, 0) { + Matched(__pos, __value) => { + if __pos == __input.len() { + return Ok(__value); + } + } + _ => {} + } + let (__line, __col) = pos_to_line(__input, __state.max_err_pos); + Err(ParseError { + line: __line, + column: __col, + offset: __state.max_err_pos, + expected: __state.expected, + }) +} + +pub fn format<'input>( + __input: &'input str, +) -> ParseResult<(Option<usize>, Option<&'input str>)> { + #![allow(non_snake_case, unused)] + let mut __state = ParseState::new(); + match __parse_format(__input, &mut __state, 0) { + Matched(__pos, __value) => { + if __pos == __input.len() { + return Ok(__value); + } + } + _ => {} + } + let (__line, __col) = pos_to_line(__input, __state.max_err_pos); + Err(ParseError { + line: __line, + column: __col, + offset: __state.max_err_pos, + expected: __state.expected, + }) +} diff --git a/third_party/rust/derive_more/src/sum_like.rs b/third_party/rust/derive_more/src/sum_like.rs new file mode 100644 index 0000000000..f8f8a86eab --- /dev/null +++ b/third_party/rust/derive_more/src/sum_like.rs @@ -0,0 +1,56 @@ +use crate::utils::{ + add_extra_ty_param_bound, add_extra_where_clauses, MultiFieldData, State, +}; +use proc_macro2::{Span, TokenStream}; +use quote::quote; +use syn::{DeriveInput, Ident, Result}; + +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let state = State::new( + input, + trait_name, + quote!(::core::iter), + trait_name.to_lowercase(), + )?; + let multi_field_data = state.enabled_fields_data(); + let MultiFieldData { + input_type, + field_types, + trait_path, + method_ident, + .. + } = multi_field_data.clone(); + + let op_trait_name = if trait_name == "Sum" { "Add" } else { "Mul" }; + let op_trait_ident = Ident::new(op_trait_name, Span::call_site()); + let op_path = quote!(::core::ops::#op_trait_ident); + let op_method_ident = + Ident::new(&(op_trait_name.to_lowercase()), Span::call_site()); + let has_type_params = input.generics.type_params().next().is_none(); + let generics = if has_type_params { + input.generics.clone() + } else { + let (_, ty_generics, _) = input.generics.split_for_impl(); + let generics = add_extra_ty_param_bound(&input.generics, trait_path); + let operator_where_clause = quote! { + where #input_type#ty_generics: #op_path<Output=#input_type#ty_generics> + }; + add_extra_where_clauses(&generics, operator_where_clause) + }; + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + + let initializers: Vec<_> = field_types + .iter() + .map(|field_type| quote!(#trait_path::#method_ident(::core::iter::empty::<#field_type>()))) + .collect(); + let identity = multi_field_data.initializer(&initializers); + + Ok(quote!( + impl#impl_generics #trait_path for #input_type#ty_generics #where_clause { + #[inline] + fn #method_ident<I: ::core::iter::Iterator<Item = Self>>(iter: I) -> Self { + iter.fold(#identity, #op_path::#op_method_ident) + } + } + )) +} diff --git a/third_party/rust/derive_more/src/try_into.rs b/third_party/rust/derive_more/src/try_into.rs new file mode 100644 index 0000000000..996bcdf458 --- /dev/null +++ b/third_party/rust/derive_more/src/try_into.rs @@ -0,0 +1,122 @@ +use crate::utils::{ + add_extra_generic_param, numbered_vars, AttrParams, DeriveType, MultiFieldData, + State, +}; +use proc_macro2::TokenStream; +use quote::{quote, ToTokens}; +use syn::{DeriveInput, Result}; + +use crate::utils::HashMap; + +/// Provides the hook to expand `#[derive(TryInto)]` into an implementation of `TryInto` +#[allow(clippy::cognitive_complexity)] +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let state = State::with_attr_params( + input, + trait_name, + quote!(::core::convert), + String::from("try_into"), + AttrParams { + enum_: vec!["ignore", "owned", "ref", "ref_mut"], + variant: vec!["ignore", "owned", "ref", "ref_mut"], + struct_: vec!["ignore", "owned", "ref", "ref_mut"], + field: vec!["ignore"], + }, + )?; + assert!( + state.derive_type == DeriveType::Enum, + "Only enums can derive TryInto" + ); + + let mut variants_per_types = HashMap::default(); + + for variant_state in state.enabled_variant_data().variant_states { + let multi_field_data = variant_state.enabled_fields_data(); + let MultiFieldData { + variant_info, + field_types, + .. + } = multi_field_data.clone(); + for ref_type in variant_info.ref_types() { + variants_per_types + .entry((ref_type, field_types.clone())) + .or_insert_with(Vec::new) + .push(multi_field_data.clone()); + } + } + + let mut tokens = TokenStream::new(); + + for ((ref_type, ref original_types), ref multi_field_datas) in variants_per_types { + let input_type = &input.ident; + + let pattern_ref = ref_type.pattern_ref(); + let lifetime = ref_type.lifetime(); + let reference_with_lifetime = ref_type.reference_with_lifetime(); + + let mut matchers = vec![]; + let vars = &numbered_vars(original_types.len(), ""); + for multi_field_data in multi_field_datas { + let patterns: Vec<_> = + vars.iter().map(|var| quote!(#pattern_ref #var)).collect(); + matchers.push( + multi_field_data.matcher(&multi_field_data.field_indexes, &patterns), + ); + } + + let vars = if vars.len() == 1 { + quote!(#(#vars)*) + } else { + quote!((#(#vars),*)) + }; + + let output_type = if original_types.len() == 1 { + format!("{}", quote!(#(#original_types)*)) + } else { + let types = original_types + .iter() + .map(|t| format!("{}", quote!(#t))) + .collect::<Vec<_>>(); + format!("({})", types.join(", ")) + }; + let variant_names = multi_field_datas + .iter() + .map(|d| { + format!( + "{}", + d.variant_name.expect("Somehow there was no variant name") + ) + }) + .collect::<Vec<_>>() + .join(", "); + let message = + format!("Only {} can be converted to {}", variant_names, output_type); + + let generics_impl; + let (_, ty_generics, where_clause) = input.generics.split_for_impl(); + let (impl_generics, _, _) = if ref_type.is_ref() { + generics_impl = add_extra_generic_param(&input.generics, lifetime.clone()); + generics_impl.split_for_impl() + } else { + input.generics.split_for_impl() + }; + + let try_from = quote! { + impl#impl_generics ::core::convert::TryFrom<#reference_with_lifetime #input_type#ty_generics> for + (#(#reference_with_lifetime #original_types),*) #where_clause { + type Error = &'static str; + + #[allow(unused_variables)] + #[inline] + fn try_from(value: #reference_with_lifetime #input_type#ty_generics) -> ::core::result::Result<Self, Self::Error> { + match value { + #(#matchers)|* => ::core::result::Result::Ok(#vars), + _ => ::core::result::Result::Err(#message), + } + } + } + }; + try_from.to_tokens(&mut tokens) + } + Ok(tokens) +} diff --git a/third_party/rust/derive_more/src/unwrap.rs b/third_party/rust/derive_more/src/unwrap.rs new file mode 100644 index 0000000000..e2243d3a2f --- /dev/null +++ b/third_party/rust/derive_more/src/unwrap.rs @@ -0,0 +1,101 @@ +use crate::utils::{AttrParams, DeriveType, State}; +use convert_case::{Case, Casing}; +use proc_macro2::TokenStream; +use quote::{format_ident, quote}; +use syn::{DeriveInput, Fields, Ident, Result}; + +pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStream> { + let state = State::with_attr_params( + input, + trait_name, + quote!(), + String::from("unwrap"), + AttrParams { + enum_: vec!["ignore"], + variant: vec!["ignore"], + struct_: vec!["ignore"], + field: vec!["ignore"], + }, + )?; + assert!( + state.derive_type == DeriveType::Enum, + "Unwrap can only be derived for enums" + ); + + let enum_name = &input.ident; + let (imp_generics, type_generics, where_clause) = input.generics.split_for_impl(); + + let mut funcs = vec![]; + for variant_state in state.enabled_variant_data().variant_states { + let variant = variant_state.variant.unwrap(); + let fn_name = Ident::new( + &format_ident!("unwrap_{}", variant.ident) + .to_string() + .to_case(Case::Snake), + variant.ident.span(), + ); + let variant_ident = &variant.ident; + + let (data_pattern, ret_value, ret_type) = match variant.fields { + Fields::Named(_) => panic!("cannot unwrap anonymous records"), + Fields::Unnamed(ref fields) => { + let data_pattern = + (0..fields.unnamed.len()).fold(vec![], |mut a, n| { + a.push(format_ident!("field_{}", n)); + a + }); + let ret_type = &fields.unnamed; + ( + quote! { (#(#data_pattern),*) }, + quote! { (#(#data_pattern),*) }, + quote! { (#ret_type) }, + ) + } + Fields::Unit => (quote! {}, quote! { () }, quote! { () }), + }; + + let other_arms = state.variant_states.iter().map(|variant| { + variant.variant.unwrap() + }).filter(|variant| { + &variant.ident != variant_ident + }).map(|variant| { + let data_pattern = match variant.fields { + Fields::Named(_) => quote! { {..} }, + Fields::Unnamed(_) => quote! { (..) }, + Fields::Unit => quote! {}, + }; + let variant_ident = &variant.ident; + quote! { #enum_name :: #variant_ident #data_pattern => + panic!(concat!("called `", stringify!(#enum_name), "::", stringify!(#fn_name), + "()` on a `", stringify!(#variant_ident), "` value")) + } + }); + + // The `track-caller` feature is set by our build script based + // on rustc version detection, as `#[track_caller]` was + // stabilized in a later version (1.46) of Rust than our MSRV (1.36). + let track_caller = if cfg!(feature = "track-caller") { + quote! { #[track_caller] } + } else { + quote! {} + }; + let func = quote! { + #track_caller + pub fn #fn_name(self) -> #ret_type { + match self { + #enum_name ::#variant_ident #data_pattern => #ret_value, + #(#other_arms),* + } + } + }; + funcs.push(func); + } + + let imp = quote! { + impl #imp_generics #enum_name #type_generics #where_clause{ + #(#funcs)* + } + }; + + Ok(imp) +} diff --git a/third_party/rust/derive_more/src/utils.rs b/third_party/rust/derive_more/src/utils.rs new file mode 100644 index 0000000000..72264701d8 --- /dev/null +++ b/third_party/rust/derive_more/src/utils.rs @@ -0,0 +1,1244 @@ +#![cfg_attr(not(feature = "default"), allow(dead_code), allow(unused_mut))] + +use proc_macro2::{Span, TokenStream}; +use quote::{quote, ToTokens}; +use syn::{ + parse_quote, punctuated::Punctuated, spanned::Spanned, Attribute, Data, + DeriveInput, Error, Field, Fields, FieldsNamed, FieldsUnnamed, GenericParam, + Generics, Ident, ImplGenerics, Index, Meta, NestedMeta, Result, Token, Type, + TypeGenerics, TypeParamBound, Variant, WhereClause, +}; + +#[derive(Clone, Copy, Default)] +pub struct DeterministicState; + +impl std::hash::BuildHasher for DeterministicState { + type Hasher = std::collections::hash_map::DefaultHasher; + + fn build_hasher(&self) -> Self::Hasher { + Self::Hasher::default() + } +} + +pub type HashMap<K, V> = std::collections::HashMap<K, V, DeterministicState>; +pub type HashSet<K> = std::collections::HashSet<K, DeterministicState>; + +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] +pub enum RefType { + No, + Ref, + Mut, +} + +impl RefType { + pub fn lifetime(self) -> TokenStream { + match self { + RefType::No => quote!(), + _ => quote!('__deriveMoreLifetime), + } + } + + pub fn reference(self) -> TokenStream { + match self { + RefType::No => quote!(), + RefType::Ref => quote!(&), + RefType::Mut => quote!(&mut), + } + } + + pub fn mutability(self) -> TokenStream { + match self { + RefType::Mut => quote!(mut), + _ => quote!(), + } + } + + pub fn pattern_ref(self) -> TokenStream { + match self { + RefType::Ref => quote!(ref), + RefType::Mut => quote!(ref mut), + RefType::No => quote!(), + } + } + + pub fn reference_with_lifetime(self) -> TokenStream { + if !self.is_ref() { + return quote!(); + } + let lifetime = self.lifetime(); + let mutability = self.mutability(); + quote!(&#lifetime #mutability) + } + + pub fn is_ref(self) -> bool { + match self { + RefType::No => false, + _ => true, + } + } + + pub fn from_attr_name(name: &str) -> Self { + match name { + "owned" => RefType::No, + "ref" => RefType::Ref, + "ref_mut" => RefType::Mut, + _ => panic!("'{}' is not a RefType", name), + } + } +} + +pub fn numbered_vars(count: usize, prefix: &str) -> Vec<Ident> { + (0..count) + .map(|i| Ident::new(&format!("__{}{}", prefix, i), Span::call_site())) + .collect() +} + +pub fn field_idents<'a>(fields: &'a [&'a Field]) -> Vec<&'a Ident> { + fields + .iter() + .map(|f| { + f.ident + .as_ref() + .expect("Tried to get field names of a tuple struct") + }) + .collect() +} + +pub fn get_field_types_iter<'a>( + fields: &'a [&'a Field], +) -> Box<dyn Iterator<Item = &'a Type> + 'a> { + Box::new(fields.iter().map(|f| &f.ty)) +} + +pub fn get_field_types<'a>(fields: &'a [&'a Field]) -> Vec<&'a Type> { + get_field_types_iter(fields).collect() +} + +pub fn add_extra_type_param_bound_op_output<'a>( + generics: &'a Generics, + trait_ident: &'a Ident, +) -> Generics { + let mut generics = generics.clone(); + for type_param in &mut generics.type_params_mut() { + let type_ident = &type_param.ident; + let bound: TypeParamBound = parse_quote! { + ::core::ops::#trait_ident<Output=#type_ident> + }; + type_param.bounds.push(bound) + } + + generics +} + +pub fn add_extra_ty_param_bound_op<'a>( + generics: &'a Generics, + trait_ident: &'a Ident, +) -> Generics { + add_extra_ty_param_bound(generics, "e!(::core::ops::#trait_ident)) +} + +pub fn add_extra_ty_param_bound<'a>( + generics: &'a Generics, + bound: &'a TokenStream, +) -> Generics { + let mut generics = generics.clone(); + let bound: TypeParamBound = parse_quote! { #bound }; + for type_param in &mut generics.type_params_mut() { + type_param.bounds.push(bound.clone()) + } + + generics +} + +pub fn add_extra_ty_param_bound_ref<'a>( + generics: &'a Generics, + bound: &'a TokenStream, + ref_type: RefType, +) -> Generics { + match ref_type { + RefType::No => add_extra_ty_param_bound(generics, bound), + _ => { + let generics = generics.clone(); + let idents = generics.type_params().map(|x| &x.ident); + let ref_with_lifetime = ref_type.reference_with_lifetime(); + add_extra_where_clauses( + &generics, + quote!( + where #(#ref_with_lifetime #idents: #bound),* + ), + ) + } + } +} + +pub fn add_extra_generic_param( + generics: &Generics, + generic_param: TokenStream, +) -> Generics { + let generic_param: GenericParam = parse_quote! { #generic_param }; + let mut generics = generics.clone(); + generics.params.push(generic_param); + + generics +} + +pub fn add_extra_generic_type_param( + generics: &Generics, + generic_param: TokenStream, +) -> Generics { + let generic_param: GenericParam = parse_quote! { #generic_param }; + let lifetimes: Vec<GenericParam> = + generics.lifetimes().map(|x| x.clone().into()).collect(); + let type_params: Vec<GenericParam> = + generics.type_params().map(|x| x.clone().into()).collect(); + let const_params: Vec<GenericParam> = + generics.const_params().map(|x| x.clone().into()).collect(); + let mut generics = generics.clone(); + generics.params = Default::default(); + generics.params.extend(lifetimes); + generics.params.extend(type_params); + generics.params.push(generic_param); + generics.params.extend(const_params); + + generics +} + +pub fn add_extra_where_clauses( + generics: &Generics, + type_where_clauses: TokenStream, +) -> Generics { + let mut type_where_clauses: WhereClause = parse_quote! { #type_where_clauses }; + let mut new_generics = generics.clone(); + if let Some(old_where) = new_generics.where_clause { + type_where_clauses.predicates.extend(old_where.predicates) + } + new_generics.where_clause = Some(type_where_clauses); + + new_generics +} + +pub fn add_where_clauses_for_new_ident<'a>( + generics: &'a Generics, + fields: &[&'a Field], + type_ident: &Ident, + type_where_clauses: TokenStream, + sized: bool, +) -> Generics { + let generic_param = if fields.len() > 1 { + quote!(#type_ident: ::core::marker::Copy) + } else if sized { + quote!(#type_ident) + } else { + quote!(#type_ident: ?::core::marker::Sized) + }; + + let generics = add_extra_where_clauses(generics, type_where_clauses); + add_extra_generic_type_param(&generics, generic_param) +} + +pub fn unnamed_to_vec(fields: &FieldsUnnamed) -> Vec<&Field> { + fields.unnamed.iter().collect() +} + +pub fn named_to_vec(fields: &FieldsNamed) -> Vec<&Field> { + fields.named.iter().collect() +} + +fn panic_one_field(trait_name: &str, trait_attr: &str) -> ! { + panic!( + "derive({}) only works when forwarding to a single field. Try putting #[{}] or #[{}(ignore)] on the fields in the struct", + trait_name, trait_attr, trait_attr, + ) +} + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum DeriveType { + Unnamed, + Named, + Enum, +} + +pub struct State<'input> { + pub input: &'input DeriveInput, + pub trait_name: &'static str, + pub trait_ident: Ident, + pub method_ident: Ident, + pub trait_module: TokenStream, + pub trait_path: TokenStream, + pub trait_path_params: Vec<TokenStream>, + pub trait_attr: String, + pub derive_type: DeriveType, + pub fields: Vec<&'input Field>, + pub variants: Vec<&'input Variant>, + pub variant_states: Vec<State<'input>>, + pub variant: Option<&'input Variant>, + pub generics: Generics, + pub default_info: FullMetaInfo, + full_meta_infos: Vec<FullMetaInfo>, +} + +#[derive(Default, Clone)] +pub struct AttrParams { + pub enum_: Vec<&'static str>, + pub variant: Vec<&'static str>, + pub struct_: Vec<&'static str>, + pub field: Vec<&'static str>, +} + +impl AttrParams { + pub fn new(params: Vec<&'static str>) -> AttrParams { + AttrParams { + enum_: params.clone(), + struct_: params.clone(), + variant: params.clone(), + field: params, + } + } + pub fn struct_(params: Vec<&'static str>) -> AttrParams { + AttrParams { + enum_: vec![], + struct_: params, + variant: vec![], + field: vec![], + } + } + + pub fn ignore_and_forward() -> AttrParams { + AttrParams::new(vec!["ignore", "forward"]) + } +} + +impl<'input> State<'input> { + pub fn new<'arg_input>( + input: &'arg_input DeriveInput, + trait_name: &'static str, + trait_module: TokenStream, + trait_attr: String, + ) -> Result<State<'arg_input>> { + State::new_impl( + input, + trait_name, + trait_module, + trait_attr, + AttrParams::default(), + true, + ) + } + + pub fn with_field_ignore<'arg_input>( + input: &'arg_input DeriveInput, + trait_name: &'static str, + trait_module: TokenStream, + trait_attr: String, + ) -> Result<State<'arg_input>> { + State::new_impl( + input, + trait_name, + trait_module, + trait_attr, + AttrParams::new(vec!["ignore"]), + true, + ) + } + + pub fn with_field_ignore_and_forward<'arg_input>( + input: &'arg_input DeriveInput, + trait_name: &'static str, + trait_module: TokenStream, + trait_attr: String, + ) -> Result<State<'arg_input>> { + State::new_impl( + input, + trait_name, + trait_module, + trait_attr, + AttrParams::new(vec!["ignore", "forward"]), + true, + ) + } + + pub fn with_field_ignore_and_refs<'arg_input>( + input: &'arg_input DeriveInput, + trait_name: &'static str, + trait_module: TokenStream, + trait_attr: String, + ) -> Result<State<'arg_input>> { + State::new_impl( + input, + trait_name, + trait_module, + trait_attr, + AttrParams::new(vec!["ignore", "owned", "ref", "ref_mut"]), + true, + ) + } + + pub fn with_attr_params<'arg_input>( + input: &'arg_input DeriveInput, + trait_name: &'static str, + trait_module: TokenStream, + trait_attr: String, + allowed_attr_params: AttrParams, + ) -> Result<State<'arg_input>> { + State::new_impl( + input, + trait_name, + trait_module, + trait_attr, + allowed_attr_params, + true, + ) + } + + pub fn with_type_bound<'arg_input>( + input: &'arg_input DeriveInput, + trait_name: &'static str, + trait_module: TokenStream, + trait_attr: String, + allowed_attr_params: AttrParams, + add_type_bound: bool, + ) -> Result<State<'arg_input>> { + Self::new_impl( + input, + trait_name, + trait_module, + trait_attr, + allowed_attr_params, + add_type_bound, + ) + } + + fn new_impl<'arg_input>( + input: &'arg_input DeriveInput, + trait_name: &'static str, + trait_module: TokenStream, + trait_attr: String, + allowed_attr_params: AttrParams, + add_type_bound: bool, + ) -> Result<State<'arg_input>> { + let trait_name = trait_name.trim_end_matches("ToInner"); + let trait_ident = Ident::new(trait_name, Span::call_site()); + let method_ident = Ident::new(&trait_attr, Span::call_site()); + let trait_path = quote!(#trait_module::#trait_ident); + let (derive_type, fields, variants): (_, Vec<_>, Vec<_>) = match input.data { + Data::Struct(ref data_struct) => match data_struct.fields { + Fields::Unnamed(ref fields) => { + (DeriveType::Unnamed, unnamed_to_vec(fields), vec![]) + } + + Fields::Named(ref fields) => { + (DeriveType::Named, named_to_vec(fields), vec![]) + } + Fields::Unit => (DeriveType::Named, vec![], vec![]), + }, + Data::Enum(ref data_enum) => ( + DeriveType::Enum, + vec![], + data_enum.variants.iter().collect(), + ), + Data::Union(_) => { + panic!("cannot derive({}) for union", trait_name) + } + }; + let attrs: Vec<_> = if derive_type == DeriveType::Enum { + variants.iter().map(|v| &v.attrs).collect() + } else { + fields.iter().map(|f| &f.attrs).collect() + }; + + let (allowed_attr_params_outer, allowed_attr_params_inner) = + if derive_type == DeriveType::Enum { + (&allowed_attr_params.enum_, &allowed_attr_params.variant) + } else { + (&allowed_attr_params.struct_, &allowed_attr_params.field) + }; + + let struct_meta_info = + get_meta_info(&trait_attr, &input.attrs, allowed_attr_params_outer)?; + let meta_infos: Result<Vec<_>> = attrs + .iter() + .map(|attrs| get_meta_info(&trait_attr, attrs, allowed_attr_params_inner)) + .collect(); + let meta_infos = meta_infos?; + let first_match = meta_infos + .iter() + .filter_map(|info| info.enabled.map(|_| info)) + .next(); + + // Default to enabled true, except when first attribute has explicit + // enabling. + // + // Except for derive Error. + // + // The way `else` case works is that if any field have any valid + // attribute specified, then all fields without any attributes + // specified are filtered out from `State::enabled_fields`. + // + // However, derive Error *infers* fields and there are cases when + // one of the fields may have an attribute specified, but another field + // would be inferred. So, for derive Error macro we default enabled + // to true unconditionally (i.e., even if some fields have attributes + // specified). + let default_enabled = if trait_name == "Error" { + true + } else { + first_match.map_or(true, |info| !info.enabled.unwrap()) + }; + + let defaults = struct_meta_info.into_full(FullMetaInfo { + enabled: default_enabled, + forward: false, + // Default to owned true, except when first attribute has one of owned, + // ref or ref_mut + // - not a single attribute means default true + // - an attribute, but non of owned, ref or ref_mut means default true + // - an attribute, and owned, ref or ref_mut means default false + owned: first_match.map_or(true, |info| { + info.owned.is_none() && info.ref_.is_none() || info.ref_mut.is_none() + }), + ref_: false, + ref_mut: false, + info: MetaInfo::default(), + }); + + let full_meta_infos: Vec<_> = meta_infos + .into_iter() + .map(|info| info.into_full(defaults.clone())) + .collect(); + + let variant_states: Result<Vec<_>> = if derive_type == DeriveType::Enum { + variants + .iter() + .zip(full_meta_infos.iter().cloned()) + .map(|(variant, info)| { + State::from_variant( + input, + trait_name, + trait_module.clone(), + trait_attr.clone(), + allowed_attr_params.clone(), + variant, + info, + ) + }) + .collect() + } else { + Ok(vec![]) + }; + + let generics = if add_type_bound { + add_extra_ty_param_bound(&input.generics, &trait_path) + } else { + input.generics.clone() + }; + + Ok(State { + input, + trait_name, + trait_ident, + method_ident, + trait_module, + trait_path, + trait_path_params: vec![], + trait_attr, + // input, + fields, + variants, + variant_states: variant_states?, + variant: None, + derive_type, + generics, + full_meta_infos, + default_info: defaults, + }) + } + + pub fn from_variant<'arg_input>( + input: &'arg_input DeriveInput, + trait_name: &'static str, + trait_module: TokenStream, + trait_attr: String, + allowed_attr_params: AttrParams, + variant: &'arg_input Variant, + default_info: FullMetaInfo, + ) -> Result<State<'arg_input>> { + let trait_name = trait_name.trim_end_matches("ToInner"); + let trait_ident = Ident::new(trait_name, Span::call_site()); + let method_ident = Ident::new(&trait_attr, Span::call_site()); + let trait_path = quote!(#trait_module::#trait_ident); + let (derive_type, fields): (_, Vec<_>) = match variant.fields { + Fields::Unnamed(ref fields) => { + (DeriveType::Unnamed, unnamed_to_vec(fields)) + } + + Fields::Named(ref fields) => (DeriveType::Named, named_to_vec(fields)), + Fields::Unit => (DeriveType::Named, vec![]), + }; + + let meta_infos: Result<Vec<_>> = fields + .iter() + .map(|f| &f.attrs) + .map(|attrs| get_meta_info(&trait_attr, attrs, &allowed_attr_params.field)) + .collect(); + let meta_infos = meta_infos?; + let full_meta_infos: Vec<_> = meta_infos + .into_iter() + .map(|info| info.into_full(default_info.clone())) + .collect(); + + let generics = add_extra_ty_param_bound(&input.generics, &trait_path); + + Ok(State { + input, + trait_name, + trait_module, + trait_path, + trait_path_params: vec![], + trait_attr, + trait_ident, + method_ident, + // input, + fields, + variants: vec![], + variant_states: vec![], + variant: Some(variant), + derive_type, + generics, + full_meta_infos, + default_info, + }) + } + pub fn add_trait_path_type_param(&mut self, param: TokenStream) { + self.trait_path_params.push(param); + } + + pub fn assert_single_enabled_field<'state>( + &'state self, + ) -> SingleFieldData<'input, 'state> { + if self.derive_type == DeriveType::Enum { + panic_one_field(self.trait_name, &self.trait_attr); + } + let data = self.enabled_fields_data(); + if data.fields.len() != 1 { + panic_one_field(self.trait_name, &self.trait_attr); + }; + SingleFieldData { + input_type: data.input_type, + field: data.fields[0], + field_type: data.field_types[0], + member: data.members[0].clone(), + info: data.infos[0].clone(), + field_ident: data.field_idents[0].clone(), + trait_path: data.trait_path, + trait_path_with_params: data.trait_path_with_params.clone(), + casted_trait: data.casted_traits[0].clone(), + impl_generics: data.impl_generics.clone(), + ty_generics: data.ty_generics.clone(), + where_clause: data.where_clause, + multi_field_data: data, + } + } + + pub fn enabled_fields_data<'state>(&'state self) -> MultiFieldData<'input, 'state> { + if self.derive_type == DeriveType::Enum { + panic!("cannot derive({}) for enum", self.trait_name) + } + let fields = self.enabled_fields(); + let field_idents = self.enabled_fields_idents(); + let field_indexes = self.enabled_fields_indexes(); + let field_types: Vec<_> = fields.iter().map(|f| &f.ty).collect(); + let members: Vec<_> = field_idents + .iter() + .map(|ident| quote!(self.#ident)) + .collect(); + let trait_path = &self.trait_path; + let trait_path_with_params = if !self.trait_path_params.is_empty() { + let params = self.trait_path_params.iter(); + quote!(#trait_path<#(#params),*>) + } else { + self.trait_path.clone() + }; + + let casted_traits: Vec<_> = field_types + .iter() + .map(|field_type| quote!(<#field_type as #trait_path_with_params>)) + .collect(); + let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl(); + let input_type = &self.input.ident; + let (variant_name, variant_type) = self.variant.map_or_else( + || (None, quote!(#input_type)), + |v| { + let variant_name = &v.ident; + (Some(variant_name), quote!(#input_type::#variant_name)) + }, + ); + MultiFieldData { + input_type, + variant_type, + variant_name, + variant_info: self.default_info.clone(), + fields, + field_types, + field_indexes, + members, + infos: self.enabled_infos(), + field_idents, + method_ident: &self.method_ident, + trait_path, + trait_path_with_params, + casted_traits, + impl_generics, + ty_generics, + where_clause, + state: self, + } + } + + pub fn enabled_variant_data<'state>( + &'state self, + ) -> MultiVariantData<'input, 'state> { + if self.derive_type != DeriveType::Enum { + panic!("can only derive({}) for enum", self.trait_name) + } + let variants = self.enabled_variants(); + let trait_path = &self.trait_path; + let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl(); + MultiVariantData { + input_type: &self.input.ident, + variants, + variant_states: self.enabled_variant_states(), + infos: self.enabled_infos(), + trait_path, + impl_generics, + ty_generics, + where_clause, + } + } + + fn enabled_variants(&self) -> Vec<&'input Variant> { + self.variants + .iter() + .zip(self.full_meta_infos.iter().map(|info| info.enabled)) + .filter(|(_, ig)| *ig) + .map(|(v, _)| *v) + .collect() + } + + fn enabled_variant_states(&self) -> Vec<&State<'input>> { + self.variant_states + .iter() + .zip(self.full_meta_infos.iter().map(|info| info.enabled)) + .filter(|(_, ig)| *ig) + .map(|(v, _)| v) + .collect() + } + + pub fn enabled_fields(&self) -> Vec<&'input Field> { + self.fields + .iter() + .zip(self.full_meta_infos.iter().map(|info| info.enabled)) + .filter(|(_, ig)| *ig) + .map(|(f, _)| *f) + .collect() + } + + fn field_idents(&self) -> Vec<TokenStream> { + if self.derive_type == DeriveType::Named { + self.fields + .iter() + .map(|f| { + f.ident + .as_ref() + .expect("Tried to get field names of a tuple struct") + .to_token_stream() + }) + .collect() + } else { + let count = self.fields.len(); + (0..count) + .map(|i| Index::from(i).to_token_stream()) + .collect() + } + } + + fn enabled_fields_idents(&self) -> Vec<TokenStream> { + self.field_idents() + .into_iter() + .zip(self.full_meta_infos.iter().map(|info| info.enabled)) + .filter(|(_, ig)| *ig) + .map(|(f, _)| f) + .collect() + } + + fn enabled_fields_indexes(&self) -> Vec<usize> { + self.full_meta_infos + .iter() + .map(|info| info.enabled) + .enumerate() + .filter(|(_, ig)| *ig) + .map(|(i, _)| i) + .collect() + } + fn enabled_infos(&self) -> Vec<FullMetaInfo> { + self.full_meta_infos + .iter() + .filter(|info| info.enabled) + .cloned() + .collect() + } +} + +#[derive(Clone)] +pub struct SingleFieldData<'input, 'state> { + pub input_type: &'input Ident, + pub field: &'input Field, + pub field_type: &'input Type, + pub field_ident: TokenStream, + pub member: TokenStream, + pub info: FullMetaInfo, + pub trait_path: &'state TokenStream, + pub trait_path_with_params: TokenStream, + pub casted_trait: TokenStream, + pub impl_generics: ImplGenerics<'state>, + pub ty_generics: TypeGenerics<'state>, + pub where_clause: Option<&'state WhereClause>, + multi_field_data: MultiFieldData<'input, 'state>, +} + +#[derive(Clone)] +pub struct MultiFieldData<'input, 'state> { + pub input_type: &'input Ident, + pub variant_type: TokenStream, + pub variant_name: Option<&'input Ident>, + pub variant_info: FullMetaInfo, + pub fields: Vec<&'input Field>, + pub field_types: Vec<&'input Type>, + pub field_idents: Vec<TokenStream>, + pub field_indexes: Vec<usize>, + pub members: Vec<TokenStream>, + pub infos: Vec<FullMetaInfo>, + pub method_ident: &'state Ident, + pub trait_path: &'state TokenStream, + pub trait_path_with_params: TokenStream, + pub casted_traits: Vec<TokenStream>, + pub impl_generics: ImplGenerics<'state>, + pub ty_generics: TypeGenerics<'state>, + pub where_clause: Option<&'state WhereClause>, + pub state: &'state State<'input>, +} + +pub struct MultiVariantData<'input, 'state> { + pub input_type: &'input Ident, + pub variants: Vec<&'input Variant>, + pub variant_states: Vec<&'state State<'input>>, + pub infos: Vec<FullMetaInfo>, + pub trait_path: &'state TokenStream, + pub impl_generics: ImplGenerics<'state>, + pub ty_generics: TypeGenerics<'state>, + pub where_clause: Option<&'state WhereClause>, +} + +impl<'input, 'state> MultiFieldData<'input, 'state> { + pub fn initializer<T: ToTokens>(&self, initializers: &[T]) -> TokenStream { + let MultiFieldData { + variant_type, + field_idents, + .. + } = self; + if self.state.derive_type == DeriveType::Named { + quote!(#variant_type{#(#field_idents: #initializers),*}) + } else { + quote!(#variant_type(#(#initializers),*)) + } + } + pub fn matcher<T: ToTokens>( + &self, + indexes: &[usize], + bindings: &[T], + ) -> TokenStream { + let MultiFieldData { variant_type, .. } = self; + let full_bindings = (0..self.state.fields.len()).map(|i| { + indexes.iter().position(|index| i == *index).map_or_else( + || quote!(_), + |found_index| bindings[found_index].to_token_stream(), + ) + }); + if self.state.derive_type == DeriveType::Named { + let field_idents = self.state.field_idents(); + quote!(#variant_type{#(#field_idents: #full_bindings),*}) + } else { + quote!(#variant_type(#(#full_bindings),*)) + } + } +} + +impl<'input, 'state> SingleFieldData<'input, 'state> { + pub fn initializer<T: ToTokens>(&self, initializers: &[T]) -> TokenStream { + self.multi_field_data.initializer(initializers) + } +} + +fn get_meta_info( + trait_attr: &str, + attrs: &[Attribute], + allowed_attr_params: &[&str], +) -> Result<MetaInfo> { + let mut it = attrs + .iter() + .filter_map(|m| m.parse_meta().ok()) + .filter(|m| { + m.path() + .segments + .first() + .map(|p| p.ident == trait_attr) + .unwrap_or_default() + }); + + let mut info = MetaInfo::default(); + + let meta = if let Some(meta) = it.next() { + meta + } else { + return Ok(info); + }; + + if allowed_attr_params.is_empty() { + return Err(Error::new(meta.span(), "Attribute is not allowed here")); + } + + info.enabled = Some(true); + + if let Some(another_meta) = it.next() { + return Err(Error::new( + another_meta.span(), + "Only a single attribute is allowed", + )); + } + + let list = match meta.clone() { + Meta::Path(_) => { + if allowed_attr_params.contains(&"ignore") { + return Ok(info); + } else { + return Err(Error::new( + meta.span(), + format!( + "Empty attribute is not allowed, add one of the following parameters: {}", + allowed_attr_params.join(", "), + ), + )); + } + } + Meta::List(list) => list, + Meta::NameValue(val) => { + return Err(Error::new( + val.span(), + "Attribute doesn't support name-value format here", + )); + } + }; + + parse_punctuated_nested_meta(&mut info, &list.nested, allowed_attr_params, None)?; + + Ok(info) +} + +fn parse_punctuated_nested_meta( + info: &mut MetaInfo, + meta: &Punctuated<NestedMeta, Token![,]>, + allowed_attr_params: &[&str], + wrapper_name: Option<&str>, +) -> Result<()> { + for meta in meta.iter() { + let meta = match meta { + NestedMeta::Meta(meta) => meta, + NestedMeta::Lit(lit) => { + return Err(Error::new( + lit.span(), + "Attribute doesn't support literals here", + )) + } + }; + + match meta { + Meta::List(list) if list.path.is_ident("not") => { + if wrapper_name.is_some() { + // Only single top-level `not` attribute is allowed. + return Err(Error::new( + list.span(), + "Attribute doesn't support multiple multiple or nested `not` parameters", + )); + } + parse_punctuated_nested_meta( + info, + &list.nested, + allowed_attr_params, + Some("not"), + )?; + } + + Meta::List(list) => { + let path = &list.path; + if !allowed_attr_params.iter().any(|param| path.is_ident(param)) { + return Err(Error::new( + meta.span(), + format!( + "Attribute nested parameter not supported. \ + Supported attribute parameters are: {}", + allowed_attr_params.join(", "), + ), + )); + } + + let mut parse_nested = true; + + let attr_name = path.get_ident().unwrap().to_string(); + match (wrapper_name, attr_name.as_str()) { + (None, "owned") => info.owned = Some(true), + (None, "ref") => info.ref_ = Some(true), + (None, "ref_mut") => info.ref_mut = Some(true), + + #[cfg(any(feature = "from", feature = "into"))] + (None, "types") + | (Some("owned"), "types") + | (Some("ref"), "types") + | (Some("ref_mut"), "types") => { + parse_nested = false; + for meta in &list.nested { + let typ: syn::Type = match meta { + NestedMeta::Meta(meta) => { + let path = if let Meta::Path(p) = meta { + p + } else { + return Err(Error::new( + meta.span(), + format!( + "Attribute doesn't support type {}", + quote! { #meta }, + ), + )); + }; + syn::TypePath { + qself: None, + path: path.clone(), + } + .into() + } + NestedMeta::Lit(syn::Lit::Str(s)) => s.parse()?, + NestedMeta::Lit(lit) => return Err(Error::new( + lit.span(), + "Attribute doesn't support nested literals here", + )), + }; + + for ref_type in wrapper_name + .map(|n| vec![RefType::from_attr_name(n)]) + .unwrap_or_else(|| { + vec![RefType::No, RefType::Ref, RefType::Mut] + }) + { + if info + .types + .entry(ref_type) + .or_default() + .replace(typ.clone()) + .is_some() + { + return Err(Error::new( + typ.span(), + format!( + "Duplicate type `{}` specified", + quote! { #path }, + ), + )); + } + } + } + } + + _ => { + return Err(Error::new( + list.span(), + format!( + "Attribute doesn't support nested parameter `{}` here", + quote! { #path }, + ), + )) + } + }; + + if parse_nested { + parse_punctuated_nested_meta( + info, + &list.nested, + allowed_attr_params, + Some(&attr_name), + )?; + } + } + + Meta::Path(path) => { + if !allowed_attr_params.iter().any(|param| path.is_ident(param)) { + return Err(Error::new( + meta.span(), + format!( + "Attribute parameter not supported. \ + Supported attribute parameters are: {}", + allowed_attr_params.join(", "), + ), + )); + } + + let attr_name = path.get_ident().unwrap().to_string(); + match (wrapper_name, attr_name.as_str()) { + (None, "ignore") => info.enabled = Some(false), + (None, "forward") => info.forward = Some(true), + (Some("not"), "forward") => info.forward = Some(false), + (None, "owned") => info.owned = Some(true), + (None, "ref") => info.ref_ = Some(true), + (None, "ref_mut") => info.ref_mut = Some(true), + (None, "source") => info.source = Some(true), + (Some("not"), "source") => info.source = Some(false), + (None, "backtrace") => info.backtrace = Some(true), + (Some("not"), "backtrace") => info.backtrace = Some(false), + _ => { + return Err(Error::new( + path.span(), + format!( + "Attribute doesn't support parameter `{}` here", + quote! { #path } + ), + )) + } + } + } + + Meta::NameValue(val) => { + return Err(Error::new( + val.span(), + "Attribute doesn't support name-value parameters here", + )) + } + } + } + + Ok(()) +} + +#[derive(Clone, Debug, Default)] +pub struct FullMetaInfo { + pub enabled: bool, + pub forward: bool, + pub owned: bool, + pub ref_: bool, + pub ref_mut: bool, + pub info: MetaInfo, +} + +#[derive(Clone, Debug, Default)] +pub struct MetaInfo { + pub enabled: Option<bool>, + pub forward: Option<bool>, + pub owned: Option<bool>, + pub ref_: Option<bool>, + pub ref_mut: Option<bool>, + pub source: Option<bool>, + pub backtrace: Option<bool>, + #[cfg(any(feature = "from", feature = "into"))] + pub types: HashMap<RefType, HashSet<syn::Type>>, +} + +impl MetaInfo { + fn into_full(self, defaults: FullMetaInfo) -> FullMetaInfo { + FullMetaInfo { + enabled: self.enabled.unwrap_or(defaults.enabled), + forward: self.forward.unwrap_or(defaults.forward), + owned: self.owned.unwrap_or(defaults.owned), + ref_: self.ref_.unwrap_or(defaults.ref_), + ref_mut: self.ref_mut.unwrap_or(defaults.ref_mut), + info: self, + } + } +} + +impl FullMetaInfo { + pub fn ref_types(&self) -> Vec<RefType> { + let mut ref_types = vec![]; + if self.owned { + ref_types.push(RefType::No); + } + if self.ref_ { + ref_types.push(RefType::Ref); + } + if self.ref_mut { + ref_types.push(RefType::Mut); + } + ref_types + } + + #[cfg(any(feature = "from", feature = "into"))] + pub fn additional_types(&self, ref_type: RefType) -> HashSet<syn::Type> { + self.info.types.get(&ref_type).cloned().unwrap_or_default() + } +} + +pub fn get_if_type_parameter_used_in_type( + type_parameters: &HashSet<syn::Ident>, + ty: &syn::Type, +) -> Option<syn::Type> { + if is_type_parameter_used_in_type(type_parameters, ty) { + match ty { + syn::Type::Reference(syn::TypeReference { elem: ty, .. }) => { + Some((&**ty).clone()) + } + ty => Some(ty.clone()), + } + } else { + None + } +} + +pub fn is_type_parameter_used_in_type( + type_parameters: &HashSet<syn::Ident>, + ty: &syn::Type, +) -> bool { + match ty { + syn::Type::Path(ty) => { + if let Some(qself) = &ty.qself { + if is_type_parameter_used_in_type(type_parameters, &qself.ty) { + return true; + } + } + + if let Some(segment) = ty.path.segments.first() { + if type_parameters.contains(&segment.ident) { + return true; + } + } + + ty.path.segments.iter().any(|segment| { + if let syn::PathArguments::AngleBracketed(arguments) = + &segment.arguments + { + arguments.args.iter().any(|argument| match argument { + syn::GenericArgument::Type(ty) => { + is_type_parameter_used_in_type(type_parameters, ty) + } + syn::GenericArgument::Constraint(constraint) => { + type_parameters.contains(&constraint.ident) + } + _ => false, + }) + } else { + false + } + }) + } + + syn::Type::Reference(ty) => { + is_type_parameter_used_in_type(type_parameters, &ty.elem) + } + + _ => false, + } +} |