cxx_build/syntax/
derive.rs1use proc_macro2::{Ident, Span};
2use std::fmt::{self, Display};
3
4#[derive(#[automatically_derived]
impl ::core::marker::Copy for Derive { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Derive {
#[inline]
fn clone(&self) -> Derive {
let _: ::core::clone::AssertParamIsClone<Trait>;
let _: ::core::clone::AssertParamIsClone<Span>;
*self
}
}Clone)]
5pub(crate) struct Derive {
6 pub what: Trait,
7 pub span: Span,
8}
9
10#[derive(#[automatically_derived]
impl ::core::marker::Copy for Trait { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Trait {
#[inline]
fn clone(&self) -> Trait { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for Trait {
#[inline]
fn eq(&self, other: &Trait) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
11pub(crate) enum Trait {
12 BitAnd,
13 BitOr,
14 BitXor,
15 Clone,
16 Copy,
17 Debug,
18 Default,
19 Eq,
20 ExternType,
21 Hash,
22 Ord,
23 PartialEq,
24 PartialOrd,
25 Serialize,
26 Deserialize,
27}
28
29impl Derive {
30 pub(crate) fn from(ident: &Ident) -> Option<Self> {
31 let what = match ident.to_string().as_str() {
32 "BitAnd" => Trait::BitAnd,
33 "BitOr" => Trait::BitOr,
34 "BitXor" => Trait::BitXor,
35 "Clone" => Trait::Clone,
36 "Copy" => Trait::Copy,
37 "Debug" => Trait::Debug,
38 "Default" => Trait::Default,
39 "Eq" => Trait::Eq,
40 "ExternType" => Trait::ExternType,
41 "Hash" => Trait::Hash,
42 "Ord" => Trait::Ord,
43 "PartialEq" => Trait::PartialEq,
44 "PartialOrd" => Trait::PartialOrd,
45 "Serialize" => Trait::Serialize,
46 "Deserialize" => Trait::Deserialize,
47 _ => return None,
48 };
49 let span = ident.span();
50 Some(Derive { what, span })
51 }
52}
53
54impl PartialEq<Trait> for Derive {
55 fn eq(&self, other: &Trait) -> bool {
56 self.what == *other
57 }
58}
59
60impl AsRef<str> for Trait {
61 fn as_ref(&self) -> &str {
62 match self {
63 Trait::BitAnd => "BitAnd",
64 Trait::BitOr => "BitOr",
65 Trait::BitXor => "BitXor",
66 Trait::Clone => "Clone",
67 Trait::Copy => "Copy",
68 Trait::Debug => "Debug",
69 Trait::Default => "Default",
70 Trait::Eq => "Eq",
71 Trait::ExternType => "ExternType",
72 Trait::Hash => "Hash",
73 Trait::Ord => "Ord",
74 Trait::PartialEq => "PartialEq",
75 Trait::PartialOrd => "PartialOrd",
76 Trait::Serialize => "Serialize",
77 Trait::Deserialize => "Deserialize",
78 }
79 }
80}
81
82impl Display for Derive {
83 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
84 formatter.write_str(self.what.as_ref())
85 }
86}
87
88pub(crate) fn contains(derives: &[Derive], query: Trait) -> bool {
89 derives.iter().any(|derive| derive.what == query)
90}