| 1 | #![cfg_attr ( |
| 2 | not(all(feature = "add" , feature = "mul" )), |
| 3 | allow(dead_code, unused_mut) |
| 4 | )] |
| 5 | |
| 6 | use proc_macro2::TokenStream; |
| 7 | use quote::{format_ident, quote, ToTokens}; |
| 8 | use syn::{ |
| 9 | parse_quote, punctuated::Punctuated, spanned::Spanned, Attribute, Data, |
| 10 | DeriveInput, Error, Field, Fields, FieldsNamed, FieldsUnnamed, GenericParam, |
| 11 | Generics, Ident, ImplGenerics, Index, Result, Token, Type, TypeGenerics, |
| 12 | TypeParamBound, Variant, WhereClause, |
| 13 | }; |
| 14 | |
| 15 | #[cfg (any( |
| 16 | feature = "as_ref" , |
| 17 | feature = "debug" , |
| 18 | feature = "display" , |
| 19 | feature = "from" , |
| 20 | feature = "into" , |
| 21 | feature = "try_from" , |
| 22 | ))] |
| 23 | pub(crate) use self::either::Either; |
| 24 | #[cfg (any(feature = "from" , feature = "into" ))] |
| 25 | pub(crate) use self::fields_ext::FieldsExt; |
| 26 | #[cfg (feature = "as_ref" )] |
| 27 | pub(crate) use self::generics_search::GenericsSearch; |
| 28 | #[cfg (any( |
| 29 | feature = "as_ref" , |
| 30 | feature = "debug" , |
| 31 | feature = "display" , |
| 32 | feature = "from" , |
| 33 | feature = "into" , |
| 34 | feature = "try_from" , |
| 35 | ))] |
| 36 | pub(crate) use self::spanning::Spanning; |
| 37 | |
| 38 | #[derive (Clone, Copy, Default)] |
| 39 | pub struct DeterministicState; |
| 40 | |
| 41 | impl std::hash::BuildHasher for DeterministicState { |
| 42 | type Hasher = std::collections::hash_map::DefaultHasher; |
| 43 | |
| 44 | fn build_hasher(&self) -> Self::Hasher { |
| 45 | Self::Hasher::default() |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub type HashMap<K, V> = std::collections::HashMap<K, V, DeterministicState>; |
| 50 | pub type HashSet<K> = std::collections::HashSet<K, DeterministicState>; |
| 51 | |
| 52 | #[derive (Clone, Copy, Debug, Eq, PartialEq, Hash)] |
| 53 | pub enum RefType { |
| 54 | No, |
| 55 | Ref, |
| 56 | Mut, |
| 57 | } |
| 58 | |
| 59 | impl RefType { |
| 60 | pub fn lifetime(self) -> TokenStream { |
| 61 | match self { |
| 62 | RefType::No => quote! {}, |
| 63 | _ => quote! { '__deriveMoreLifetime }, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | pub fn reference(self) -> TokenStream { |
| 68 | match self { |
| 69 | RefType::No => quote! {}, |
| 70 | RefType::Ref => quote! { & }, |
| 71 | RefType::Mut => quote! { &mut }, |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | pub fn mutability(self) -> TokenStream { |
| 76 | match self { |
| 77 | RefType::Mut => quote! { mut }, |
| 78 | _ => quote! {}, |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | pub fn pattern_ref(self) -> TokenStream { |
| 83 | match self { |
| 84 | RefType::Ref => quote! { ref }, |
| 85 | RefType::Mut => quote! { ref mut }, |
| 86 | RefType::No => quote! {}, |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | pub fn reference_with_lifetime(self) -> TokenStream { |
| 91 | if !self.is_ref() { |
| 92 | return quote! {}; |
| 93 | } |
| 94 | let lifetime = self.lifetime(); |
| 95 | let mutability = self.mutability(); |
| 96 | quote! { &#lifetime #mutability } |
| 97 | } |
| 98 | |
| 99 | pub fn is_ref(self) -> bool { |
| 100 | !matches!(self, RefType::No) |
| 101 | } |
| 102 | |
| 103 | pub fn from_attr_name(name: &str) -> Self { |
| 104 | match name { |
| 105 | "owned" => RefType::No, |
| 106 | "ref" => RefType::Ref, |
| 107 | "ref_mut" => RefType::Mut, |
| 108 | _ => panic!("` {name}` is not a `RefType`" ), |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | pub fn numbered_vars(count: usize, prefix: &str) -> Vec<Ident> { |
| 114 | (0..count).map(|i: usize| format_ident!("__ {prefix}{i}" )).collect() |
| 115 | } |
| 116 | |
| 117 | pub fn field_idents<'a>(fields: &'a [&'a Field]) -> Vec<&'a Ident> { |
| 118 | fieldsimpl Iterator |
| 119 | .iter() |
| 120 | .map(|f: &&Field| { |
| 121 | f.ident |
| 122 | .as_ref() |
| 123 | .expect(msg:"Tried to get field names of a tuple struct" ) |
| 124 | }) |
| 125 | .collect() |
| 126 | } |
| 127 | |
| 128 | pub fn get_field_types_iter<'a>( |
| 129 | fields: &'a [&'a Field], |
| 130 | ) -> Box<dyn Iterator<Item = &'a Type> + 'a> { |
| 131 | Box::new(fields.iter().map(|f: &&Field| &f.ty)) |
| 132 | } |
| 133 | |
| 134 | pub fn get_field_types<'a>(fields: &'a [&'a Field]) -> Vec<&'a Type> { |
| 135 | get_field_types_iter(fields).collect() |
| 136 | } |
| 137 | |
| 138 | pub fn add_extra_type_param_bound_op_output<'a>( |
| 139 | generics: &'a Generics, |
| 140 | trait_ident: &'a Ident, |
| 141 | ) -> Generics { |
| 142 | let mut generics: Generics = generics.clone(); |
| 143 | for type_param: &mut TypeParam in &mut generics.type_params_mut() { |
| 144 | let type_ident: &Ident = &type_param.ident; |
| 145 | let bound: TypeParamBound = parse_quote! { |
| 146 | derive_more::core::ops::#trait_ident<Output = #type_ident> |
| 147 | }; |
| 148 | type_param.bounds.push(bound) |
| 149 | } |
| 150 | |
| 151 | generics |
| 152 | } |
| 153 | |
| 154 | pub fn add_extra_ty_param_bound_op<'a>( |
| 155 | generics: &'a Generics, |
| 156 | trait_ident: &'a Ident, |
| 157 | ) -> Generics { |
| 158 | add_extra_ty_param_bound(generics, "e! { derive_more::core::ops::#trait_ident }) |
| 159 | } |
| 160 | |
| 161 | pub fn add_extra_ty_param_bound<'a>( |
| 162 | generics: &'a Generics, |
| 163 | bound: &'a TokenStream, |
| 164 | ) -> Generics { |
| 165 | let mut generics: Generics = generics.clone(); |
| 166 | let bound: TypeParamBound = parse_quote! { #bound }; |
| 167 | for type_param: &mut TypeParam in &mut generics.type_params_mut() { |
| 168 | type_param.bounds.push(bound.clone()) |
| 169 | } |
| 170 | |
| 171 | generics |
| 172 | } |
| 173 | |
| 174 | pub fn add_extra_generic_param( |
| 175 | generics: &Generics, |
| 176 | generic_param: TokenStream, |
| 177 | ) -> Generics { |
| 178 | let generic_param: GenericParam = parse_quote! { #generic_param }; |
| 179 | let mut generics: Generics = generics.clone(); |
| 180 | generics.params.push(generic_param); |
| 181 | |
| 182 | generics |
| 183 | } |
| 184 | |
| 185 | pub fn add_extra_generic_type_param( |
| 186 | generics: &Generics, |
| 187 | generic_param: TokenStream, |
| 188 | ) -> Generics { |
| 189 | let generic_param: GenericParam = parse_quote! { #generic_param }; |
| 190 | let lifetimes: Vec<GenericParam> = |
| 191 | generics.lifetimes().map(|x: &LifetimeParam| x.clone().into()).collect(); |
| 192 | let type_params: Vec<GenericParam> = |
| 193 | generics.type_params().map(|x: &TypeParam| x.clone().into()).collect(); |
| 194 | let const_params: Vec<GenericParam> = |
| 195 | generics.const_params().map(|x: &ConstParam| x.clone().into()).collect(); |
| 196 | let mut generics: Generics = generics.clone(); |
| 197 | generics.params = Default::default(); |
| 198 | generics.params.extend(iter:lifetimes); |
| 199 | generics.params.extend(iter:type_params); |
| 200 | generics.params.push(generic_param); |
| 201 | generics.params.extend(iter:const_params); |
| 202 | |
| 203 | generics |
| 204 | } |
| 205 | |
| 206 | pub fn add_extra_where_clauses( |
| 207 | generics: &Generics, |
| 208 | type_where_clauses: TokenStream, |
| 209 | ) -> Generics { |
| 210 | let mut type_where_clauses: WhereClause = parse_quote! { #type_where_clauses }; |
| 211 | let mut new_generics: Generics = generics.clone(); |
| 212 | if let Some(old_where: WhereClause) = new_generics.where_clause { |
| 213 | type_where_clauses.predicates.extend(iter:old_where.predicates) |
| 214 | } |
| 215 | new_generics.where_clause = Some(type_where_clauses); |
| 216 | |
| 217 | new_generics |
| 218 | } |
| 219 | |
| 220 | pub fn add_where_clauses_for_new_ident<'a>( |
| 221 | generics: &'a Generics, |
| 222 | fields: &[&'a Field], |
| 223 | type_ident: &Ident, |
| 224 | type_where_clauses: TokenStream, |
| 225 | sized: bool, |
| 226 | ) -> Generics { |
| 227 | let generic_param: TokenStream = if fields.len() > 1 { |
| 228 | quote! { #type_ident: derive_more::core::marker::Copy } |
| 229 | } else if sized { |
| 230 | quote! { #type_ident } |
| 231 | } else { |
| 232 | quote! { #type_ident: ?derive_more::core::marker::Sized } |
| 233 | }; |
| 234 | |
| 235 | let generics: Generics = add_extra_where_clauses(generics, type_where_clauses); |
| 236 | add_extra_generic_type_param(&generics, generic_param) |
| 237 | } |
| 238 | |
| 239 | pub fn unnamed_to_vec(fields: &FieldsUnnamed) -> Vec<&Field> { |
| 240 | fields.unnamed.iter().collect() |
| 241 | } |
| 242 | |
| 243 | pub fn named_to_vec(fields: &FieldsNamed) -> Vec<&Field> { |
| 244 | fields.named.iter().collect() |
| 245 | } |
| 246 | |
| 247 | fn panic_one_field(trait_name: &str, trait_attr: &str) -> ! { |
| 248 | panic!( |
| 249 | "derive( {trait_name}) only works when forwarding to a single field. \ |
| 250 | Try putting #[ {trait_attr}] or #[ {trait_attr}(ignore)] on the fields in the struct" , |
| 251 | ) |
| 252 | } |
| 253 | |
| 254 | #[derive (Copy, Clone, Debug, PartialEq, Eq)] |
| 255 | pub enum DeriveType { |
| 256 | Unnamed, |
| 257 | Named, |
| 258 | Enum, |
| 259 | } |
| 260 | |
| 261 | pub struct State<'input> { |
| 262 | pub input: &'input DeriveInput, |
| 263 | pub trait_name: &'static str, |
| 264 | pub method_ident: Ident, |
| 265 | pub trait_path: TokenStream, |
| 266 | pub trait_path_params: Vec<TokenStream>, |
| 267 | pub trait_attr: String, |
| 268 | pub derive_type: DeriveType, |
| 269 | pub fields: Vec<&'input Field>, |
| 270 | pub variants: Vec<&'input Variant>, |
| 271 | pub variant_states: Vec<State<'input>>, |
| 272 | pub variant: Option<&'input Variant>, |
| 273 | pub generics: Generics, |
| 274 | pub default_info: FullMetaInfo, |
| 275 | full_meta_infos: Vec<FullMetaInfo>, |
| 276 | } |
| 277 | |
| 278 | #[derive (Default, Clone)] |
| 279 | pub struct AttrParams { |
| 280 | pub enum_: Vec<&'static str>, |
| 281 | pub variant: Vec<&'static str>, |
| 282 | pub struct_: Vec<&'static str>, |
| 283 | pub field: Vec<&'static str>, |
| 284 | } |
| 285 | |
| 286 | impl AttrParams { |
| 287 | pub fn new(params: Vec<&'static str>) -> AttrParams { |
| 288 | AttrParams { |
| 289 | enum_: params.clone(), |
| 290 | struct_: params.clone(), |
| 291 | variant: params.clone(), |
| 292 | field: params, |
| 293 | } |
| 294 | } |
| 295 | pub fn struct_(params: Vec<&'static str>) -> AttrParams { |
| 296 | AttrParams { |
| 297 | enum_: vec![], |
| 298 | struct_: params, |
| 299 | variant: vec![], |
| 300 | field: vec![], |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | impl<'input> State<'input> { |
| 306 | pub fn new<'arg_input>( |
| 307 | input: &'arg_input DeriveInput, |
| 308 | trait_name: &'static str, |
| 309 | trait_attr: String, |
| 310 | ) -> Result<State<'arg_input>> { |
| 311 | State::new_impl(input, trait_name, trait_attr, AttrParams::default(), true) |
| 312 | } |
| 313 | |
| 314 | pub fn with_field_ignore<'arg_input>( |
| 315 | input: &'arg_input DeriveInput, |
| 316 | trait_name: &'static str, |
| 317 | trait_attr: String, |
| 318 | ) -> Result<State<'arg_input>> { |
| 319 | State::new_impl( |
| 320 | input, |
| 321 | trait_name, |
| 322 | trait_attr, |
| 323 | AttrParams::new(vec!["ignore" ]), |
| 324 | true, |
| 325 | ) |
| 326 | } |
| 327 | |
| 328 | pub fn with_field_ignore_and_forward<'arg_input>( |
| 329 | input: &'arg_input DeriveInput, |
| 330 | trait_name: &'static str, |
| 331 | trait_attr: String, |
| 332 | ) -> Result<State<'arg_input>> { |
| 333 | State::new_impl( |
| 334 | input, |
| 335 | trait_name, |
| 336 | trait_attr, |
| 337 | AttrParams::new(vec!["ignore" , "forward" ]), |
| 338 | true, |
| 339 | ) |
| 340 | } |
| 341 | |
| 342 | pub fn with_field_ignore_and_refs<'arg_input>( |
| 343 | input: &'arg_input DeriveInput, |
| 344 | trait_name: &'static str, |
| 345 | trait_attr: String, |
| 346 | ) -> Result<State<'arg_input>> { |
| 347 | State::new_impl( |
| 348 | input, |
| 349 | trait_name, |
| 350 | trait_attr, |
| 351 | AttrParams::new(vec!["ignore" , "owned" , "ref" , "ref_mut" ]), |
| 352 | true, |
| 353 | ) |
| 354 | } |
| 355 | |
| 356 | pub fn with_attr_params<'arg_input>( |
| 357 | input: &'arg_input DeriveInput, |
| 358 | trait_name: &'static str, |
| 359 | trait_attr: String, |
| 360 | allowed_attr_params: AttrParams, |
| 361 | ) -> Result<State<'arg_input>> { |
| 362 | State::new_impl(input, trait_name, trait_attr, allowed_attr_params, true) |
| 363 | } |
| 364 | |
| 365 | fn new_impl<'arg_input>( |
| 366 | input: &'arg_input DeriveInput, |
| 367 | trait_name: &'static str, |
| 368 | trait_attr: String, |
| 369 | allowed_attr_params: AttrParams, |
| 370 | add_type_bound: bool, |
| 371 | ) -> Result<State<'arg_input>> { |
| 372 | let trait_name = trait_name.trim_end_matches("ToInner" ); |
| 373 | let trait_ident = format_ident!(" {trait_name}" ); |
| 374 | let method_ident = format_ident!(" {trait_attr}" ); |
| 375 | let trait_path = quote! { derive_more::with_trait::#trait_ident }; |
| 376 | let (derive_type, fields, variants): (_, Vec<_>, Vec<_>) = match input.data { |
| 377 | Data::Struct(ref data_struct) => match data_struct.fields { |
| 378 | Fields::Unnamed(ref fields) => { |
| 379 | (DeriveType::Unnamed, unnamed_to_vec(fields), vec![]) |
| 380 | } |
| 381 | |
| 382 | Fields::Named(ref fields) => { |
| 383 | (DeriveType::Named, named_to_vec(fields), vec![]) |
| 384 | } |
| 385 | Fields::Unit => (DeriveType::Named, vec![], vec![]), |
| 386 | }, |
| 387 | Data::Enum(ref data_enum) => ( |
| 388 | DeriveType::Enum, |
| 389 | vec![], |
| 390 | data_enum.variants.iter().collect(), |
| 391 | ), |
| 392 | Data::Union(_) => { |
| 393 | panic!("cannot derive( {trait_name}) for union" ) |
| 394 | } |
| 395 | }; |
| 396 | let attrs: Vec<_> = if derive_type == DeriveType::Enum { |
| 397 | variants.iter().map(|v| &v.attrs).collect() |
| 398 | } else { |
| 399 | fields.iter().map(|f| &f.attrs).collect() |
| 400 | }; |
| 401 | |
| 402 | let (allowed_attr_params_outer, allowed_attr_params_inner) = |
| 403 | if derive_type == DeriveType::Enum { |
| 404 | (&allowed_attr_params.enum_, &allowed_attr_params.variant) |
| 405 | } else { |
| 406 | (&allowed_attr_params.struct_, &allowed_attr_params.field) |
| 407 | }; |
| 408 | |
| 409 | let struct_meta_info = |
| 410 | get_meta_info(&trait_attr, &input.attrs, allowed_attr_params_outer)?; |
| 411 | let meta_infos: Result<Vec<_>> = attrs |
| 412 | .iter() |
| 413 | .map(|attrs| get_meta_info(&trait_attr, attrs, allowed_attr_params_inner)) |
| 414 | .collect(); |
| 415 | let meta_infos = meta_infos?; |
| 416 | let first_match = meta_infos |
| 417 | .iter() |
| 418 | .find_map(|info| info.enabled.map(|_| info)); |
| 419 | |
| 420 | // Default to enabled true, except when first attribute has explicit |
| 421 | // enabling. |
| 422 | // |
| 423 | // Except for derive Error. |
| 424 | // |
| 425 | // The way `else` case works is that if any field have any valid |
| 426 | // attribute specified, then all fields without any attributes |
| 427 | // specified are filtered out from `State::enabled_fields`. |
| 428 | // |
| 429 | // However, derive Error *infers* fields and there are cases when |
| 430 | // one of the fields may have an attribute specified, but another field |
| 431 | // would be inferred. So, for derive Error macro we default enabled |
| 432 | // to true unconditionally (i.e., even if some fields have attributes |
| 433 | // specified). |
| 434 | let default_enabled = if trait_name == "Error" { |
| 435 | true |
| 436 | } else { |
| 437 | first_match.map_or(true, |info| !info.enabled.unwrap()) |
| 438 | }; |
| 439 | |
| 440 | let defaults = struct_meta_info.into_full(FullMetaInfo { |
| 441 | enabled: default_enabled, |
| 442 | forward: false, |
| 443 | // Default to owned true, except when first attribute has one of owned, |
| 444 | // ref or ref_mut |
| 445 | // - not a single attribute means default true |
| 446 | // - an attribute, but non of owned, ref or ref_mut means default true |
| 447 | // - an attribute, and owned, ref or ref_mut means default false |
| 448 | owned: first_match.map_or(true, |info| { |
| 449 | info.owned.is_none() && info.ref_.is_none() || info.ref_mut.is_none() |
| 450 | }), |
| 451 | ref_: false, |
| 452 | ref_mut: false, |
| 453 | info: MetaInfo::default(), |
| 454 | }); |
| 455 | |
| 456 | let full_meta_infos: Vec<_> = meta_infos |
| 457 | .into_iter() |
| 458 | .map(|info| info.into_full(defaults.clone())) |
| 459 | .collect(); |
| 460 | |
| 461 | let variant_states: Result<Vec<_>> = if derive_type == DeriveType::Enum { |
| 462 | variants |
| 463 | .iter() |
| 464 | .zip(full_meta_infos.iter().cloned()) |
| 465 | .map(|(variant, info)| { |
| 466 | State::from_variant( |
| 467 | input, |
| 468 | trait_name, |
| 469 | trait_attr.clone(), |
| 470 | allowed_attr_params.clone(), |
| 471 | variant, |
| 472 | info, |
| 473 | ) |
| 474 | }) |
| 475 | .collect() |
| 476 | } else { |
| 477 | Ok(vec![]) |
| 478 | }; |
| 479 | |
| 480 | let generics = if add_type_bound { |
| 481 | add_extra_ty_param_bound(&input.generics, &trait_path) |
| 482 | } else { |
| 483 | input.generics.clone() |
| 484 | }; |
| 485 | |
| 486 | Ok(State { |
| 487 | input, |
| 488 | trait_name, |
| 489 | method_ident, |
| 490 | trait_path, |
| 491 | trait_path_params: vec![], |
| 492 | trait_attr, |
| 493 | // input, |
| 494 | fields, |
| 495 | variants, |
| 496 | variant_states: variant_states?, |
| 497 | variant: None, |
| 498 | derive_type, |
| 499 | generics, |
| 500 | full_meta_infos, |
| 501 | default_info: defaults, |
| 502 | }) |
| 503 | } |
| 504 | |
| 505 | pub fn from_variant<'arg_input>( |
| 506 | input: &'arg_input DeriveInput, |
| 507 | trait_name: &'static str, |
| 508 | trait_attr: String, |
| 509 | allowed_attr_params: AttrParams, |
| 510 | variant: &'arg_input Variant, |
| 511 | default_info: FullMetaInfo, |
| 512 | ) -> Result<State<'arg_input>> { |
| 513 | let trait_name = trait_name.trim_end_matches("ToInner" ); |
| 514 | let trait_ident = format_ident!(" {trait_name}" ); |
| 515 | let method_ident = format_ident!(" {trait_attr}" ); |
| 516 | let trait_path = quote! { derive_more::with_trait::#trait_ident }; |
| 517 | let (derive_type, fields): (_, Vec<_>) = match variant.fields { |
| 518 | Fields::Unnamed(ref fields) => { |
| 519 | (DeriveType::Unnamed, unnamed_to_vec(fields)) |
| 520 | } |
| 521 | |
| 522 | Fields::Named(ref fields) => (DeriveType::Named, named_to_vec(fields)), |
| 523 | Fields::Unit => (DeriveType::Named, vec![]), |
| 524 | }; |
| 525 | |
| 526 | let meta_infos: Result<Vec<_>> = fields |
| 527 | .iter() |
| 528 | .map(|f| &f.attrs) |
| 529 | .map(|attrs| get_meta_info(&trait_attr, attrs, &allowed_attr_params.field)) |
| 530 | .collect(); |
| 531 | let meta_infos = meta_infos?; |
| 532 | let full_meta_infos: Vec<_> = meta_infos |
| 533 | .into_iter() |
| 534 | .map(|info| info.into_full(default_info.clone())) |
| 535 | .collect(); |
| 536 | |
| 537 | let generics = add_extra_ty_param_bound(&input.generics, &trait_path); |
| 538 | |
| 539 | Ok(State { |
| 540 | input, |
| 541 | trait_name, |
| 542 | trait_path, |
| 543 | trait_path_params: vec![], |
| 544 | trait_attr, |
| 545 | method_ident, |
| 546 | // input, |
| 547 | fields, |
| 548 | variants: vec![], |
| 549 | variant_states: vec![], |
| 550 | variant: Some(variant), |
| 551 | derive_type, |
| 552 | generics, |
| 553 | full_meta_infos, |
| 554 | default_info, |
| 555 | }) |
| 556 | } |
| 557 | pub fn add_trait_path_type_param(&mut self, param: TokenStream) { |
| 558 | self.trait_path_params.push(param); |
| 559 | } |
| 560 | |
| 561 | pub fn assert_single_enabled_field<'state>( |
| 562 | &'state self, |
| 563 | ) -> SingleFieldData<'input, 'state> { |
| 564 | if self.derive_type == DeriveType::Enum { |
| 565 | panic_one_field(self.trait_name, &self.trait_attr); |
| 566 | } |
| 567 | let data = self.enabled_fields_data(); |
| 568 | if data.fields.len() != 1 { |
| 569 | panic_one_field(self.trait_name, &self.trait_attr); |
| 570 | }; |
| 571 | SingleFieldData { |
| 572 | input_type: data.input_type, |
| 573 | field: data.fields[0], |
| 574 | field_type: data.field_types[0], |
| 575 | member: data.members[0].clone(), |
| 576 | info: data.infos[0].clone(), |
| 577 | trait_path: data.trait_path, |
| 578 | trait_path_with_params: data.trait_path_with_params.clone(), |
| 579 | casted_trait: data.casted_traits[0].clone(), |
| 580 | impl_generics: data.impl_generics.clone(), |
| 581 | ty_generics: data.ty_generics.clone(), |
| 582 | where_clause: data.where_clause, |
| 583 | multi_field_data: data, |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | pub fn enabled_fields_data<'state>(&'state self) -> MultiFieldData<'input, 'state> { |
| 588 | if self.derive_type == DeriveType::Enum { |
| 589 | panic!("cannot derive( {}) for enum" , self.trait_name) |
| 590 | } |
| 591 | let fields = self.enabled_fields(); |
| 592 | let field_idents = self.enabled_fields_idents(); |
| 593 | let field_indexes = self.enabled_fields_indexes(); |
| 594 | let field_types: Vec<_> = fields.iter().map(|f| &f.ty).collect(); |
| 595 | let members: Vec<_> = field_idents |
| 596 | .iter() |
| 597 | .map(|ident| quote! { self.#ident }) |
| 598 | .collect(); |
| 599 | let trait_path = &self.trait_path; |
| 600 | let trait_path_with_params = if !self.trait_path_params.is_empty() { |
| 601 | let params = self.trait_path_params.iter(); |
| 602 | quote! { #trait_path<#(#params),*> } |
| 603 | } else { |
| 604 | self.trait_path.clone() |
| 605 | }; |
| 606 | |
| 607 | let casted_traits: Vec<_> = field_types |
| 608 | .iter() |
| 609 | .map(|field_type| quote! { <#field_type as #trait_path_with_params> }) |
| 610 | .collect(); |
| 611 | let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl(); |
| 612 | let input_type = &self.input.ident; |
| 613 | let (variant_name, variant_type) = self.variant.map_or_else( |
| 614 | || (None, quote! { #input_type }), |
| 615 | |v| { |
| 616 | let variant_name = &v.ident; |
| 617 | (Some(variant_name), quote! { #input_type::#variant_name }) |
| 618 | }, |
| 619 | ); |
| 620 | MultiFieldData { |
| 621 | input_type, |
| 622 | variant_type, |
| 623 | variant_name, |
| 624 | variant_info: self.default_info.clone(), |
| 625 | fields, |
| 626 | field_types, |
| 627 | field_indexes, |
| 628 | members, |
| 629 | infos: self.enabled_infos(), |
| 630 | field_idents, |
| 631 | method_ident: &self.method_ident, |
| 632 | trait_path, |
| 633 | trait_path_with_params, |
| 634 | casted_traits, |
| 635 | impl_generics, |
| 636 | ty_generics, |
| 637 | where_clause, |
| 638 | state: self, |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | pub fn enabled_variant_data<'state>( |
| 643 | &'state self, |
| 644 | ) -> MultiVariantData<'input, 'state> { |
| 645 | if self.derive_type != DeriveType::Enum { |
| 646 | panic!("can only derive( {}) for enum" , self.trait_name) |
| 647 | } |
| 648 | let variants = self.enabled_variants(); |
| 649 | MultiVariantData { |
| 650 | variants, |
| 651 | variant_states: self.enabled_variant_states(), |
| 652 | infos: self.enabled_infos(), |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | fn enabled_variants(&self) -> Vec<&'input Variant> { |
| 657 | self.variants |
| 658 | .iter() |
| 659 | .zip(self.full_meta_infos.iter().map(|info| info.enabled)) |
| 660 | .filter(|(_, ig)| *ig) |
| 661 | .map(|(v, _)| *v) |
| 662 | .collect() |
| 663 | } |
| 664 | |
| 665 | fn enabled_variant_states(&self) -> Vec<&State<'input>> { |
| 666 | self.variant_states |
| 667 | .iter() |
| 668 | .zip(self.full_meta_infos.iter().map(|info| info.enabled)) |
| 669 | .filter(|(_, ig)| *ig) |
| 670 | .map(|(v, _)| v) |
| 671 | .collect() |
| 672 | } |
| 673 | |
| 674 | pub fn enabled_fields(&self) -> Vec<&'input Field> { |
| 675 | self.fields |
| 676 | .iter() |
| 677 | .zip(self.full_meta_infos.iter().map(|info| info.enabled)) |
| 678 | .filter(|(_, ig)| *ig) |
| 679 | .map(|(f, _)| *f) |
| 680 | .collect() |
| 681 | } |
| 682 | |
| 683 | fn field_idents(&self) -> Vec<TokenStream> { |
| 684 | if self.derive_type == DeriveType::Named { |
| 685 | self.fields |
| 686 | .iter() |
| 687 | .map(|f| { |
| 688 | f.ident |
| 689 | .as_ref() |
| 690 | .expect("Tried to get field names of a tuple struct" ) |
| 691 | .to_token_stream() |
| 692 | }) |
| 693 | .collect() |
| 694 | } else { |
| 695 | let count = self.fields.len(); |
| 696 | (0..count) |
| 697 | .map(|i| Index::from(i).to_token_stream()) |
| 698 | .collect() |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | fn enabled_fields_idents(&self) -> Vec<TokenStream> { |
| 703 | self.field_idents() |
| 704 | .into_iter() |
| 705 | .zip(self.full_meta_infos.iter().map(|info| info.enabled)) |
| 706 | .filter(|(_, ig)| *ig) |
| 707 | .map(|(f, _)| f) |
| 708 | .collect() |
| 709 | } |
| 710 | |
| 711 | fn enabled_fields_indexes(&self) -> Vec<usize> { |
| 712 | self.full_meta_infos |
| 713 | .iter() |
| 714 | .map(|info| info.enabled) |
| 715 | .enumerate() |
| 716 | .filter(|(_, ig)| *ig) |
| 717 | .map(|(i, _)| i) |
| 718 | .collect() |
| 719 | } |
| 720 | fn enabled_infos(&self) -> Vec<FullMetaInfo> { |
| 721 | self.full_meta_infos |
| 722 | .iter() |
| 723 | .filter(|info| info.enabled) |
| 724 | .cloned() |
| 725 | .collect() |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | #[derive (Clone)] |
| 730 | pub struct SingleFieldData<'input, 'state> { |
| 731 | pub input_type: &'input Ident, |
| 732 | pub field: &'input Field, |
| 733 | pub field_type: &'input Type, |
| 734 | pub member: TokenStream, |
| 735 | pub info: FullMetaInfo, |
| 736 | pub trait_path: &'state TokenStream, |
| 737 | pub trait_path_with_params: TokenStream, |
| 738 | pub casted_trait: TokenStream, |
| 739 | pub impl_generics: ImplGenerics<'state>, |
| 740 | pub ty_generics: TypeGenerics<'state>, |
| 741 | pub where_clause: Option<&'state WhereClause>, |
| 742 | multi_field_data: MultiFieldData<'input, 'state>, |
| 743 | } |
| 744 | |
| 745 | #[derive (Clone)] |
| 746 | pub struct MultiFieldData<'input, 'state> { |
| 747 | pub input_type: &'input Ident, |
| 748 | pub variant_type: TokenStream, |
| 749 | pub variant_name: Option<&'input Ident>, |
| 750 | pub variant_info: FullMetaInfo, |
| 751 | pub fields: Vec<&'input Field>, |
| 752 | pub field_types: Vec<&'input Type>, |
| 753 | pub field_idents: Vec<TokenStream>, |
| 754 | pub field_indexes: Vec<usize>, |
| 755 | pub members: Vec<TokenStream>, |
| 756 | pub infos: Vec<FullMetaInfo>, |
| 757 | pub method_ident: &'state Ident, |
| 758 | pub trait_path: &'state TokenStream, |
| 759 | pub trait_path_with_params: TokenStream, |
| 760 | pub casted_traits: Vec<TokenStream>, |
| 761 | pub impl_generics: ImplGenerics<'state>, |
| 762 | pub ty_generics: TypeGenerics<'state>, |
| 763 | pub where_clause: Option<&'state WhereClause>, |
| 764 | pub state: &'state State<'input>, |
| 765 | } |
| 766 | |
| 767 | pub struct MultiVariantData<'input, 'state> { |
| 768 | pub variants: Vec<&'input Variant>, |
| 769 | pub variant_states: Vec<&'state State<'input>>, |
| 770 | pub infos: Vec<FullMetaInfo>, |
| 771 | } |
| 772 | |
| 773 | impl MultiFieldData<'_, '_> { |
| 774 | pub fn initializer<T: ToTokens>(&self, initializers: &[T]) -> TokenStream { |
| 775 | let MultiFieldData { |
| 776 | variant_type, |
| 777 | field_idents, |
| 778 | .. |
| 779 | } = self; |
| 780 | if self.state.derive_type == DeriveType::Named { |
| 781 | quote! { #variant_type{#(#field_idents: #initializers),*} } |
| 782 | } else { |
| 783 | quote! { #variant_type(#(#initializers),*) } |
| 784 | } |
| 785 | } |
| 786 | pub fn matcher<T: ToTokens>( |
| 787 | &self, |
| 788 | indexes: &[usize], |
| 789 | bindings: &[T], |
| 790 | ) -> TokenStream { |
| 791 | let MultiFieldData { variant_type, .. } = self; |
| 792 | let full_bindings = (0..self.state.fields.len()).map(|i| { |
| 793 | indexes.iter().position(|index| i == *index).map_or_else( |
| 794 | || quote! { _ }, |
| 795 | |found_index| bindings[found_index].to_token_stream(), |
| 796 | ) |
| 797 | }); |
| 798 | if self.state.derive_type == DeriveType::Named { |
| 799 | let field_idents = self.state.field_idents(); |
| 800 | quote! { #variant_type{#(#field_idents: #full_bindings),*} } |
| 801 | } else { |
| 802 | quote! { #variant_type(#(#full_bindings),*) } |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | impl SingleFieldData<'_, '_> { |
| 808 | pub fn initializer<T: ToTokens>(&self, initializers: &[T]) -> TokenStream { |
| 809 | self.multi_field_data.initializer(initializers) |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | fn get_meta_info( |
| 814 | trait_attr: &str, |
| 815 | attrs: &[Attribute], |
| 816 | allowed_attr_params: &[&str], |
| 817 | ) -> Result<MetaInfo> { |
| 818 | let mut it = attrs.iter().filter(|a| { |
| 819 | a.meta |
| 820 | .path() |
| 821 | .segments |
| 822 | .first() |
| 823 | .map(|p| p.ident == trait_attr) |
| 824 | .unwrap_or_default() |
| 825 | }); |
| 826 | |
| 827 | let mut info = MetaInfo::default(); |
| 828 | |
| 829 | let Some(attr) = it.next() else { |
| 830 | return Ok(info); |
| 831 | }; |
| 832 | |
| 833 | if allowed_attr_params.is_empty() { |
| 834 | return Err(Error::new(attr.span(), "Attribute is not allowed here" )); |
| 835 | } |
| 836 | |
| 837 | info.enabled = Some(true); |
| 838 | |
| 839 | if let Some(another_attr) = it.next() { |
| 840 | return Err(Error::new( |
| 841 | another_attr.span(), |
| 842 | "Only a single attribute is allowed" , |
| 843 | )); |
| 844 | } |
| 845 | |
| 846 | let list = match &attr.meta { |
| 847 | syn::Meta::Path(_) => { |
| 848 | if allowed_attr_params.contains(&"ignore" ) { |
| 849 | return Ok(info); |
| 850 | } else { |
| 851 | return Err(Error::new( |
| 852 | attr.span(), |
| 853 | format!( |
| 854 | "Empty attribute is not allowed, add one of the following parameters: {}" , |
| 855 | allowed_attr_params.join(", " ), |
| 856 | ), |
| 857 | )); |
| 858 | } |
| 859 | } |
| 860 | syn::Meta::List(list) => list, |
| 861 | syn::Meta::NameValue(val) => { |
| 862 | return Err(Error::new( |
| 863 | val.span(), |
| 864 | "Attribute doesn't support name-value format here" , |
| 865 | )); |
| 866 | } |
| 867 | }; |
| 868 | |
| 869 | parse_punctuated_nested_meta( |
| 870 | &mut info, |
| 871 | &list.parse_args_with(Punctuated::parse_terminated)?, |
| 872 | allowed_attr_params, |
| 873 | None, |
| 874 | )?; |
| 875 | |
| 876 | Ok(info) |
| 877 | } |
| 878 | |
| 879 | fn parse_punctuated_nested_meta( |
| 880 | info: &mut MetaInfo, |
| 881 | meta: &Punctuated<polyfill::Meta, Token![,]>, |
| 882 | allowed_attr_params: &[&str], |
| 883 | wrapper_name: Option<&str>, |
| 884 | ) -> Result<()> { |
| 885 | for meta in meta.iter() { |
| 886 | match meta { |
| 887 | polyfill::Meta::List(list) if list.path.is_ident("not" ) => { |
| 888 | if wrapper_name.is_some() { |
| 889 | // Only single top-level `not` attribute is allowed. |
| 890 | return Err(Error::new( |
| 891 | list.span(), |
| 892 | "Attribute doesn't support multiple multiple or nested `not` parameters" , |
| 893 | )); |
| 894 | } |
| 895 | parse_punctuated_nested_meta( |
| 896 | info, |
| 897 | &list.parse_args_with(Punctuated::parse_terminated)?, |
| 898 | allowed_attr_params, |
| 899 | Some("not" ), |
| 900 | )?; |
| 901 | } |
| 902 | |
| 903 | polyfill::Meta::List(list) => { |
| 904 | let path = &list.path; |
| 905 | if !allowed_attr_params.iter().any(|param| path.is_ident(param)) { |
| 906 | return Err(Error::new( |
| 907 | meta.span(), |
| 908 | format!( |
| 909 | "Attribute nested parameter not supported. \ |
| 910 | Supported attribute parameters are: {}" , |
| 911 | allowed_attr_params.join(", " ), |
| 912 | ), |
| 913 | )); |
| 914 | } |
| 915 | |
| 916 | let mut parse_nested = true; |
| 917 | |
| 918 | let attr_name = path.get_ident().unwrap().to_string(); |
| 919 | match (wrapper_name, attr_name.as_str()) { |
| 920 | (None, "owned" ) => info.owned = Some(true), |
| 921 | (None, "ref" ) => info.ref_ = Some(true), |
| 922 | (None, "ref_mut" ) => info.ref_mut = Some(true), |
| 923 | |
| 924 | #[cfg (any(feature = "from" , feature = "into" ))] |
| 925 | (None, "types" ) |
| 926 | | (Some("owned" ), "types" ) |
| 927 | | (Some("ref" ), "types" ) |
| 928 | | (Some("ref_mut" ), "types" ) => { |
| 929 | parse_nested = false; |
| 930 | for meta in &list.parse_args_with( |
| 931 | Punctuated::<polyfill::NestedMeta, syn::token::Comma>::parse_terminated, |
| 932 | )? { |
| 933 | let typ: syn::Type = match meta { |
| 934 | polyfill::NestedMeta::Meta(meta) => { |
| 935 | let polyfill::Meta::Path(path) = meta else { |
| 936 | return Err(Error::new( |
| 937 | meta.span(), |
| 938 | format!( |
| 939 | "Attribute doesn't support type {}" , |
| 940 | quote! { #meta }, |
| 941 | ), |
| 942 | )); |
| 943 | }; |
| 944 | syn::TypePath { |
| 945 | qself: None, |
| 946 | path: path.clone().into(), |
| 947 | } |
| 948 | .into() |
| 949 | } |
| 950 | polyfill::NestedMeta::Lit(syn::Lit::Str(s)) => s.parse()?, |
| 951 | polyfill::NestedMeta::Lit(lit) => return Err(Error::new( |
| 952 | lit.span(), |
| 953 | "Attribute doesn't support nested literals here" , |
| 954 | )), |
| 955 | }; |
| 956 | |
| 957 | for ref_type in wrapper_name |
| 958 | .map(|n| vec![RefType::from_attr_name(n)]) |
| 959 | .unwrap_or_else(|| { |
| 960 | vec![RefType::No, RefType::Ref, RefType::Mut] |
| 961 | }) |
| 962 | { |
| 963 | if info |
| 964 | .types |
| 965 | .entry(ref_type) |
| 966 | .or_default() |
| 967 | .replace(typ.clone()) |
| 968 | .is_some() |
| 969 | { |
| 970 | return Err(Error::new( |
| 971 | typ.span(), |
| 972 | format!( |
| 973 | "Duplicate type ` {}` specified" , |
| 974 | quote! { #path }, |
| 975 | ), |
| 976 | )); |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | _ => { |
| 983 | return Err(Error::new( |
| 984 | list.span(), |
| 985 | format!( |
| 986 | "Attribute doesn't support nested parameter ` {}` here" , |
| 987 | quote! { #path }, |
| 988 | ), |
| 989 | )) |
| 990 | } |
| 991 | }; |
| 992 | |
| 993 | if parse_nested { |
| 994 | parse_punctuated_nested_meta( |
| 995 | info, |
| 996 | &list.parse_args_with(Punctuated::parse_terminated)?, |
| 997 | allowed_attr_params, |
| 998 | Some(&attr_name), |
| 999 | )?; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | polyfill::Meta::Path(path) => { |
| 1004 | if !allowed_attr_params.iter().any(|param| path.is_ident(param)) { |
| 1005 | return Err(Error::new( |
| 1006 | meta.span(), |
| 1007 | format!( |
| 1008 | "Attribute parameter not supported. \ |
| 1009 | Supported attribute parameters are: {}" , |
| 1010 | allowed_attr_params.join(", " ), |
| 1011 | ), |
| 1012 | )); |
| 1013 | } |
| 1014 | |
| 1015 | let attr_name = path.get_ident().unwrap().to_string(); |
| 1016 | match (wrapper_name, attr_name.as_str()) { |
| 1017 | (None, "ignore" ) => info.enabled = Some(false), |
| 1018 | (None, "forward" ) => info.forward = Some(true), |
| 1019 | (Some("not" ), "forward" ) => info.forward = Some(false), |
| 1020 | (None, "owned" ) => info.owned = Some(true), |
| 1021 | (None, "ref" ) => info.ref_ = Some(true), |
| 1022 | (None, "ref_mut" ) => info.ref_mut = Some(true), |
| 1023 | (None, "source" ) => info.source = Some(true), |
| 1024 | (Some("not" ), "source" ) => info.source = Some(false), |
| 1025 | (None, "backtrace" ) => info.backtrace = Some(true), |
| 1026 | (Some("not" ), "backtrace" ) => info.backtrace = Some(false), |
| 1027 | _ => { |
| 1028 | return Err(Error::new( |
| 1029 | path.span(), |
| 1030 | format!( |
| 1031 | "Attribute doesn't support parameter ` {}` here" , |
| 1032 | quote! { #path } |
| 1033 | ), |
| 1034 | )) |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | Ok(()) |
| 1042 | } |
| 1043 | |
| 1044 | // TODO: Remove this eventually, once all macros migrate to |
| 1045 | // custom typed attributes parsing. |
| 1046 | /// Polyfill for [`syn`] 1.x AST. |
| 1047 | pub(crate) mod polyfill { |
| 1048 | use proc_macro2::TokenStream; |
| 1049 | use quote::ToTokens; |
| 1050 | use syn::{ |
| 1051 | ext::IdentExt as _, |
| 1052 | parse::{Parse, ParseStream, Parser}, |
| 1053 | token, Token, |
| 1054 | }; |
| 1055 | |
| 1056 | #[derive (Clone)] |
| 1057 | pub(crate) enum PathOrKeyword { |
| 1058 | Path(syn::Path), |
| 1059 | Keyword(syn::Ident), |
| 1060 | } |
| 1061 | |
| 1062 | impl Parse for PathOrKeyword { |
| 1063 | fn parse(input: ParseStream<'_>) -> syn::Result<Self> { |
| 1064 | if input.fork().parse::<syn::Path>().is_ok() { |
| 1065 | return input.parse().map(Self::Path); |
| 1066 | } |
| 1067 | syn::Ident::parse_any(input).map(Self::Keyword) |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | impl ToTokens for PathOrKeyword { |
| 1072 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1073 | match self { |
| 1074 | Self::Path(p) => p.to_tokens(tokens), |
| 1075 | Self::Keyword(i) => i.to_tokens(tokens), |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | impl PathOrKeyword { |
| 1081 | pub(crate) fn is_ident<I: ?Sized>(&self, ident: &I) -> bool |
| 1082 | where |
| 1083 | syn::Ident: PartialEq<I>, |
| 1084 | { |
| 1085 | match self { |
| 1086 | Self::Path(p) => p.is_ident(ident), |
| 1087 | Self::Keyword(i) => i == ident, |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | pub fn get_ident(&self) -> Option<&syn::Ident> { |
| 1092 | match self { |
| 1093 | Self::Path(p) => p.get_ident(), |
| 1094 | Self::Keyword(i) => Some(i), |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | impl From<PathOrKeyword> for syn::Path { |
| 1100 | fn from(p: PathOrKeyword) -> Self { |
| 1101 | match p { |
| 1102 | PathOrKeyword::Path(p) => p, |
| 1103 | PathOrKeyword::Keyword(i) => i.into(), |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | #[derive (Clone)] |
| 1109 | pub(crate) struct MetaList { |
| 1110 | pub(crate) path: PathOrKeyword, |
| 1111 | pub(crate) tokens: TokenStream, |
| 1112 | } |
| 1113 | |
| 1114 | impl Parse for MetaList { |
| 1115 | fn parse(input: ParseStream<'_>) -> syn::Result<Self> { |
| 1116 | let path = input.parse::<PathOrKeyword>()?; |
| 1117 | let tokens; |
| 1118 | _ = syn::parenthesized!(tokens in input); |
| 1119 | Ok(Self { |
| 1120 | path, |
| 1121 | tokens: tokens.parse()?, |
| 1122 | }) |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | impl ToTokens for MetaList { |
| 1127 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1128 | self.path.to_tokens(tokens); |
| 1129 | token::Paren::default() |
| 1130 | .surround(tokens, |tokens| self.tokens.to_tokens(tokens)) |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | impl MetaList { |
| 1135 | pub fn parse_args_with<F: Parser>(&self, parser: F) -> syn::Result<F::Output> { |
| 1136 | parser.parse2(self.tokens.clone()) |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | #[derive (Clone)] |
| 1141 | pub(crate) enum Meta { |
| 1142 | Path(PathOrKeyword), |
| 1143 | List(MetaList), |
| 1144 | } |
| 1145 | |
| 1146 | impl Parse for Meta { |
| 1147 | fn parse(input: ParseStream<'_>) -> syn::Result<Self> { |
| 1148 | let path = input.parse::<PathOrKeyword>()?; |
| 1149 | Ok(if input.peek(token::Paren) { |
| 1150 | let tokens; |
| 1151 | _ = syn::parenthesized!(tokens in input); |
| 1152 | Self::List(MetaList { |
| 1153 | path, |
| 1154 | tokens: tokens.parse()?, |
| 1155 | }) |
| 1156 | } else { |
| 1157 | Self::Path(path) |
| 1158 | }) |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | impl ToTokens for Meta { |
| 1163 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1164 | match self { |
| 1165 | Self::Path(p) => p.to_tokens(tokens), |
| 1166 | Self::List(l) => l.to_tokens(tokens), |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | #[derive (Clone)] |
| 1172 | pub(crate) enum NestedMeta { |
| 1173 | Meta(Meta), |
| 1174 | Lit(syn::Lit), |
| 1175 | } |
| 1176 | |
| 1177 | impl Parse for NestedMeta { |
| 1178 | fn parse(input: ParseStream<'_>) -> syn::Result<Self> { |
| 1179 | if input.peek(syn::Lit) |
| 1180 | && !(input.peek(syn::LitBool) && input.peek2(Token![=])) |
| 1181 | { |
| 1182 | input.parse().map(Self::Lit) |
| 1183 | } else if input.peek(syn::Ident::peek_any) |
| 1184 | || input.peek(Token![::]) && input.peek3(syn::Ident::peek_any) |
| 1185 | { |
| 1186 | input.parse().map(Self::Meta) |
| 1187 | } else { |
| 1188 | Err(input.error("expected identifier or literal" )) |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | impl ToTokens for NestedMeta { |
| 1194 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1195 | match self { |
| 1196 | Self::Meta(m) => m.to_tokens(tokens), |
| 1197 | Self::Lit(l) => l.to_tokens(tokens), |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | #[derive (Clone, Debug, Default)] |
| 1204 | pub struct FullMetaInfo { |
| 1205 | pub enabled: bool, |
| 1206 | pub forward: bool, |
| 1207 | pub owned: bool, |
| 1208 | pub ref_: bool, |
| 1209 | pub ref_mut: bool, |
| 1210 | pub info: MetaInfo, |
| 1211 | } |
| 1212 | |
| 1213 | #[derive (Clone, Debug, Default)] |
| 1214 | pub struct MetaInfo { |
| 1215 | pub enabled: Option<bool>, |
| 1216 | pub forward: Option<bool>, |
| 1217 | pub owned: Option<bool>, |
| 1218 | pub ref_: Option<bool>, |
| 1219 | pub ref_mut: Option<bool>, |
| 1220 | pub source: Option<bool>, |
| 1221 | pub backtrace: Option<bool>, |
| 1222 | #[cfg (any(feature = "from" , feature = "into" ))] |
| 1223 | pub types: HashMap<RefType, HashSet<syn::Type>>, |
| 1224 | } |
| 1225 | |
| 1226 | impl MetaInfo { |
| 1227 | fn into_full(self, defaults: FullMetaInfo) -> FullMetaInfo { |
| 1228 | FullMetaInfo { |
| 1229 | enabled: self.enabled.unwrap_or(default:defaults.enabled), |
| 1230 | forward: self.forward.unwrap_or(default:defaults.forward), |
| 1231 | owned: self.owned.unwrap_or(default:defaults.owned), |
| 1232 | ref_: self.ref_.unwrap_or(default:defaults.ref_), |
| 1233 | ref_mut: self.ref_mut.unwrap_or(default:defaults.ref_mut), |
| 1234 | info: self, |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | impl FullMetaInfo { |
| 1240 | pub fn ref_types(&self) -> Vec<RefType> { |
| 1241 | let mut ref_types: Vec = vec![]; |
| 1242 | if self.owned { |
| 1243 | ref_types.push(RefType::No); |
| 1244 | } |
| 1245 | if self.ref_ { |
| 1246 | ref_types.push(RefType::Ref); |
| 1247 | } |
| 1248 | if self.ref_mut { |
| 1249 | ref_types.push(RefType::Mut); |
| 1250 | } |
| 1251 | ref_types |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | pub fn get_if_type_parameter_used_in_type( |
| 1256 | type_parameters: &HashSet<syn::Ident>, |
| 1257 | ty: &syn::Type, |
| 1258 | ) -> Option<syn::Type> { |
| 1259 | is_type_parameter_used_in_type(type_parameters, ty).then(|| match ty { |
| 1260 | syn::Type::Reference(syn::TypeReference { elem: ty: &Box, .. }) => (**ty).clone(), |
| 1261 | ty: &Type => ty.clone(), |
| 1262 | }) |
| 1263 | } |
| 1264 | |
| 1265 | pub fn is_type_parameter_used_in_type( |
| 1266 | type_parameters: &HashSet<syn::Ident>, |
| 1267 | ty: &syn::Type, |
| 1268 | ) -> bool { |
| 1269 | match ty { |
| 1270 | syn::Type::Path(ty) => { |
| 1271 | if let Some(qself) = &ty.qself { |
| 1272 | if is_type_parameter_used_in_type(type_parameters, &qself.ty) { |
| 1273 | return true; |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | if let Some(segment) = ty.path.segments.first() { |
| 1278 | if type_parameters.contains(&segment.ident) { |
| 1279 | return true; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | ty.path.segments.iter().any(|segment| { |
| 1284 | if let syn::PathArguments::AngleBracketed(arguments) = |
| 1285 | &segment.arguments |
| 1286 | { |
| 1287 | arguments.args.iter().any(|argument| match argument { |
| 1288 | syn::GenericArgument::Type(ty) => { |
| 1289 | is_type_parameter_used_in_type(type_parameters, ty) |
| 1290 | } |
| 1291 | syn::GenericArgument::Constraint(constraint) => { |
| 1292 | type_parameters.contains(&constraint.ident) |
| 1293 | } |
| 1294 | _ => false, |
| 1295 | }) |
| 1296 | } else { |
| 1297 | false |
| 1298 | } |
| 1299 | }) |
| 1300 | } |
| 1301 | |
| 1302 | syn::Type::Reference(ty) => { |
| 1303 | is_type_parameter_used_in_type(type_parameters, &ty.elem) |
| 1304 | } |
| 1305 | |
| 1306 | _ => false, |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | #[cfg (any( |
| 1311 | feature = "as_ref" , |
| 1312 | feature = "debug" , |
| 1313 | feature = "display" , |
| 1314 | feature = "from" , |
| 1315 | feature = "into" , |
| 1316 | feature = "try_from" , |
| 1317 | ))] |
| 1318 | mod either { |
| 1319 | use proc_macro2::TokenStream; |
| 1320 | use quote::ToTokens; |
| 1321 | use syn::parse::{discouraged::Speculative as _, Parse, ParseStream}; |
| 1322 | |
| 1323 | /// Either [`Left`] or [`Right`]. |
| 1324 | /// |
| 1325 | /// [`Left`]: Either::Left |
| 1326 | /// [`Right`]: Either::Right |
| 1327 | #[derive (Clone, Copy, Debug)] |
| 1328 | pub(crate) enum Either<L, R> { |
| 1329 | /// Left variant. |
| 1330 | Left(L), |
| 1331 | |
| 1332 | /// Right variant. |
| 1333 | Right(R), |
| 1334 | } |
| 1335 | |
| 1336 | impl<L, R> Parse for Either<L, R> |
| 1337 | where |
| 1338 | L: Parse, |
| 1339 | R: Parse, |
| 1340 | { |
| 1341 | fn parse(input: ParseStream<'_>) -> syn::Result<Self> { |
| 1342 | let ahead = input.fork(); |
| 1343 | if let Ok(left) = ahead.parse::<L>() { |
| 1344 | input.advance_to(&ahead); |
| 1345 | Ok(Self::Left(left)) |
| 1346 | } else { |
| 1347 | input.parse::<R>().map(Self::Right) |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | impl<L, R, T> Iterator for Either<L, R> |
| 1353 | where |
| 1354 | L: Iterator<Item = T>, |
| 1355 | R: Iterator<Item = T>, |
| 1356 | { |
| 1357 | type Item = T; |
| 1358 | |
| 1359 | fn next(&mut self) -> Option<Self::Item> { |
| 1360 | match self { |
| 1361 | Self::Left(left) => left.next(), |
| 1362 | Self::Right(right) => right.next(), |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | impl<L, R> ToTokens for Either<L, R> |
| 1368 | where |
| 1369 | L: ToTokens, |
| 1370 | R: ToTokens, |
| 1371 | { |
| 1372 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1373 | match self { |
| 1374 | Self::Left(l) => l.to_tokens(tokens), |
| 1375 | Self::Right(r) => r.to_tokens(tokens), |
| 1376 | } |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | #[cfg (any( |
| 1382 | feature = "as_ref" , |
| 1383 | feature = "debug" , |
| 1384 | feature = "display" , |
| 1385 | feature = "from" , |
| 1386 | feature = "into" , |
| 1387 | feature = "try_from" , |
| 1388 | ))] |
| 1389 | mod spanning { |
| 1390 | use std::ops::{Deref, DerefMut}; |
| 1391 | |
| 1392 | use proc_macro2::Span; |
| 1393 | |
| 1394 | /// Wrapper for non-[`Spanned`] types to hold their [`Span`]. |
| 1395 | /// |
| 1396 | /// [`Spanned`]: syn::spanned::Spanned |
| 1397 | #[derive (Clone, Copy, Debug)] |
| 1398 | pub(crate) struct Spanning<T: ?Sized> { |
| 1399 | /// [`Span`] of the `item`. |
| 1400 | pub(crate) span: Span, |
| 1401 | |
| 1402 | /// Item the [`Span`] is held for. |
| 1403 | pub(crate) item: T, |
| 1404 | } |
| 1405 | |
| 1406 | impl<T: ?Sized> Spanning<T> { |
| 1407 | /// Creates a new [`Spanning`] `item`, attaching the provided [`Span`] to it. |
| 1408 | pub(crate) const fn new(item: T, span: Span) -> Self |
| 1409 | where |
| 1410 | T: Sized, |
| 1411 | { |
| 1412 | Self { span, item } |
| 1413 | } |
| 1414 | |
| 1415 | /// Destructures this [`Spanning`] wrapper returning the underlying `item`. |
| 1416 | pub fn into_inner(self) -> T |
| 1417 | where |
| 1418 | T: Sized, |
| 1419 | { |
| 1420 | self.item |
| 1421 | } |
| 1422 | |
| 1423 | /// Returns the [`Span`] contained in this [`Spanning`] wrapper. |
| 1424 | pub(crate) const fn span(&self) -> Span { |
| 1425 | self.span |
| 1426 | } |
| 1427 | |
| 1428 | /// Converts this `&`[`Spanning`]`<T>` into [`Spanning`]`<&T>` (moves the reference inside). |
| 1429 | pub(crate) const fn as_ref(&self) -> Spanning<&T> { |
| 1430 | Spanning { |
| 1431 | span: self.span, |
| 1432 | item: &self.item, |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | /// Maps the wrapped `item` with the provided `f`unction, preserving the current [`Span`]. |
| 1437 | pub(crate) fn map<U>(self, f: impl FnOnce(T) -> U) -> Spanning<U> |
| 1438 | where |
| 1439 | T: Sized, |
| 1440 | { |
| 1441 | Spanning { |
| 1442 | span: self.span, |
| 1443 | item: f(self.item), |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | #[cfg (feature = "into" )] |
| 1449 | impl<T> Spanning<Option<T>> { |
| 1450 | pub(crate) fn transpose(self) -> Option<Spanning<T>> { |
| 1451 | match self.item { |
| 1452 | Some(item) => Some(Spanning { |
| 1453 | item, |
| 1454 | span: self.span, |
| 1455 | }), |
| 1456 | None => None, |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | impl<T: ?Sized> Deref for Spanning<T> { |
| 1462 | type Target = T; |
| 1463 | |
| 1464 | fn deref(&self) -> &Self::Target { |
| 1465 | &self.item |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | impl<T: ?Sized> DerefMut for Spanning<T> { |
| 1470 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 1471 | &mut self.item |
| 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | #[cfg (any( |
| 1477 | feature = "as_ref" , |
| 1478 | feature = "debug" , |
| 1479 | feature = "display" , |
| 1480 | feature = "from" , |
| 1481 | feature = "into" , |
| 1482 | feature = "try_from" , |
| 1483 | ))] |
| 1484 | pub(crate) mod attr { |
| 1485 | use std::any::Any; |
| 1486 | |
| 1487 | use syn::{ |
| 1488 | parse::{Parse, ParseStream}, |
| 1489 | spanned::Spanned as _, |
| 1490 | }; |
| 1491 | |
| 1492 | use super::{Either, Spanning}; |
| 1493 | |
| 1494 | #[cfg (any( |
| 1495 | feature = "as_ref" , |
| 1496 | feature = "from" , |
| 1497 | feature = "into" , |
| 1498 | feature = "try_from" |
| 1499 | ))] |
| 1500 | pub(crate) use self::empty::Empty; |
| 1501 | #[cfg (any( |
| 1502 | feature = "as_ref" , |
| 1503 | feature = "debug" , |
| 1504 | feature = "from" , |
| 1505 | feature = "into" , |
| 1506 | ))] |
| 1507 | pub(crate) use self::skip::Skip; |
| 1508 | #[cfg (any(feature = "as_ref" , feature = "from" , feature = "try_from" ))] |
| 1509 | pub(crate) use self::types::Types; |
| 1510 | #[cfg (any(feature = "as_ref" , feature = "from" ))] |
| 1511 | pub(crate) use self::{ |
| 1512 | conversion::Conversion, field_conversion::FieldConversion, forward::Forward, |
| 1513 | }; |
| 1514 | #[cfg (feature = "try_from" )] |
| 1515 | pub(crate) use self::{repr_conversion::ReprConversion, repr_int::ReprInt}; |
| 1516 | |
| 1517 | /// [`Parse`]ing with additional state or metadata. |
| 1518 | pub(crate) trait Parser { |
| 1519 | /// [`Parse`]s an item, using additional state or metadata. |
| 1520 | /// |
| 1521 | /// Default implementation just calls [`Parse::parse()`] directly. |
| 1522 | fn parse<T: Parse + Any>(&self, input: ParseStream<'_>) -> syn::Result<T> { |
| 1523 | T::parse(input) |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | impl Parser for () {} |
| 1528 | |
| 1529 | /// Parsing of a typed attribute from multiple [`syn::Attribute`]s. |
| 1530 | pub(crate) trait ParseMultiple: Parse + Sized + 'static { |
| 1531 | /// Parses this attribute from the provided single [`syn::Attribute`] with the provided |
| 1532 | /// [`Parser`]. |
| 1533 | /// |
| 1534 | /// Required, because with [`Parse`] we only able to parse inner attribute tokens, which |
| 1535 | /// doesn't work for attributes with empty arguments, like `#[attr]`. |
| 1536 | /// |
| 1537 | /// Override this method if the default [`syn::Attribute::parse_args_with()`] is not enough. |
| 1538 | fn parse_attr_with<P: Parser>( |
| 1539 | attr: &syn::Attribute, |
| 1540 | parser: &P, |
| 1541 | ) -> syn::Result<Self> { |
| 1542 | attr.parse_args_with(|ps: ParseStream<'_>| parser.parse(ps)) |
| 1543 | } |
| 1544 | |
| 1545 | /// Merges multiple values of this attribute into a single one. |
| 1546 | /// |
| 1547 | /// Default implementation only errors, disallowing multiple values of the same attribute. |
| 1548 | fn merge_attrs( |
| 1549 | _prev: Spanning<Self>, |
| 1550 | new: Spanning<Self>, |
| 1551 | name: &syn::Ident, |
| 1552 | ) -> syn::Result<Spanning<Self>> { |
| 1553 | Err(syn::Error::new( |
| 1554 | new.span, |
| 1555 | format!("only single `#[ {name}(...)]` attribute is allowed here" ), |
| 1556 | )) |
| 1557 | } |
| 1558 | |
| 1559 | /// Merges multiple [`Option`]al values of this attribute into a single one. |
| 1560 | /// |
| 1561 | /// Default implementation uses [`ParseMultiple::merge_attrs()`] when both `prev` and `new` |
| 1562 | /// are [`Some`]. |
| 1563 | fn merge_opt_attrs( |
| 1564 | prev: Option<Spanning<Self>>, |
| 1565 | new: Option<Spanning<Self>>, |
| 1566 | name: &syn::Ident, |
| 1567 | ) -> syn::Result<Option<Spanning<Self>>> { |
| 1568 | Ok(match (prev, new) { |
| 1569 | (Some(p), Some(n)) => Some(Self::merge_attrs(p, n, name)?), |
| 1570 | (Some(p), None) => Some(p), |
| 1571 | (None, Some(n)) => Some(n), |
| 1572 | (None, None) => None, |
| 1573 | }) |
| 1574 | } |
| 1575 | |
| 1576 | /// Parses this attribute from the provided multiple [`syn::Attribute`]s with the provided |
| 1577 | /// [`Parser`], merging them, and preserving their [`Span`]. |
| 1578 | /// |
| 1579 | /// [`Span`]: proc_macro2::Span |
| 1580 | fn parse_attrs_with<P: Parser>( |
| 1581 | attrs: impl AsRef<[syn::Attribute]>, |
| 1582 | name: &syn::Ident, |
| 1583 | parser: &P, |
| 1584 | ) -> syn::Result<Option<Spanning<Self>>> { |
| 1585 | attrs |
| 1586 | .as_ref() |
| 1587 | .iter() |
| 1588 | .filter(|attr| attr.path().is_ident(name)) |
| 1589 | .try_fold(None, |merged, attr| { |
| 1590 | let parsed = Spanning::new( |
| 1591 | Self::parse_attr_with(attr, parser)?, |
| 1592 | attr.span(), |
| 1593 | ); |
| 1594 | if let Some(prev) = merged { |
| 1595 | Self::merge_attrs(prev, parsed, name).map(Some) |
| 1596 | } else { |
| 1597 | Ok(Some(parsed)) |
| 1598 | } |
| 1599 | }) |
| 1600 | } |
| 1601 | |
| 1602 | /// Parses this attribute from the provided multiple [`syn::Attribute`]s with the default |
| 1603 | /// [`Parse`], merging them, and preserving their [`Span`]. |
| 1604 | /// |
| 1605 | /// [`Span`]: proc_macro2::Span |
| 1606 | fn parse_attrs( |
| 1607 | attrs: impl AsRef<[syn::Attribute]>, |
| 1608 | name: &syn::Ident, |
| 1609 | ) -> syn::Result<Option<Spanning<Self>>> { |
| 1610 | Self::parse_attrs_with(attrs, name, &()) |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | impl<L: ParseMultiple, R: ParseMultiple> ParseMultiple for Either<L, R> { |
| 1615 | fn parse_attr_with<P: Parser>( |
| 1616 | attr: &syn::Attribute, |
| 1617 | parser: &P, |
| 1618 | ) -> syn::Result<Self> { |
| 1619 | L::parse_attr_with(attr, parser) |
| 1620 | .map(Self::Left) |
| 1621 | .or_else(|_| R::parse_attr_with(attr, parser).map(Self::Right)) |
| 1622 | } |
| 1623 | |
| 1624 | fn merge_attrs( |
| 1625 | prev: Spanning<Self>, |
| 1626 | new: Spanning<Self>, |
| 1627 | name: &syn::Ident, |
| 1628 | ) -> syn::Result<Spanning<Self>> { |
| 1629 | Ok(match (prev.item, new.item) { |
| 1630 | (Self::Left(p), Self::Left(n)) => { |
| 1631 | L::merge_attrs(Spanning::new(p, prev.span), Spanning::new(n, new.span), name)? |
| 1632 | .map(Self::Left) |
| 1633 | }, |
| 1634 | (Self::Right(p), Self::Right(n)) => { |
| 1635 | R::merge_attrs(Spanning::new(p, prev.span), Spanning::new(n, new.span), name)? |
| 1636 | .map(Self::Right) |
| 1637 | }, |
| 1638 | _ => return Err(syn::Error::new( |
| 1639 | new.span, |
| 1640 | format!("only single kind of `#[ {name}(...)]` attribute is allowed here" ), |
| 1641 | )) |
| 1642 | }) |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | #[cfg (any( |
| 1647 | feature = "as_ref" , |
| 1648 | feature = "from" , |
| 1649 | feature = "into" , |
| 1650 | feature = "try_from" |
| 1651 | ))] |
| 1652 | mod empty { |
| 1653 | use syn::{ |
| 1654 | parse::{Parse, ParseStream}, |
| 1655 | spanned::Spanned as _, |
| 1656 | }; |
| 1657 | |
| 1658 | use super::{ParseMultiple, Parser, Spanning}; |
| 1659 | |
| 1660 | /// Representation of an empty attribute, containing no arguments. |
| 1661 | /// |
| 1662 | /// ```rust,ignore |
| 1663 | /// #[<attribute>] |
| 1664 | /// ``` |
| 1665 | #[derive (Clone, Copy, Debug)] |
| 1666 | pub(crate) struct Empty; |
| 1667 | |
| 1668 | impl Parse for Empty { |
| 1669 | fn parse(input: ParseStream<'_>) -> syn::Result<Self> { |
| 1670 | if input.is_empty() { |
| 1671 | Ok(Self) |
| 1672 | } else { |
| 1673 | Err(syn::Error::new( |
| 1674 | input.span(), |
| 1675 | "no attribute arguments allowed here" , |
| 1676 | )) |
| 1677 | } |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | impl ParseMultiple for Empty { |
| 1682 | fn parse_attr_with<P: Parser>( |
| 1683 | attr: &syn::Attribute, |
| 1684 | _: &P, |
| 1685 | ) -> syn::Result<Self> { |
| 1686 | if matches!(attr.meta, syn::Meta::Path(_)) { |
| 1687 | Ok(Self) |
| 1688 | } else { |
| 1689 | Err(syn::Error::new( |
| 1690 | attr.span(), |
| 1691 | "no attribute arguments allowed here" , |
| 1692 | )) |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | fn merge_attrs( |
| 1697 | _prev: Spanning<Self>, |
| 1698 | new: Spanning<Self>, |
| 1699 | name: &syn::Ident, |
| 1700 | ) -> syn::Result<Spanning<Self>> { |
| 1701 | Err(syn::Error::new( |
| 1702 | new.span, |
| 1703 | format!("only single `#[ {name}]` attribute is allowed here" ), |
| 1704 | )) |
| 1705 | } |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | #[cfg (any(feature = "as_ref" , feature = "from" ))] |
| 1710 | mod forward { |
| 1711 | use syn::{ |
| 1712 | parse::{Parse, ParseStream}, |
| 1713 | spanned::Spanned as _, |
| 1714 | }; |
| 1715 | |
| 1716 | use super::ParseMultiple; |
| 1717 | |
| 1718 | /// Representation of a `forward` attribute. |
| 1719 | /// |
| 1720 | /// ```rust,ignore |
| 1721 | /// #[<attribute>(forward)] |
| 1722 | /// ``` |
| 1723 | #[derive (Clone, Copy, Debug)] |
| 1724 | pub(crate) struct Forward; |
| 1725 | |
| 1726 | impl Parse for Forward { |
| 1727 | fn parse(input: ParseStream<'_>) -> syn::Result<Self> { |
| 1728 | match input.parse::<syn::Path>()? { |
| 1729 | p if p.is_ident("forward" ) => Ok(Self), |
| 1730 | p => Err(syn::Error::new(p.span(), "only `forward` allowed here" )), |
| 1731 | } |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | impl ParseMultiple for Forward {} |
| 1736 | } |
| 1737 | |
| 1738 | #[cfg (feature = "try_from" )] |
| 1739 | mod repr_int { |
| 1740 | use proc_macro2::Span; |
| 1741 | use syn::parse::{Parse, ParseStream}; |
| 1742 | |
| 1743 | use super::{ParseMultiple, Parser, Spanning}; |
| 1744 | |
| 1745 | /// Representation of a [`#[repr(u/i*)]` Rust attribute][0]. |
| 1746 | /// |
| 1747 | /// **NOTE**: Disregards any non-integer representation `#[repr]`s. |
| 1748 | /// |
| 1749 | /// ```rust,ignore |
| 1750 | /// #[repr(<type>)] |
| 1751 | /// ``` |
| 1752 | /// |
| 1753 | /// [0]: https://doc.rust-lang.org/reference/type-layout.html#primitive-representations |
| 1754 | #[derive (Default)] |
| 1755 | pub(crate) struct ReprInt(Option<syn::Ident>); |
| 1756 | |
| 1757 | impl ReprInt { |
| 1758 | /// Returns [`syn::Ident`] of the primitive integer type behind this [`ReprInt`] |
| 1759 | /// attribute. |
| 1760 | /// |
| 1761 | /// If there is no explicitly specified primitive integer type, then returns a |
| 1762 | /// [default `isize` discriminant][0]. |
| 1763 | /// |
| 1764 | /// [`syn::Ident`]: struct@syn::Ident |
| 1765 | /// [0]: https://doc.rust-lang.org/reference/items/enumerations.html#discriminants |
| 1766 | pub(crate) fn ty(&self) -> syn::Ident { |
| 1767 | self.0 |
| 1768 | .as_ref() |
| 1769 | .cloned() |
| 1770 | .unwrap_or_else(|| syn::Ident::new("isize" , Span::call_site())) |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | impl Parse for ReprInt { |
| 1775 | fn parse(_: ParseStream<'_>) -> syn::Result<Self> { |
| 1776 | unreachable!("call `attr::ParseMultiple::parse_attr_with()` instead" ) |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | impl ParseMultiple for ReprInt { |
| 1781 | fn parse_attr_with<P: Parser>( |
| 1782 | attr: &syn::Attribute, |
| 1783 | _: &P, |
| 1784 | ) -> syn::Result<Self> { |
| 1785 | let mut repr = None; |
| 1786 | attr.parse_nested_meta(|meta| { |
| 1787 | if let Some(ident) = meta.path.get_ident() { |
| 1788 | if matches!( |
| 1789 | ident.to_string().as_str(), |
| 1790 | "u8" | "u16" |
| 1791 | | "u32" |
| 1792 | | "u64" |
| 1793 | | "u128" |
| 1794 | | "usize" |
| 1795 | | "i8" |
| 1796 | | "i16" |
| 1797 | | "i32" |
| 1798 | | "i64" |
| 1799 | | "i128" |
| 1800 | | "isize" |
| 1801 | ) { |
| 1802 | repr = Some(ident.clone()); |
| 1803 | return Ok(()); |
| 1804 | } |
| 1805 | } |
| 1806 | // Ignore all other attributes that could have a body, e.g. `align`. |
| 1807 | _ = meta.input.parse::<proc_macro2::Group>(); |
| 1808 | Ok(()) |
| 1809 | })?; |
| 1810 | Ok(Self(repr)) |
| 1811 | } |
| 1812 | |
| 1813 | fn merge_attrs( |
| 1814 | prev: Spanning<Self>, |
| 1815 | new: Spanning<Self>, |
| 1816 | name: &syn::Ident, |
| 1817 | ) -> syn::Result<Spanning<Self>> { |
| 1818 | match (&prev.item.0, &new.item.0) { |
| 1819 | (Some(_), None) | (None, None) => Ok(prev), |
| 1820 | (None, Some(_)) => Ok(new), |
| 1821 | (Some(_), Some(_)) => Err(syn::Error::new( |
| 1822 | new.span, |
| 1823 | format!( |
| 1824 | "only single `#[{name}(u/i*)]` attribute is expected here" , |
| 1825 | ), |
| 1826 | )), |
| 1827 | } |
| 1828 | } |
| 1829 | } |
| 1830 | } |
| 1831 | |
| 1832 | #[cfg (any( |
| 1833 | feature = "as_ref" , |
| 1834 | feature = "debug" , |
| 1835 | feature = "display" , |
| 1836 | feature = "from" , |
| 1837 | feature = "into" , |
| 1838 | ))] |
| 1839 | mod skip { |
| 1840 | use syn::{ |
| 1841 | parse::{Parse, ParseStream}, |
| 1842 | spanned::Spanned as _, |
| 1843 | }; |
| 1844 | |
| 1845 | use super::{ParseMultiple, Spanning}; |
| 1846 | |
| 1847 | /// Representation of a `skip`/`ignore` attribute. |
| 1848 | /// |
| 1849 | /// ```rust,ignore |
| 1850 | /// #[<attribute>(skip)] |
| 1851 | /// #[<attribute>(ignore)] |
| 1852 | /// ``` |
| 1853 | #[derive (Clone, Copy, Debug)] |
| 1854 | pub(crate) struct Skip(&'static str); |
| 1855 | |
| 1856 | impl Parse for Skip { |
| 1857 | fn parse(content: ParseStream<'_>) -> syn::Result<Self> { |
| 1858 | match content.parse::<syn::Path>()? { |
| 1859 | p if p.is_ident("skip" ) => Ok(Self("skip" )), |
| 1860 | p if p.is_ident("ignore" ) => Ok(Self("ignore" )), |
| 1861 | p => Err(syn::Error::new( |
| 1862 | p.span(), |
| 1863 | "only `skip`/`ignore` allowed here" , |
| 1864 | )), |
| 1865 | } |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | impl Skip { |
| 1870 | /// Returns the concrete name of this attribute (`skip` or `ignore`). |
| 1871 | pub(crate) const fn name(&self) -> &'static str { |
| 1872 | self.0 |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | impl ParseMultiple for Skip { |
| 1877 | fn merge_attrs( |
| 1878 | _: Spanning<Self>, |
| 1879 | new: Spanning<Self>, |
| 1880 | name: &syn::Ident, |
| 1881 | ) -> syn::Result<Spanning<Self>> { |
| 1882 | Err(syn::Error::new( |
| 1883 | new.span, |
| 1884 | format!( |
| 1885 | "only single `#[ {name}(skip)]`/`#[ {name}(ignore)]` attribute is allowed \ |
| 1886 | here" , |
| 1887 | ), |
| 1888 | )) |
| 1889 | } |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | #[cfg (any(feature = "as_ref" , feature = "from" , feature = "try_from" ))] |
| 1894 | mod types { |
| 1895 | use syn::{ |
| 1896 | parse::{Parse, ParseStream}, |
| 1897 | punctuated::Punctuated, |
| 1898 | Token, |
| 1899 | }; |
| 1900 | |
| 1901 | use super::{ParseMultiple, Spanning}; |
| 1902 | |
| 1903 | /// Representation of an attribute, containing a comma-separated list of types. |
| 1904 | /// |
| 1905 | /// ```rust,ignore |
| 1906 | /// #[<attribute>(<types>)] |
| 1907 | /// ``` |
| 1908 | pub(crate) struct Types(pub(crate) Punctuated<syn::Type, Token![,]>); |
| 1909 | |
| 1910 | impl Parse for Types { |
| 1911 | fn parse(input: ParseStream<'_>) -> syn::Result<Self> { |
| 1912 | input |
| 1913 | .parse_terminated(syn::Type::parse, Token![,]) |
| 1914 | .map(Self) |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | impl ParseMultiple for Types { |
| 1919 | fn merge_attrs( |
| 1920 | mut prev: Spanning<Self>, |
| 1921 | new: Spanning<Self>, |
| 1922 | _: &syn::Ident, |
| 1923 | ) -> syn::Result<Spanning<Self>> { |
| 1924 | prev.item.0.extend(new.item.0); |
| 1925 | Ok(Spanning::new( |
| 1926 | prev.item, |
| 1927 | prev.span.join(new.span).unwrap_or(prev.span), |
| 1928 | )) |
| 1929 | } |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | #[cfg (any(feature = "as_ref" , feature = "from" ))] |
| 1934 | mod conversion { |
| 1935 | use syn::parse::{Parse, ParseStream}; |
| 1936 | |
| 1937 | use crate::utils::attr; |
| 1938 | |
| 1939 | use super::{Either, ParseMultiple, Spanning}; |
| 1940 | |
| 1941 | /// Untyped analogue of a [`Conversion`], recreating its type structure via [`Either`]. |
| 1942 | /// |
| 1943 | /// Used to piggyback [`Parse`] and [`ParseMultiple`] impls to [`Either`]. |
| 1944 | type Untyped = Either<attr::Forward, attr::Types>; |
| 1945 | |
| 1946 | /// Representation of an attribute, specifying which conversions should be generated: |
| 1947 | /// either forwarded via a blanket impl, or direct for concrete specified types. |
| 1948 | /// |
| 1949 | /// ```rust,ignore |
| 1950 | /// #[<attribute>(forward)] |
| 1951 | /// #[<attribute>(<types>)] |
| 1952 | /// ``` |
| 1953 | pub(crate) enum Conversion { |
| 1954 | Forward(attr::Forward), |
| 1955 | Types(attr::Types), |
| 1956 | } |
| 1957 | |
| 1958 | impl From<Untyped> for Conversion { |
| 1959 | fn from(v: Untyped) -> Self { |
| 1960 | match v { |
| 1961 | Untyped::Left(f) => Self::Forward(f), |
| 1962 | Untyped::Right(t) => Self::Types(t), |
| 1963 | } |
| 1964 | } |
| 1965 | } |
| 1966 | impl From<Conversion> for Untyped { |
| 1967 | fn from(v: Conversion) -> Self { |
| 1968 | match v { |
| 1969 | Conversion::Forward(f) => Self::Left(f), |
| 1970 | Conversion::Types(t) => Self::Right(t), |
| 1971 | } |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | impl Parse for Conversion { |
| 1976 | fn parse(input: ParseStream<'_>) -> syn::Result<Self> { |
| 1977 | Untyped::parse(input).map(Self::from) |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | impl ParseMultiple for Conversion { |
| 1982 | fn parse_attr_with<P: attr::Parser>( |
| 1983 | attr: &syn::Attribute, |
| 1984 | parser: &P, |
| 1985 | ) -> syn::Result<Self> { |
| 1986 | Untyped::parse_attr_with(attr, parser).map(Self::from) |
| 1987 | } |
| 1988 | |
| 1989 | fn merge_attrs( |
| 1990 | prev: Spanning<Self>, |
| 1991 | new: Spanning<Self>, |
| 1992 | name: &syn::Ident, |
| 1993 | ) -> syn::Result<Spanning<Self>> { |
| 1994 | Untyped::merge_attrs(prev.map(Into::into), new.map(Into::into), name) |
| 1995 | .map(|v| v.map(Self::from)) |
| 1996 | } |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | #[cfg (any(feature = "as_ref" , feature = "from" ))] |
| 2001 | mod field_conversion { |
| 2002 | use syn::parse::{Parse, ParseStream}; |
| 2003 | |
| 2004 | use crate::utils::attr; |
| 2005 | |
| 2006 | use super::{Either, ParseMultiple, Spanning}; |
| 2007 | |
| 2008 | /// Untyped analogue of a [`FieldConversion`], recreating its type structure via [`Either`]. |
| 2009 | /// |
| 2010 | /// Used to piggyback [`Parse`] and [`ParseMultiple`] impls to [`Either`]. |
| 2011 | type Untyped = |
| 2012 | Either<attr::Empty, Either<attr::Skip, Either<attr::Forward, attr::Types>>>; |
| 2013 | |
| 2014 | /// Representation of an attribute, specifying which conversions should be generated: |
| 2015 | /// either forwarded via a blanket impl, or direct for concrete specified types. |
| 2016 | /// |
| 2017 | /// ```rust,ignore |
| 2018 | /// #[<attribute>] |
| 2019 | /// #[<attribute>(skip)] #[<attribute>(ignore)] |
| 2020 | /// #[<attribute>(forward)] |
| 2021 | /// #[<attribute>(<types>)] |
| 2022 | /// ``` |
| 2023 | pub(crate) enum FieldConversion { |
| 2024 | Empty(attr::Empty), |
| 2025 | Skip(attr::Skip), |
| 2026 | Forward(attr::Forward), |
| 2027 | Types(attr::Types), |
| 2028 | } |
| 2029 | |
| 2030 | impl From<Untyped> for FieldConversion { |
| 2031 | fn from(v: Untyped) -> Self { |
| 2032 | match v { |
| 2033 | Untyped::Left(e) => Self::Empty(e), |
| 2034 | Untyped::Right(Either::Left(s)) => Self::Skip(s), |
| 2035 | Untyped::Right(Either::Right(Either::Left(f))) => Self::Forward(f), |
| 2036 | Untyped::Right(Either::Right(Either::Right(t))) => Self::Types(t), |
| 2037 | } |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | impl From<FieldConversion> for Untyped { |
| 2042 | fn from(v: FieldConversion) -> Self { |
| 2043 | match v { |
| 2044 | FieldConversion::Empty(e) => Self::Left(e), |
| 2045 | FieldConversion::Skip(s) => Self::Right(Either::Left(s)), |
| 2046 | FieldConversion::Forward(f) => { |
| 2047 | Self::Right(Either::Right(Either::Left(f))) |
| 2048 | } |
| 2049 | FieldConversion::Types(t) => { |
| 2050 | Self::Right(Either::Right(Either::Right(t))) |
| 2051 | } |
| 2052 | } |
| 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | impl From<attr::Conversion> for FieldConversion { |
| 2057 | fn from(v: attr::Conversion) -> Self { |
| 2058 | match v { |
| 2059 | attr::Conversion::Forward(f) => Self::Forward(f), |
| 2060 | attr::Conversion::Types(t) => Self::Types(t), |
| 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | impl From<FieldConversion> for Option<attr::Conversion> { |
| 2066 | fn from(v: FieldConversion) -> Self { |
| 2067 | match v { |
| 2068 | FieldConversion::Forward(f) => Some(attr::Conversion::Forward(f)), |
| 2069 | FieldConversion::Types(t) => Some(attr::Conversion::Types(t)), |
| 2070 | FieldConversion::Empty(_) | FieldConversion::Skip(_) => None, |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | impl Parse for FieldConversion { |
| 2076 | fn parse(input: ParseStream<'_>) -> syn::Result<Self> { |
| 2077 | Untyped::parse(input).map(Self::from) |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | impl ParseMultiple for FieldConversion { |
| 2082 | fn parse_attr_with<P: attr::Parser>( |
| 2083 | attr: &syn::Attribute, |
| 2084 | parser: &P, |
| 2085 | ) -> syn::Result<Self> { |
| 2086 | Untyped::parse_attr_with(attr, parser).map(Self::from) |
| 2087 | } |
| 2088 | |
| 2089 | fn merge_attrs( |
| 2090 | prev: Spanning<Self>, |
| 2091 | new: Spanning<Self>, |
| 2092 | name: &syn::Ident, |
| 2093 | ) -> syn::Result<Spanning<Self>> { |
| 2094 | Untyped::merge_attrs(prev.map(Into::into), new.map(Into::into), name) |
| 2095 | .map(|v| v.map(Self::from)) |
| 2096 | } |
| 2097 | } |
| 2098 | } |
| 2099 | |
| 2100 | #[cfg (feature = "try_from" )] |
| 2101 | mod repr_conversion { |
| 2102 | use syn::parse::{Parse, ParseStream}; |
| 2103 | |
| 2104 | use crate::utils::attr; |
| 2105 | |
| 2106 | use super::{ParseMultiple, Spanning}; |
| 2107 | |
| 2108 | /// Representation of an attribute, specifying which `repr`-conversions should be generated: |
| 2109 | /// either direct into a discriminant, or for concrete specified types forwarding from a |
| 2110 | /// discriminant. |
| 2111 | /// |
| 2112 | /// ```rust,ignore |
| 2113 | /// #[<attribute>(repr)] |
| 2114 | /// #[<attribute>(repr(<types>))] |
| 2115 | /// ``` |
| 2116 | pub(crate) enum ReprConversion { |
| 2117 | Discriminant(attr::Empty), |
| 2118 | Types(attr::Types), |
| 2119 | } |
| 2120 | |
| 2121 | impl Parse for ReprConversion { |
| 2122 | fn parse(input: ParseStream<'_>) -> syn::Result<Self> { |
| 2123 | let prefix = syn::Ident::parse(input)?; |
| 2124 | if prefix != "repr" { |
| 2125 | return Err(syn::Error::new( |
| 2126 | prefix.span(), |
| 2127 | "expected `repr` argument here" , |
| 2128 | )); |
| 2129 | } |
| 2130 | if input.is_empty() { |
| 2131 | Ok(Self::Discriminant(attr::Empty)) |
| 2132 | } else { |
| 2133 | let inner; |
| 2134 | syn::parenthesized!(inner in input); |
| 2135 | Ok(Self::Types(attr::Types::parse(&inner)?)) |
| 2136 | } |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | impl ParseMultiple for ReprConversion { |
| 2141 | fn merge_attrs( |
| 2142 | prev: Spanning<Self>, |
| 2143 | new: Spanning<Self>, |
| 2144 | name: &syn::Ident, |
| 2145 | ) -> syn::Result<Spanning<Self>> { |
| 2146 | Ok(match (prev.item, new.item) { |
| 2147 | (Self::Discriminant(_), Self::Discriminant(_)) => { |
| 2148 | return Err(syn::Error::new( |
| 2149 | new.span, |
| 2150 | format!("only single `#[{name}(repr)]` attribute is allowed here" ), |
| 2151 | )) |
| 2152 | }, |
| 2153 | (Self::Types(p), Self::Types(n)) => { |
| 2154 | attr::Types::merge_attrs( |
| 2155 | Spanning::new(p, prev.span), |
| 2156 | Spanning::new(n, new.span), |
| 2157 | name, |
| 2158 | )?.map(Self::Types) |
| 2159 | }, |
| 2160 | _ => return Err(syn::Error::new( |
| 2161 | new.span, |
| 2162 | format!( |
| 2163 | "only single kind of `#[{name}(repr(...))]` attribute is allowed here" , |
| 2164 | ), |
| 2165 | )) |
| 2166 | }) |
| 2167 | } |
| 2168 | } |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | #[cfg (any(feature = "from" , feature = "into" ))] |
| 2173 | mod fields_ext { |
| 2174 | use std::{cmp, iter}; |
| 2175 | |
| 2176 | use quote::ToTokens as _; |
| 2177 | use syn::{punctuated, spanned::Spanned as _}; |
| 2178 | |
| 2179 | use super::Either; |
| 2180 | |
| 2181 | /// Abstraction over `.len()` method to use it on type parameters. |
| 2182 | pub(crate) trait Len { |
| 2183 | /// Returns number of fields. |
| 2184 | fn len(&self) -> usize; |
| 2185 | } |
| 2186 | |
| 2187 | impl Len for syn::Fields { |
| 2188 | fn len(&self) -> usize { |
| 2189 | self.len() |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | impl<T> Len for [T] { |
| 2194 | fn len(&self) -> usize { |
| 2195 | self.len() |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | /// [`syn::Fields`] extension. |
| 2200 | pub(crate) trait FieldsExt: Len { |
| 2201 | /// Validates the provided [`syn::Type`] against these [`syn::Fields`]. |
| 2202 | fn validate_type<'t>( |
| 2203 | &self, |
| 2204 | ty: &'t syn::Type, |
| 2205 | ) -> syn::Result< |
| 2206 | Either<punctuated::Iter<'t, syn::Type>, iter::Once<&'t syn::Type>>, |
| 2207 | > { |
| 2208 | match ty { |
| 2209 | syn::Type::Tuple(syn::TypeTuple { elems, .. }) if self.len() > 1 => { |
| 2210 | match self.len().cmp(&elems.len()) { |
| 2211 | cmp::Ordering::Greater => { |
| 2212 | return Err(syn::Error::new( |
| 2213 | ty.span(), |
| 2214 | format!( |
| 2215 | "wrong tuple length: expected {}, found {}. \ |
| 2216 | Consider adding {} more type {}: `( {})`" , |
| 2217 | self.len(), |
| 2218 | elems.len(), |
| 2219 | self.len() - elems.len(), |
| 2220 | if self.len() - elems.len() > 1 { |
| 2221 | "s" |
| 2222 | } else { |
| 2223 | "" |
| 2224 | }, |
| 2225 | elems |
| 2226 | .iter() |
| 2227 | .map(|ty| ty.into_token_stream().to_string()) |
| 2228 | .chain( |
| 2229 | (0..(self.len() - elems.len())) |
| 2230 | .map(|_| "_" .to_string()) |
| 2231 | ) |
| 2232 | .collect::<Vec<_>>() |
| 2233 | .join(", " ), |
| 2234 | ), |
| 2235 | )); |
| 2236 | } |
| 2237 | cmp::Ordering::Less => { |
| 2238 | return Err(syn::Error::new( |
| 2239 | ty.span(), |
| 2240 | format!( |
| 2241 | "wrong tuple length: expected {}, found {}. \ |
| 2242 | Consider removing last {} type {}: `( {})`" , |
| 2243 | self.len(), |
| 2244 | elems.len(), |
| 2245 | elems.len() - self.len(), |
| 2246 | if elems.len() - self.len() > 1 { |
| 2247 | "s" |
| 2248 | } else { |
| 2249 | "" |
| 2250 | }, |
| 2251 | elems |
| 2252 | .iter() |
| 2253 | .take(self.len()) |
| 2254 | .map(|ty| ty.into_token_stream().to_string()) |
| 2255 | .collect::<Vec<_>>() |
| 2256 | .join(", " ), |
| 2257 | ), |
| 2258 | )); |
| 2259 | } |
| 2260 | cmp::Ordering::Equal => {} |
| 2261 | } |
| 2262 | } |
| 2263 | other if self.len() > 1 => { |
| 2264 | return Err(syn::Error::new( |
| 2265 | other.span(), |
| 2266 | format!( |
| 2267 | "expected tuple: `( {}, {})`" , |
| 2268 | other.into_token_stream(), |
| 2269 | (0..(self.len() - 1)) |
| 2270 | .map(|_| "_" ) |
| 2271 | .collect::<Vec<_>>() |
| 2272 | .join(", " ), |
| 2273 | ), |
| 2274 | )); |
| 2275 | } |
| 2276 | _ => {} |
| 2277 | } |
| 2278 | Ok(match ty { |
| 2279 | syn::Type::Tuple(syn::TypeTuple { elems, .. }) => { |
| 2280 | Either::Left(elems.iter()) |
| 2281 | } |
| 2282 | other => Either::Right(iter::once(other)), |
| 2283 | }) |
| 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | impl<T: Len + ?Sized> FieldsExt for T {} |
| 2288 | } |
| 2289 | |
| 2290 | #[cfg (feature = "as_ref" )] |
| 2291 | mod generics_search { |
| 2292 | use syn::visit::Visit; |
| 2293 | |
| 2294 | use super::HashSet; |
| 2295 | |
| 2296 | /// Search of whether some generics (type parameters, lifetime parameters or const parameters) |
| 2297 | /// are present in some [`syn::Type`]. |
| 2298 | pub(crate) struct GenericsSearch<'s> { |
| 2299 | /// Type parameters to look for. |
| 2300 | pub(crate) types: HashSet<&'s syn::Ident>, |
| 2301 | |
| 2302 | /// Lifetime parameters to look for. |
| 2303 | pub(crate) lifetimes: HashSet<&'s syn::Ident>, |
| 2304 | |
| 2305 | /// Const parameters to look for. |
| 2306 | pub(crate) consts: HashSet<&'s syn::Ident>, |
| 2307 | } |
| 2308 | |
| 2309 | impl GenericsSearch<'_> { |
| 2310 | /// Checks the provided [`syn::Type`] to contain anything from this [`GenericsSearch`]. |
| 2311 | pub(crate) fn any_in(&self, ty: &syn::Type) -> bool { |
| 2312 | let mut visitor = Visitor { |
| 2313 | search: self, |
| 2314 | found: false, |
| 2315 | }; |
| 2316 | visitor.visit_type(ty); |
| 2317 | visitor.found |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | /// [`Visit`]or performing a [`GenericsSearch`]. |
| 2322 | struct Visitor<'s> { |
| 2323 | /// [`GenericsSearch`] parameters. |
| 2324 | search: &'s GenericsSearch<'s>, |
| 2325 | |
| 2326 | /// Indication whether anything was found for the [`GenericsSearch`] parameters. |
| 2327 | found: bool, |
| 2328 | } |
| 2329 | |
| 2330 | impl<'ast> Visit<'ast> for Visitor<'_> { |
| 2331 | fn visit_type_path(&mut self, tp: &'ast syn::TypePath) { |
| 2332 | self.found |= tp.path.get_ident().is_some_and(|ident| { |
| 2333 | self.search.types.contains(ident) || self.search.consts.contains(ident) |
| 2334 | }); |
| 2335 | |
| 2336 | syn::visit::visit_type_path(self, tp) |
| 2337 | } |
| 2338 | |
| 2339 | fn visit_lifetime(&mut self, lf: &'ast syn::Lifetime) { |
| 2340 | self.found |= self.search.lifetimes.contains(&lf.ident); |
| 2341 | |
| 2342 | syn::visit::visit_lifetime(self, lf) |
| 2343 | } |
| 2344 | |
| 2345 | fn visit_expr_path(&mut self, ep: &'ast syn::ExprPath) { |
| 2346 | self.found |= ep |
| 2347 | .path |
| 2348 | .get_ident() |
| 2349 | .is_some_and(|ident| self.search.consts.contains(ident)); |
| 2350 | |
| 2351 | syn::visit::visit_expr_path(self, ep) |
| 2352 | } |
| 2353 | } |
| 2354 | } |
| 2355 | |