@@ -12,6 +12,7 @@ pub use self::SyntaxExtension::*;
12
12
13
13
use ast;
14
14
use ast:: { Name , PatKind } ;
15
+ use attr:: HasAttrs ;
15
16
use codemap;
16
17
use codemap:: { CodeMap , Span , ExpnId , ExpnInfo , NO_EXPANSION } ;
17
18
use errors:: DiagnosticBuilder ;
@@ -41,29 +42,31 @@ pub enum Annotatable {
41
42
ImplItem ( P < ast:: ImplItem > ) ,
42
43
}
43
44
44
- impl Annotatable {
45
- pub fn attrs ( & self ) -> & [ ast:: Attribute ] {
45
+ impl HasAttrs for Annotatable {
46
+ fn attrs ( & self ) -> & [ ast:: Attribute ] {
46
47
match * self {
47
- Annotatable :: Item ( ref i ) => & i . attrs ,
48
- Annotatable :: TraitItem ( ref ti ) => & ti . attrs ,
49
- Annotatable :: ImplItem ( ref ii ) => & ii . attrs ,
48
+ Annotatable :: Item ( ref item ) => & item . attrs ,
49
+ Annotatable :: TraitItem ( ref trait_item ) => & trait_item . attrs ,
50
+ Annotatable :: ImplItem ( ref impl_item ) => & impl_item . attrs ,
50
51
}
51
52
}
52
53
53
- pub fn fold_attrs ( self , attrs : Vec < ast:: Attribute > ) -> Annotatable {
54
+ fn map_attrs < F : FnOnce ( Vec < ast :: Attribute > ) -> Vec < ast:: Attribute > > ( self , f : F ) -> Self {
54
55
match self {
55
- Annotatable :: Item ( i) => Annotatable :: Item ( i. map ( |i| ast:: Item {
56
- attrs : attrs,
57
- ..i
58
- } ) ) ,
59
- Annotatable :: TraitItem ( i) => Annotatable :: TraitItem ( i. map ( |ti| {
60
- ast:: TraitItem { attrs : attrs, ..ti }
61
- } ) ) ,
62
- Annotatable :: ImplItem ( i) => Annotatable :: ImplItem ( i. map ( |ii| {
63
- ast:: ImplItem { attrs : attrs, ..ii }
64
- } ) ) ,
56
+ Annotatable :: Item ( item) => Annotatable :: Item ( item. map_attrs ( f) ) ,
57
+ Annotatable :: TraitItem ( trait_item) => Annotatable :: TraitItem ( trait_item. map_attrs ( f) ) ,
58
+ Annotatable :: ImplItem ( impl_item) => Annotatable :: ImplItem ( impl_item. map_attrs ( f) ) ,
65
59
}
66
60
}
61
+ }
62
+
63
+ impl Annotatable {
64
+ pub fn attrs ( & self ) -> & [ ast:: Attribute ] {
65
+ HasAttrs :: attrs ( self )
66
+ }
67
+ pub fn fold_attrs ( self , attrs : Vec < ast:: Attribute > ) -> Annotatable {
68
+ self . map_attrs ( |_| attrs)
69
+ }
67
70
68
71
pub fn expect_item ( self ) -> P < ast:: Item > {
69
72
match self {
0 commit comments