12
12
//! for the `Code` associated with a particular NodeId.
13
13
14
14
use crate :: hir as ast;
15
+ use crate :: hir:: intravisit:: FnKind ;
15
16
use crate :: hir:: map;
16
17
use crate :: hir:: { Expr , FnDecl , Node } ;
17
- use crate :: hir:: intravisit:: FnKind ;
18
18
use syntax:: ast:: { Attribute , Ident } ;
19
19
use syntax_pos:: Span ;
20
20
@@ -29,11 +29,15 @@ use syntax_pos::Span;
29
29
///
30
30
/// To construct one, use the `Code::from_node` function.
31
31
#[ derive( Copy , Clone , Debug ) ]
32
- pub struct FnLikeNode < ' a > { node : Node < ' a > }
32
+ pub struct FnLikeNode < ' a > {
33
+ node : Node < ' a > ,
34
+ }
33
35
34
36
/// MaybeFnLike wraps a method that indicates if an object
35
37
/// corresponds to some FnLikeNode.
36
- trait MaybeFnLike { fn is_fn_like ( & self ) -> bool ; }
38
+ trait MaybeFnLike {
39
+ fn is_fn_like ( & self ) -> bool ;
40
+ }
37
41
38
42
impl MaybeFnLike for ast:: Item {
39
43
fn is_fn_like ( & self ) -> bool {
@@ -96,23 +100,23 @@ impl<'a> Code<'a> {
96
100
Code :: from_node ( map, map. get_parent_node ( id) )
97
101
}
98
102
map:: Node :: Expr ( expr) => Some ( Code :: Expr ( expr) ) ,
99
- node => FnLikeNode :: from_node ( node) . map ( Code :: FnLike )
103
+ node => FnLikeNode :: from_node ( node) . map ( Code :: FnLike ) ,
100
104
}
101
105
}
102
106
}
103
107
104
108
/// These are all the components one can extract from a fn item for
105
109
/// use when implementing FnLikeNode operations.
106
110
struct ItemFnParts < ' a > {
107
- ident : Ident ,
108
- decl : & ' a ast:: FnDecl ,
109
- header : ast:: FnHeader ,
110
- vis : & ' a ast:: Visibility ,
111
+ ident : Ident ,
112
+ decl : & ' a ast:: FnDecl ,
113
+ header : ast:: FnHeader ,
114
+ vis : & ' a ast:: Visibility ,
111
115
generics : & ' a ast:: Generics ,
112
- body : ast:: BodyId ,
113
- id : ast:: HirId ,
114
- span : Span ,
115
- attrs : & ' a [ Attribute ] ,
116
+ body : ast:: BodyId ,
117
+ id : ast:: HirId ,
118
+ span : Span ,
119
+ attrs : & ' a [ Attribute ] ,
116
120
}
117
121
118
122
/// These are all the components one can extract from a closure expr
@@ -127,13 +131,7 @@ struct ClosureParts<'a> {
127
131
128
132
impl < ' a > ClosureParts < ' a > {
129
133
fn new ( d : & ' a FnDecl , b : ast:: BodyId , id : ast:: HirId , s : Span , attrs : & ' a [ Attribute ] ) -> Self {
130
- ClosureParts {
131
- decl : d,
132
- body : b,
133
- id,
134
- span : s,
135
- attrs,
136
- }
134
+ ClosureParts { decl : d, body : b, id, span : s, attrs }
137
135
}
138
136
}
139
137
@@ -145,33 +143,41 @@ impl<'a> FnLikeNode<'a> {
145
143
map:: Node :: TraitItem ( tm) => tm. is_fn_like ( ) ,
146
144
map:: Node :: ImplItem ( it) => it. is_fn_like ( ) ,
147
145
map:: Node :: Expr ( e) => e. is_fn_like ( ) ,
148
- _ => false
146
+ _ => false ,
149
147
} ;
150
148
fn_like. then_some ( FnLikeNode { node } )
151
149
}
152
150
153
151
pub fn body ( self ) -> ast:: BodyId {
154
- self . handle ( |i : ItemFnParts < ' a > | i. body ,
155
- |_, _, _: & ' a ast:: FnSig , _, body : ast:: BodyId , _, _| body,
156
- |c : ClosureParts < ' a > | c. body )
152
+ self . handle (
153
+ |i : ItemFnParts < ' a > | i. body ,
154
+ |_, _, _: & ' a ast:: FnSig , _, body : ast:: BodyId , _, _| body,
155
+ |c : ClosureParts < ' a > | c. body ,
156
+ )
157
157
}
158
158
159
159
pub fn decl ( self ) -> & ' a FnDecl {
160
- self . handle ( |i : ItemFnParts < ' a > | & * i. decl ,
161
- |_, _, sig : & ' a ast:: FnSig , _, _, _, _| & sig. decl ,
162
- |c : ClosureParts < ' a > | c. decl )
160
+ self . handle (
161
+ |i : ItemFnParts < ' a > | & * i. decl ,
162
+ |_, _, sig : & ' a ast:: FnSig , _, _, _, _| & sig. decl ,
163
+ |c : ClosureParts < ' a > | c. decl ,
164
+ )
163
165
}
164
166
165
167
pub fn span ( self ) -> Span {
166
- self . handle ( |i : ItemFnParts < ' _ > | i. span ,
167
- |_, _, _: & ' a ast:: FnSig , _, _, span, _| span,
168
- |c : ClosureParts < ' _ > | c. span )
168
+ self . handle (
169
+ |i : ItemFnParts < ' _ > | i. span ,
170
+ |_, _, _: & ' a ast:: FnSig , _, _, span, _| span,
171
+ |c : ClosureParts < ' _ > | c. span ,
172
+ )
169
173
}
170
174
171
175
pub fn id ( self ) -> ast:: HirId {
172
- self . handle ( |i : ItemFnParts < ' _ > | i. id ,
173
- |id, _, _: & ' a ast:: FnSig , _, _, _, _| id,
174
- |c : ClosureParts < ' _ > | c. id )
176
+ self . handle (
177
+ |i : ItemFnParts < ' _ > | i. id ,
178
+ |id, _, _: & ' a ast:: FnSig , _, _, _, _| id,
179
+ |c : ClosureParts < ' _ > | c. id ,
180
+ )
175
181
}
176
182
177
183
pub fn constness ( self ) -> ast:: Constness {
@@ -190,41 +196,40 @@ impl<'a> FnLikeNode<'a> {
190
196
let item = |p : ItemFnParts < ' a > | -> FnKind < ' a > {
191
197
FnKind :: ItemFn ( p. ident , p. generics , p. header , p. vis , p. attrs )
192
198
} ;
193
- let closure = |c : ClosureParts < ' a > | {
194
- FnKind :: Closure ( c. attrs )
195
- } ;
199
+ let closure = |c : ClosureParts < ' a > | FnKind :: Closure ( c. attrs ) ;
196
200
let method = |_, ident : Ident , sig : & ' a ast:: FnSig , vis, _, _, attrs| {
197
201
FnKind :: Method ( ident, sig, vis, attrs)
198
202
} ;
199
203
self . handle ( item, method, closure)
200
204
}
201
205
202
- fn handle < A , I , M , C > ( self , item_fn : I , method : M , closure : C ) -> A where
206
+ fn handle < A , I , M , C > ( self , item_fn : I , method : M , closure : C ) -> A
207
+ where
203
208
I : FnOnce ( ItemFnParts < ' a > ) -> A ,
204
- M : FnOnce ( ast:: HirId ,
205
- Ident ,
206
- & ' a ast:: FnSig ,
207
- Option < & ' a ast:: Visibility > ,
208
- ast:: BodyId ,
209
- Span ,
210
- & ' a [ Attribute ] )
211
- -> A ,
209
+ M : FnOnce (
210
+ ast:: HirId ,
211
+ Ident ,
212
+ & ' a ast:: FnSig ,
213
+ Option < & ' a ast:: Visibility > ,
214
+ ast:: BodyId ,
215
+ Span ,
216
+ & ' a [ Attribute ] ,
217
+ ) -> A ,
212
218
C : FnOnce ( ClosureParts < ' a > ) -> A ,
213
219
{
214
220
match self . node {
215
221
map:: Node :: Item ( i) => match i. kind {
216
- ast:: ItemKind :: Fn ( ref sig, ref generics, block) =>
217
- item_fn ( ItemFnParts {
218
- id : i. hir_id ,
219
- ident : i. ident ,
220
- decl : & sig. decl ,
221
- body : block,
222
- vis : & i. vis ,
223
- span : i. span ,
224
- attrs : & i. attrs ,
225
- header : sig. header ,
226
- generics,
227
- } ) ,
222
+ ast:: ItemKind :: Fn ( ref sig, ref generics, block) => item_fn ( ItemFnParts {
223
+ id : i. hir_id ,
224
+ ident : i. ident ,
225
+ decl : & sig. decl ,
226
+ body : block,
227
+ vis : & i. vis ,
228
+ span : i. span ,
229
+ attrs : & i. attrs ,
230
+ header : sig. header ,
231
+ generics,
232
+ } ) ,
228
233
_ => bug ! ( "item FnLikeNode that is not fn-like" ) ,
229
234
} ,
230
235
map:: Node :: TraitItem ( ti) => match ti. kind {
@@ -233,17 +238,16 @@ impl<'a> FnLikeNode<'a> {
233
238
}
234
239
_ => bug ! ( "trait method FnLikeNode that is not fn-like" ) ,
235
240
} ,
236
- map:: Node :: ImplItem ( ii) => {
237
- match ii. kind {
238
- ast:: ImplItemKind :: Method ( ref sig, body) => {
239
- method ( ii. hir_id , ii. ident , sig, Some ( & ii. vis ) , body, ii. span , & ii. attrs )
240
- }
241
- _ => bug ! ( "impl method FnLikeNode that is not fn-like" )
241
+ map:: Node :: ImplItem ( ii) => match ii. kind {
242
+ ast:: ImplItemKind :: Method ( ref sig, body) => {
243
+ method ( ii. hir_id , ii. ident , sig, Some ( & ii. vis ) , body, ii. span , & ii. attrs )
242
244
}
245
+ _ => bug ! ( "impl method FnLikeNode that is not fn-like" ) ,
243
246
} ,
244
247
map:: Node :: Expr ( e) => match e. kind {
245
- ast:: ExprKind :: Closure ( _, ref decl, block, _fn_decl_span, _gen) =>
246
- closure ( ClosureParts :: new ( & decl, block, e. hir_id , e. span , & e. attrs ) ) ,
248
+ ast:: ExprKind :: Closure ( _, ref decl, block, _fn_decl_span, _gen) => {
249
+ closure ( ClosureParts :: new ( & decl, block, e. hir_id , e. span , & e. attrs ) )
250
+ }
247
251
_ => bug ! ( "expr FnLikeNode that is not fn-like" ) ,
248
252
} ,
249
253
_ => bug ! ( "other FnLikeNode that is not fn-like" ) ,
0 commit comments