1212//! for the `Code` associated with a particular NodeId.
1313
1414use crate :: hir as ast;
15+ use crate :: hir:: intravisit:: FnKind ;
1516use crate :: hir:: map;
1617use crate :: hir:: { Expr , FnDecl , Node } ;
17- use crate :: hir:: intravisit:: FnKind ;
1818use syntax:: ast:: { Attribute , Ident } ;
1919use syntax_pos:: Span ;
2020
@@ -29,11 +29,15 @@ use syntax_pos::Span;
2929///
3030/// To construct one, use the `Code::from_node` function.
3131#[ derive( Copy , Clone , Debug ) ]
32- pub struct FnLikeNode < ' a > { node : Node < ' a > }
32+ pub struct FnLikeNode < ' a > {
33+ node : Node < ' a > ,
34+ }
3335
3436/// MaybeFnLike wraps a method that indicates if an object
3537/// corresponds to some FnLikeNode.
36- trait MaybeFnLike { fn is_fn_like ( & self ) -> bool ; }
38+ trait MaybeFnLike {
39+ fn is_fn_like ( & self ) -> bool ;
40+ }
3741
3842impl MaybeFnLike for ast:: Item {
3943 fn is_fn_like ( & self ) -> bool {
@@ -96,23 +100,23 @@ impl<'a> Code<'a> {
96100 Code :: from_node ( map, map. get_parent_node ( id) )
97101 }
98102 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 ) ,
100104 }
101105 }
102106}
103107
104108/// These are all the components one can extract from a fn item for
105109/// use when implementing FnLikeNode operations.
106110struct 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 ,
111115 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 ] ,
116120}
117121
118122/// These are all the components one can extract from a closure expr
@@ -127,13 +131,7 @@ struct ClosureParts<'a> {
127131
128132impl < ' a > ClosureParts < ' a > {
129133 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 }
137135 }
138136}
139137
@@ -145,33 +143,41 @@ impl<'a> FnLikeNode<'a> {
145143 map:: Node :: TraitItem ( tm) => tm. is_fn_like ( ) ,
146144 map:: Node :: ImplItem ( it) => it. is_fn_like ( ) ,
147145 map:: Node :: Expr ( e) => e. is_fn_like ( ) ,
148- _ => false
146+ _ => false ,
149147 } ;
150148 fn_like. then_some ( FnLikeNode { node } )
151149 }
152150
153151 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+ )
157157 }
158158
159159 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+ )
163165 }
164166
165167 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+ )
169173 }
170174
171175 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+ )
175181 }
176182
177183 pub fn constness ( self ) -> ast:: Constness {
@@ -190,41 +196,40 @@ impl<'a> FnLikeNode<'a> {
190196 let item = |p : ItemFnParts < ' a > | -> FnKind < ' a > {
191197 FnKind :: ItemFn ( p. ident , p. generics , p. header , p. vis , p. attrs )
192198 } ;
193- let closure = |c : ClosureParts < ' a > | {
194- FnKind :: Closure ( c. attrs )
195- } ;
199+ let closure = |c : ClosureParts < ' a > | FnKind :: Closure ( c. attrs ) ;
196200 let method = |_, ident : Ident , sig : & ' a ast:: FnSig , vis, _, _, attrs| {
197201 FnKind :: Method ( ident, sig, vis, attrs)
198202 } ;
199203 self . handle ( item, method, closure)
200204 }
201205
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
203208 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 ,
212218 C : FnOnce ( ClosureParts < ' a > ) -> A ,
213219 {
214220 match self . node {
215221 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+ } ) ,
228233 _ => bug ! ( "item FnLikeNode that is not fn-like" ) ,
229234 } ,
230235 map:: Node :: TraitItem ( ti) => match ti. kind {
@@ -233,17 +238,16 @@ impl<'a> FnLikeNode<'a> {
233238 }
234239 _ => bug ! ( "trait method FnLikeNode that is not fn-like" ) ,
235240 } ,
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 )
242244 }
245+ _ => bug ! ( "impl method FnLikeNode that is not fn-like" ) ,
243246 } ,
244247 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+ }
247251 _ => bug ! ( "expr FnLikeNode that is not fn-like" ) ,
248252 } ,
249253 _ => bug ! ( "other FnLikeNode that is not fn-like" ) ,
0 commit comments