|
1 | 1 | //! [`Attribute`] parsing for items. |
2 | 2 |
|
3 | | -use std::{borrow::Cow, ops::Deref}; |
| 3 | +use std::borrow::Cow; |
4 | 4 |
|
5 | 5 | use proc_macro2::Span; |
6 | 6 | use syn::{ |
7 | 7 | parse::{discouraged::Speculative, Parse, ParseStream}, |
8 | 8 | punctuated::Punctuated, |
9 | 9 | spanned::Spanned, |
10 | | - Attribute, BoundLifetimes, Data, Ident, Meta, Path, PredicateType, Result, Token, TraitBound, |
11 | | - TraitBoundModifier, Type, TypeParamBound, TypePath, WhereClause, WherePredicate, |
| 10 | + Attribute, BoundLifetimes, Data, Ident, Meta, PredicateType, Result, Token, Type, TypePath, |
| 11 | + WhereClause, WherePredicate, |
12 | 12 | }; |
13 | 13 |
|
14 | | -use crate::{ |
15 | | - util::{self, MetaListExt}, |
16 | | - Error, Incomparable, Item, Skip, SkipGroup, Trait, TraitImpl, DERIVE_WHERE, |
17 | | -}; |
| 14 | +use crate::{trait_::DeriveTrait, Error, Incomparable, Item, Skip, SkipGroup, Trait, DERIVE_WHERE}; |
18 | 15 |
|
19 | 16 | /// Attributes on item. |
20 | 17 | #[derive(Default)] |
@@ -259,7 +256,7 @@ impl DeriveWhere { |
259 | 256 | pub fn any_skip(&self) -> bool { |
260 | 257 | self.traits |
261 | 258 | .iter() |
262 | | - .any(|trait_| SkipGroup::trait_supported_by_skip_all(**trait_)) |
| 259 | + .any(|trait_| SkipGroup::trait_supported_by_skip_all(***trait_)) |
263 | 260 | } |
264 | 261 |
|
265 | 262 | /// Create [`WhereClause`] for the given parameters. |
@@ -353,172 +350,3 @@ impl Parse for Generic { |
353 | 350 | } |
354 | 351 | } |
355 | 352 | } |
356 | | - |
357 | | -/// Trait to implement. |
358 | | -#[derive(Eq, PartialEq)] |
359 | | -pub enum DeriveTrait { |
360 | | - /// [`Clone`]. |
361 | | - Clone, |
362 | | - /// [`Copy`]. |
363 | | - Copy, |
364 | | - /// [`Debug`](std::fmt::Debug). |
365 | | - Debug, |
366 | | - /// [`Default`]. |
367 | | - Default, |
368 | | - /// [`Eq`]. |
369 | | - Eq, |
370 | | - /// [`Hash`](std::hash::Hash). |
371 | | - Hash, |
372 | | - /// [`Ord`]. |
373 | | - Ord, |
374 | | - /// [`PartialEq`]. |
375 | | - PartialEq, |
376 | | - /// [`PartialOrd`]. |
377 | | - PartialOrd, |
378 | | - /// [`Zeroize`](https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html). |
379 | | - #[cfg(feature = "zeroize")] |
380 | | - Zeroize { |
381 | | - /// [`Zeroize`](https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html) path. |
382 | | - crate_: Option<Path>, |
383 | | - }, |
384 | | - /// [`ZeroizeOnDrop`](https://docs.rs/zeroize/latest/zeroize/trait.ZeroizeOnDrop.html). |
385 | | - #[cfg(feature = "zeroize")] |
386 | | - ZeroizeOnDrop { |
387 | | - /// [`ZeroizeOnDrop`](https://docs.rs/zeroize/latest/zeroize/trait.ZeroizeOnDrop.html) path. |
388 | | - crate_: Option<Path>, |
389 | | - }, |
390 | | -} |
391 | | - |
392 | | -impl Deref for DeriveTrait { |
393 | | - type Target = Trait; |
394 | | - |
395 | | - fn deref(&self) -> &Self::Target { |
396 | | - use DeriveTrait::*; |
397 | | - |
398 | | - match self { |
399 | | - Clone => &Trait::Clone, |
400 | | - Copy => &Trait::Copy, |
401 | | - Debug => &Trait::Debug, |
402 | | - Default => &Trait::Default, |
403 | | - Eq => &Trait::Eq, |
404 | | - Hash => &Trait::Hash, |
405 | | - Ord => &Trait::Ord, |
406 | | - PartialEq => &Trait::PartialEq, |
407 | | - PartialOrd => &Trait::PartialOrd, |
408 | | - #[cfg(feature = "zeroize")] |
409 | | - Zeroize { .. } => &Trait::Zeroize, |
410 | | - #[cfg(feature = "zeroize")] |
411 | | - ZeroizeOnDrop { .. } => &Trait::ZeroizeOnDrop, |
412 | | - } |
413 | | - } |
414 | | -} |
415 | | - |
416 | | -impl PartialEq<Trait> for &DeriveTrait { |
417 | | - fn eq(&self, other: &Trait) -> bool { |
418 | | - let trait_: &Trait = self; |
419 | | - trait_ == other |
420 | | - } |
421 | | -} |
422 | | - |
423 | | -impl DeriveTrait { |
424 | | - /// Returns fully qualified [`Path`] for this trait. |
425 | | - pub fn path(&self) -> Path { |
426 | | - use DeriveTrait::*; |
427 | | - |
428 | | - match self { |
429 | | - Clone => util::path_from_root_and_strs(self.crate_(), &["clone", "Clone"]), |
430 | | - Copy => util::path_from_root_and_strs(self.crate_(), &["marker", "Copy"]), |
431 | | - Debug => util::path_from_root_and_strs(self.crate_(), &["fmt", "Debug"]), |
432 | | - Default => util::path_from_root_and_strs(self.crate_(), &["default", "Default"]), |
433 | | - Eq => util::path_from_root_and_strs(self.crate_(), &["cmp", "Eq"]), |
434 | | - Hash => util::path_from_root_and_strs(self.crate_(), &["hash", "Hash"]), |
435 | | - Ord => util::path_from_root_and_strs(self.crate_(), &["cmp", "Ord"]), |
436 | | - PartialEq => util::path_from_root_and_strs(self.crate_(), &["cmp", "PartialEq"]), |
437 | | - PartialOrd => util::path_from_root_and_strs(self.crate_(), &["cmp", "PartialOrd"]), |
438 | | - #[cfg(feature = "zeroize")] |
439 | | - Zeroize { .. } => util::path_from_root_and_strs(self.crate_(), &["Zeroize"]), |
440 | | - #[cfg(feature = "zeroize")] |
441 | | - ZeroizeOnDrop { .. } => util::path_from_root_and_strs(self.crate_(), &["ZeroizeOnDrop"]), |
442 | | - } |
443 | | - } |
444 | | - |
445 | | - /// Returns the path to the root crate for this trait. |
446 | | - pub fn crate_(&self) -> Path { |
447 | | - use DeriveTrait::*; |
448 | | - |
449 | | - match self { |
450 | | - Clone => util::path_from_strs(&["core"]), |
451 | | - Copy => util::path_from_strs(&["core"]), |
452 | | - Debug => util::path_from_strs(&["core"]), |
453 | | - Default => util::path_from_strs(&["core"]), |
454 | | - Eq => util::path_from_strs(&["core"]), |
455 | | - Hash => util::path_from_strs(&["core"]), |
456 | | - Ord => util::path_from_strs(&["core"]), |
457 | | - PartialEq => util::path_from_strs(&["core"]), |
458 | | - PartialOrd => util::path_from_strs(&["core"]), |
459 | | - #[cfg(feature = "zeroize")] |
460 | | - Zeroize { crate_, .. } => { |
461 | | - if let Some(crate_) = crate_ { |
462 | | - crate_.clone() |
463 | | - } else { |
464 | | - util::path_from_strs(&["zeroize"]) |
465 | | - } |
466 | | - } |
467 | | - #[cfg(feature = "zeroize")] |
468 | | - ZeroizeOnDrop { crate_, .. } => { |
469 | | - if let Some(crate_) = crate_ { |
470 | | - crate_.clone() |
471 | | - } else { |
472 | | - util::path_from_strs(&["zeroize"]) |
473 | | - } |
474 | | - } |
475 | | - } |
476 | | - } |
477 | | - |
478 | | - /// Returns where-clause bounds for the trait in respect of the item type. |
479 | | - fn where_bounds(&self, data: &Item) -> Punctuated<TypeParamBound, Token![+]> { |
480 | | - let mut list = Punctuated::new(); |
481 | | - |
482 | | - list.push(TypeParamBound::Trait(TraitBound { |
483 | | - paren_token: None, |
484 | | - modifier: TraitBoundModifier::None, |
485 | | - lifetimes: None, |
486 | | - path: self.path(), |
487 | | - })); |
488 | | - |
489 | | - // Add bounds specific to the trait. |
490 | | - if let Some(bound) = self.additional_where_bounds(data) { |
491 | | - list.push(bound) |
492 | | - } |
493 | | - |
494 | | - list |
495 | | - } |
496 | | - |
497 | | - /// Create [`DeriveTrait`] from [`ParseStream`]. |
498 | | - fn from_stream(span: Span, data: &Data, input: ParseStream) -> Result<(Span, Self)> { |
499 | | - match Meta::parse(input) { |
500 | | - Ok(meta) => { |
501 | | - let trait_ = Trait::from_path(meta.path())?; |
502 | | - |
503 | | - if let Data::Union(_) = data { |
504 | | - // Make sure this `Trait` supports unions. |
505 | | - if !trait_.supports_union() { |
506 | | - return Err(Error::union(span)); |
507 | | - } |
508 | | - } |
509 | | - |
510 | | - match &meta { |
511 | | - Meta::Path(path) => Ok((path.span(), trait_.default_derive_trait())), |
512 | | - Meta::List(list) => { |
513 | | - let nested = list.parse_non_empty_nested_metas()?; |
514 | | - |
515 | | - // This will return an error if no options are supported. |
516 | | - Ok((list.span(), trait_.parse_derive_trait(meta.span(), nested)?)) |
517 | | - } |
518 | | - Meta::NameValue(name_value) => Err(Error::option_syntax(name_value.span())), |
519 | | - } |
520 | | - } |
521 | | - Err(error) => Err(Error::trait_syntax(error.span())), |
522 | | - } |
523 | | - } |
524 | | -} |
0 commit comments