@@ -9,6 +9,42 @@ pub(crate) mod attributes {
9
9
use proc_macro2:: { Ident , Span , TokenStream } ;
10
10
use std:: { borrow:: Cow , str:: FromStr } ;
11
11
12
+ /// Attributes which may be added ontop of items
13
+ #[ derive( Clone , Debug ) ]
14
+ pub enum Attribute {
15
+ /// `#[derive($inner)]`
16
+ Derives ( Vec < String > ) ,
17
+ /// `#[inline]`
18
+ Inline ,
19
+ /// `#[must_use]`
20
+ MustUse ,
21
+ /// `#[doc($inner)]`
22
+ Doc ( String ) ,
23
+ /// `#[cfg($inner)`
24
+ Cfg ( String ) ,
25
+ /// `#[cfg_attr($inner)`
26
+ CfgAttr ( String ) ,
27
+ /// `#[deprecated]` or `#[deprecated($inner)]`
28
+ Deprecated ( Option < String > ) ,
29
+ }
30
+
31
+ impl Attribute {
32
+ pub ( crate ) fn to_tokenstream ( & self ) -> TokenStream {
33
+ match self {
34
+ Attribute :: Derives ( d) => {
35
+ let tmp: Vec < & str > = d. iter ( ) . map ( String :: as_str) . collect ( ) ;
36
+ derives ( & tmp)
37
+ }
38
+ Attribute :: Inline => inline ( ) ,
39
+ Attribute :: MustUse => must_use ( ) ,
40
+ Attribute :: Doc ( comment) => doc ( & comment) ,
41
+ Attribute :: Cfg ( condition) => cfg ( & condition) ,
42
+ Attribute :: CfgAttr ( inner) => cfg_attr ( & inner) ,
43
+ Attribute :: Deprecated ( inner) => deprecated ( inner. clone ( ) ) ,
44
+ }
45
+ }
46
+ }
47
+
12
48
pub ( crate ) fn repr ( which : & str ) -> TokenStream {
13
49
let which = Ident :: new ( which, Span :: call_site ( ) ) ;
14
50
quote ! {
@@ -60,6 +96,28 @@ pub(crate) mod attributes {
60
96
}
61
97
}
62
98
99
+ pub ( crate ) fn cfg ( cfg : & str ) -> TokenStream {
100
+ let raw_cfg = TokenStream :: from_str ( cfg) . expect ( "cfg to be valid" ) ;
101
+ quote ! ( #[ cfg( #raw_cfg) ] )
102
+ }
103
+
104
+ pub ( crate ) fn cfg_attr ( cfg : & str ) -> TokenStream {
105
+ let raw_cfg = TokenStream :: from_str ( cfg) . expect ( "cfg to be valid" ) ;
106
+ quote ! ( #[ cfg_attr( #raw_cfg) ] )
107
+ }
108
+
109
+ pub ( crate ) fn deprecated ( inner : Option < String > ) -> TokenStream {
110
+ if let Some ( inner) = inner {
111
+ let raw =
112
+ TokenStream :: from_str ( inner. as_str ( ) ) . expect ( "cfg to be valid" ) ;
113
+ quote ! {
114
+ #[ deprecated( #raw) ]
115
+ }
116
+ } else {
117
+ quote ! ( #[ deprecated] )
118
+ }
119
+ }
120
+
63
121
pub ( crate ) fn link_name < const MANGLE : bool > ( name : & str ) -> TokenStream {
64
122
// LLVM mangles the name by default but it's already mangled.
65
123
// Prefixing the name with \u{1} should tell LLVM to not mangle it.
0 commit comments