1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
use proc_macro2;
use ast;
use attr;
use matcher;
use syn;
use utils;
/// Derive `Copy` for `input`.
pub fn derive_copy(input: &ast::Input) -> proc_macro2::TokenStream {
let name = &input.ident;
let copy_trait_path = copy_trait_path();
let generics = utils::build_impl_generics(
input,
©_trait_path,
|attrs| attrs.copy_bound().is_none(),
|field| field.copy_bound(),
|input| input.copy_bound(),
);
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
quote! {
#[allow(unused_qualifications)]
impl #impl_generics #copy_trait_path for #name #ty_generics #where_clause {}
}
}
/// Derive `Clone` for `input`.
pub fn derive_clone(input: &ast::Input) -> proc_macro2::TokenStream {
let name = &input.ident;
let clone_trait_path = clone_trait_path();
let generics = utils::build_impl_generics(
input,
&clone_trait_path,
needs_clone_bound,
|field| field.clone_bound(),
|input| input.clone_bound(),
);
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let is_copy = input.attrs.copy.is_some();
if is_copy && input.generics.type_params().count() == 0 {
quote! {
#[allow(unused_qualifications)]
impl #impl_generics #clone_trait_path for #name #ty_generics #where_clause {
fn clone(&self) -> Self {
*self
}
}
}
} else {
let body = matcher::Matcher::new(matcher::BindingStyle::Ref, input.attrs.is_packed).build_arms(
input,
"__arg",
|arm_path, _, _, style, _, bis| {
let field_clones = bis.iter().map(|bi| {
let arg = &bi.expr;
let clone = if let Some(clone_with) = bi.field.attrs.clone_with() {
quote!(#clone_with(&#arg))
} else {
quote!(#arg.clone())
};
if let Some(ref name) = bi.field.ident {
quote! {
#name: #clone
}
} else {
clone
}
});
match style {
ast::Style::Struct => {
quote! {
#arm_path {
#(#field_clones),*
}
}
}
ast::Style::Tuple => {
quote! {
#arm_path (#(#field_clones),*)
}
}
ast::Style::Unit => {
quote! {
#arm_path
}
}
}
},
);
let clone_from = if input.attrs.clone_from() {
Some(
matcher::Matcher::new(matcher::BindingStyle::RefMut, input.attrs.is_packed).build_arms(
input,
"__arg",
|outer_arm_path, _, _, _, _, outer_bis| {
let body = matcher::Matcher::new(matcher::BindingStyle::Ref, input.attrs.is_packed).build_arms(
input,
"__other",
|inner_arm_path, _, _, _, _, inner_bis| {
if outer_arm_path == inner_arm_path {
let field_clones = outer_bis.iter().zip(inner_bis).map(
|(outer_bi, inner_bi)| {
let outer = &outer_bi.expr;
let inner = &inner_bi.expr;
quote!(#outer.clone_from(&#inner);)
},
);
quote! {
#(#field_clones)*
return;
}
} else {
quote!()
}
},
);
quote! {
match *other {
#body
}
}
},
),
)
} else {
None
};
let clone_from = clone_from.map(|body| {
// Enumerations are only cloned-from if both variants are the same.
// If they are different, fallback to normal cloning.
let fallback = if let ast::Body::Enum(_) = input.body {
Some(quote!(*self = other.clone();))
} else {
None
};
quote! {
#[allow(clippy::needless_return)]
fn clone_from(&mut self, other: &Self) {
match *self {
#body
}
#fallback
}
}
});
quote! {
#[allow(unused_qualifications)]
impl #impl_generics #clone_trait_path for #name #ty_generics #where_clause {
fn clone(&self) -> Self {
match *self {
#body
}
}
#clone_from
}
}
}
}
fn needs_clone_bound(attrs: &attr::Field) -> bool {
attrs.clone_bound().is_none()
}
/// Return the path of the `Clone` trait, that is `::std::clone::Clone`.
fn clone_trait_path() -> syn::Path {
if cfg!(feature = "use_core") {
parse_quote!(::core::clone::Clone)
} else {
parse_quote!(::std::clone::Clone)
}
}
/// Return the path of the `Copy` trait, that is `::std::marker::Copy`.
fn copy_trait_path() -> syn::Path {
if cfg!(feature = "use_core") {
parse_quote!(::core::marker::Copy)
} else {
parse_quote!(::std::marker::Copy)
}
}