9
9
// except according to those terms.
10
10
11
11
12
- use std:: option;
13
12
use syntax:: fold:: ast_fold;
14
13
use syntax:: { ast, fold, attr} ;
15
14
16
- type in_cfg_pred = @fn ( attrs : & [ ast:: Attribute ] ) -> bool ;
17
-
18
- struct Context {
19
- in_cfg : in_cfg_pred
15
+ struct Context < ' self > {
16
+ in_cfg : & ' self fn ( attrs : & [ ast:: Attribute ] ) -> bool ,
20
17
}
21
18
22
19
// Support conditional compilation by transforming the AST, stripping out
@@ -27,56 +24,55 @@ pub fn strip_unconfigured_items(crate: @ast::Crate) -> @ast::Crate {
27
24
}
28
25
}
29
26
30
- struct ItemRemover {
31
- ctxt : @Context ,
32
- }
33
-
34
- impl fold:: ast_fold for ItemRemover {
27
+ impl < ' self > fold:: ast_fold for Context < ' self > {
35
28
fn fold_mod ( & self , module : & ast:: _mod ) -> ast:: _mod {
36
- fold_mod ( self . ctxt , module, self )
29
+ fold_mod ( self , module)
37
30
}
38
31
fn fold_block ( & self , block : & ast:: Block ) -> ast:: Block {
39
- fold_block ( self . ctxt , block, self )
32
+ fold_block ( self , block)
40
33
}
41
34
fn fold_foreign_mod ( & self , foreign_module : & ast:: foreign_mod )
42
35
-> ast:: foreign_mod {
43
- fold_foreign_mod ( self . ctxt , foreign_module, self )
36
+ fold_foreign_mod ( self , foreign_module)
44
37
}
45
38
fn fold_item_underscore ( & self , item : & ast:: item_ ) -> ast:: item_ {
46
- fold_item_underscore ( self . ctxt , item, self )
39
+ fold_item_underscore ( self , item)
47
40
}
48
41
}
49
42
50
- pub fn strip_items ( crate : & ast:: Crate , in_cfg : in_cfg_pred ) -> @ast:: Crate {
51
- let ctxt = @Context {
43
+ pub fn strip_items ( crate : & ast:: Crate ,
44
+ in_cfg : & fn ( attrs : & [ ast:: Attribute ] ) -> bool )
45
+ -> @ast:: Crate {
46
+ let ctxt = Context {
52
47
in_cfg : in_cfg,
53
48
} ;
54
- let precursor = ItemRemover {
55
- ctxt : ctxt,
56
- } ;
57
- @precursor. fold_crate ( crate )
49
+ @ctxt. fold_crate ( crate )
58
50
}
59
51
60
- fn filter_item ( cx : @Context , item : @ast:: item ) ->
61
- Option < @ast:: item > {
62
- if item_in_cfg ( cx, item) { option:: Some ( item) } else { option:: None }
52
+ fn filter_item ( cx : & Context , item : @ast:: item ) -> Option < @ast:: item > {
53
+ if item_in_cfg ( cx, item) {
54
+ Some ( item)
55
+ } else {
56
+ None
57
+ }
63
58
}
64
59
65
- fn filter_view_item < ' r > ( cx : @Context , view_item : & ' r ast:: view_item ) -> Option < & ' r ast:: view_item > {
60
+ fn filter_view_item < ' r > ( cx : & Context , view_item : & ' r ast:: view_item )
61
+ -> Option < & ' r ast:: view_item > {
66
62
if view_item_in_cfg ( cx, view_item) {
67
- option :: Some ( view_item)
63
+ Some ( view_item)
68
64
} else {
69
- option :: None
65
+ None
70
66
}
71
67
}
72
68
73
- fn fold_mod ( cx : @ Context , m : & ast:: _mod , fld : & ItemRemover ) -> ast:: _mod {
74
- let filtered_items = do m. items . iter ( ) . filter_map |a| {
75
- filter_item ( cx, * a) . and_then ( |x| fld . fold_item ( x) )
69
+ fn fold_mod ( cx : & Context , m : & ast:: _mod ) -> ast:: _mod {
70
+ let filtered_items = do m. items . iter ( ) . filter_map |a| {
71
+ filter_item ( cx, * a) . and_then ( |x| cx . fold_item ( x) )
76
72
} . collect ( ) ;
77
73
let filtered_view_items = do m. view_items . iter ( ) . filter_map |a| {
78
74
do filter_view_item ( cx, a) . map_move |x| {
79
- fld . fold_view_item ( x)
75
+ cx . fold_view_item ( x)
80
76
}
81
77
} . collect ( ) ;
82
78
ast:: _mod {
@@ -85,22 +81,23 @@ fn fold_mod(cx: @Context, m: &ast::_mod, fld: &ItemRemover) -> ast::_mod {
85
81
}
86
82
}
87
83
88
- fn filter_foreign_item ( cx : @ Context , item : @ast:: foreign_item ) ->
89
- Option < @ast:: foreign_item > {
84
+ fn filter_foreign_item ( cx : & Context , item : @ast:: foreign_item )
85
+ -> Option < @ast:: foreign_item > {
90
86
if foreign_item_in_cfg ( cx, item) {
91
- option:: Some ( item)
92
- } else { option:: None }
87
+ Some ( item)
88
+ } else {
89
+ None
90
+ }
93
91
}
94
92
95
- fn fold_foreign_mod ( cx : @Context , nm : & ast:: foreign_mod , fld : & ItemRemover )
96
- -> ast:: foreign_mod {
93
+ fn fold_foreign_mod ( cx : & Context , nm : & ast:: foreign_mod ) -> ast:: foreign_mod {
97
94
let filtered_items = nm. items
98
95
. iter ( )
99
96
. filter_map ( |a| filter_foreign_item ( cx, * a) )
100
97
. collect ( ) ;
101
98
let filtered_view_items = do nm. view_items . iter ( ) . filter_map |a| {
102
99
do filter_view_item ( cx, a) . map_move |x| {
103
- fld . fold_view_item ( x)
100
+ cx . fold_view_item ( x)
104
101
}
105
102
} . collect ( ) ;
106
103
ast:: foreign_mod {
@@ -111,76 +108,78 @@ fn fold_foreign_mod(cx: @Context, nm: &ast::foreign_mod, fld: &ItemRemover)
111
108
}
112
109
}
113
110
114
- fn fold_item_underscore ( cx : @Context , item : & ast:: item_ , fld : & ItemRemover )
115
- -> ast:: item_ {
111
+ fn fold_item_underscore ( cx : & Context , item : & ast:: item_ ) -> ast:: item_ {
116
112
let item = match * item {
117
113
ast:: item_impl( ref a, ref b, ref c, ref methods) => {
118
114
let methods = methods. iter ( ) . filter ( |m| method_in_cfg ( cx, * * m) )
119
115
. map ( |x| * x) . collect ( ) ;
120
116
ast:: item_impl ( ( * a) . clone ( ) , ( * b) . clone ( ) , ( * c) . clone ( ) , methods)
121
117
}
122
118
ast:: item_trait( ref a, ref b, ref methods) => {
123
- let methods = methods. iter ( ) . filter ( |m| trait_method_in_cfg ( cx, * m) )
124
- . map ( |x| ( * x) . clone ( ) ) . collect ( ) ;
119
+ let methods = methods. iter ( )
120
+ . filter ( |m| trait_method_in_cfg ( cx, * m) )
121
+ . map ( |x| ( * x) . clone ( ) )
122
+ . collect ( ) ;
125
123
ast:: item_trait ( ( * a) . clone ( ) , ( * b) . clone ( ) , methods)
126
124
}
127
125
ref item => ( * item) . clone ( ) ,
128
126
} ;
129
127
130
- fold:: noop_fold_item_underscore ( & item, fld )
128
+ fold:: noop_fold_item_underscore ( & item, cx )
131
129
}
132
130
133
- fn filter_stmt ( cx : @Context , stmt : @ast:: Stmt ) ->
134
- Option < @ast:: Stmt > {
131
+ fn filter_stmt ( cx : & Context , stmt : @ast:: Stmt ) -> Option < @ast:: Stmt > {
135
132
match stmt. node {
136
133
ast:: StmtDecl ( decl, _) => {
137
134
match decl. node {
138
135
ast:: DeclItem ( item) => {
139
136
if item_in_cfg ( cx, item) {
140
- option:: Some ( stmt)
141
- } else { option:: None }
137
+ Some ( stmt)
138
+ } else {
139
+ None
140
+ }
142
141
}
143
- _ => option :: Some ( stmt)
142
+ _ => Some ( stmt)
144
143
}
145
144
}
146
- _ => option :: Some ( stmt)
145
+ _ => Some ( stmt) ,
147
146
}
148
147
}
149
148
150
- fn fold_block ( cx : @ Context , b : & ast:: Block , fld : & ItemRemover ) -> ast:: Block {
149
+ fn fold_block ( cx : & Context , b : & ast:: Block ) -> ast:: Block {
151
150
let resulting_stmts = do b. stmts . iter ( ) . filter_map |a| {
152
- filter_stmt ( cx, * a) . and_then ( |stmt| fld . fold_stmt ( stmt) )
151
+ filter_stmt ( cx, * a) . and_then ( |stmt| cx . fold_stmt ( stmt) )
153
152
} . collect ( ) ;
154
153
let filtered_view_items = do b. view_items . iter ( ) . filter_map |a| {
155
- filter_view_item ( cx, a) . map ( |x| fld . fold_view_item ( * x) )
154
+ filter_view_item ( cx, a) . map ( |x| cx . fold_view_item ( * x) )
156
155
} . collect ( ) ;
157
156
ast:: Block {
158
157
view_items : filtered_view_items,
159
158
stmts : resulting_stmts,
160
- expr : b. expr . map ( |x| fld . fold_expr ( * x) ) ,
159
+ expr : b. expr . map ( |x| cx . fold_expr ( * x) ) ,
161
160
id : b. id ,
162
161
rules : b. rules ,
163
162
span : b. span ,
164
163
}
165
164
}
166
165
167
- fn item_in_cfg ( cx : @ Context , item : @ast:: item ) -> bool {
166
+ fn item_in_cfg ( cx : & Context , item : @ast:: item ) -> bool {
168
167
return ( cx. in_cfg ) ( item. attrs ) ;
169
168
}
170
169
171
- fn foreign_item_in_cfg ( cx : @ Context , item : @ast:: foreign_item ) -> bool {
170
+ fn foreign_item_in_cfg ( cx : & Context , item : @ast:: foreign_item ) -> bool {
172
171
return ( cx. in_cfg ) ( item. attrs ) ;
173
172
}
174
173
175
- fn view_item_in_cfg ( cx : @ Context , item : & ast:: view_item ) -> bool {
174
+ fn view_item_in_cfg ( cx : & Context , item : & ast:: view_item ) -> bool {
176
175
return ( cx. in_cfg ) ( item. attrs ) ;
177
176
}
178
177
179
- fn method_in_cfg ( cx : @ Context , meth : @ast:: method ) -> bool {
178
+ fn method_in_cfg ( cx : & Context , meth : @ast:: method ) -> bool {
180
179
return ( cx. in_cfg ) ( meth. attrs ) ;
181
180
}
182
181
183
- fn trait_method_in_cfg ( cx : @ Context , meth : & ast:: trait_method ) -> bool {
182
+ fn trait_method_in_cfg ( cx : & Context , meth : & ast:: trait_method ) -> bool {
184
183
match * meth {
185
184
ast:: required( ref meth) => ( cx. in_cfg ) ( meth. attrs ) ,
186
185
ast:: provided( @ref meth) => ( cx. in_cfg ) ( meth. attrs )
@@ -192,3 +191,4 @@ fn trait_method_in_cfg(cx: @Context, meth: &ast::trait_method) -> bool {
192
191
fn in_cfg ( cfg : & [ @ast:: MetaItem ] , attrs : & [ ast:: Attribute ] ) -> bool {
193
192
attr:: test_cfg ( cfg, attrs. iter ( ) . map ( |x| * x) )
194
193
}
194
+
0 commit comments