@@ -13,34 +13,6 @@ use crate::{
13
13
Ty , Variant , Visibility , WherePredicate ,
14
14
} ;
15
15
16
- /// A utility trait to reduce boilerplate.
17
- /// Standard `Deref(Mut)` cannot be reused due to coherence.
18
- pub trait AstDeref {
19
- type Target ;
20
- fn ast_deref ( & self ) -> & Self :: Target ;
21
- fn ast_deref_mut ( & mut self ) -> & mut Self :: Target ;
22
- }
23
-
24
- macro_rules! impl_not_ast_deref {
25
- ( $( $T: ty) ,+ $( , ) ?) => {
26
- $(
27
- impl !AstDeref for $T { }
28
- ) +
29
- } ;
30
- }
31
-
32
- impl_not_ast_deref ! ( AssocItem , Expr , ForeignItem , Item , Stmt ) ;
33
-
34
- impl < T > AstDeref for P < T > {
35
- type Target = T ;
36
- fn ast_deref ( & self ) -> & Self :: Target {
37
- self
38
- }
39
- fn ast_deref_mut ( & mut self ) -> & mut Self :: Target {
40
- self
41
- }
42
- }
43
-
44
16
/// A trait for AST nodes having an ID.
45
17
pub trait HasNodeId {
46
18
fn node_id ( & self ) -> NodeId ;
@@ -81,12 +53,12 @@ impl_has_node_id!(
81
53
WherePredicate ,
82
54
) ;
83
55
84
- impl < T : AstDeref < Target : HasNodeId > > HasNodeId for T {
56
+ impl < T : HasNodeId > HasNodeId for P < T > {
85
57
fn node_id ( & self ) -> NodeId {
86
- self . ast_deref ( ) . node_id ( )
58
+ ( * * self ) . node_id ( )
87
59
}
88
60
fn node_id_mut ( & mut self ) -> & mut NodeId {
89
- self . ast_deref_mut ( ) . node_id_mut ( )
61
+ ( * * self ) . node_id_mut ( )
90
62
}
91
63
}
92
64
@@ -138,21 +110,21 @@ impl_has_tokens_none!(
138
110
WherePredicate
139
111
) ;
140
112
141
- impl < T : AstDeref < Target : HasTokens > > HasTokens for T {
113
+ impl < T : HasTokens > HasTokens for Option < T > {
142
114
fn tokens ( & self ) -> Option < & LazyAttrTokenStream > {
143
- self . ast_deref ( ) . tokens ( )
115
+ self . as_ref ( ) . and_then ( |inner| inner . tokens ( ) )
144
116
}
145
117
fn tokens_mut ( & mut self ) -> Option < & mut Option < LazyAttrTokenStream > > {
146
- self . ast_deref_mut ( ) . tokens_mut ( )
118
+ self . as_mut ( ) . and_then ( |inner| inner . tokens_mut ( ) )
147
119
}
148
120
}
149
121
150
- impl < T : HasTokens > HasTokens for Option < T > {
122
+ impl < T : HasTokens > HasTokens for P < T > {
151
123
fn tokens ( & self ) -> Option < & LazyAttrTokenStream > {
152
- self . as_ref ( ) . and_then ( |inner| inner . tokens ( ) )
124
+ ( * * self ) . tokens ( )
153
125
}
154
126
fn tokens_mut ( & mut self ) -> Option < & mut Option < LazyAttrTokenStream > > {
155
- self . as_mut ( ) . and_then ( |inner| inner . tokens_mut ( ) )
127
+ ( * * self ) . tokens_mut ( )
156
128
}
157
129
}
158
130
@@ -273,13 +245,13 @@ impl_has_attrs!(
273
245
) ;
274
246
impl_has_attrs_none ! ( Attribute , AttrItem , Block , Pat , Path , Ty , Visibility ) ;
275
247
276
- impl < T : AstDeref < Target : HasAttrs > > HasAttrs for T {
277
- const SUPPORTS_CUSTOM_INNER_ATTRS : bool = T :: Target :: SUPPORTS_CUSTOM_INNER_ATTRS ;
248
+ impl < T : HasAttrs > HasAttrs for P < T > {
249
+ const SUPPORTS_CUSTOM_INNER_ATTRS : bool = T :: SUPPORTS_CUSTOM_INNER_ATTRS ;
278
250
fn attrs ( & self ) -> & [ Attribute ] {
279
- self . ast_deref ( ) . attrs ( )
251
+ ( * * self ) . attrs ( )
280
252
}
281
253
fn visit_attrs ( & mut self , f : impl FnOnce ( & mut AttrVec ) ) {
282
- self . ast_deref_mut ( ) . visit_attrs ( f)
254
+ ( * * self ) . visit_attrs ( f) ;
283
255
}
284
256
}
285
257
@@ -343,13 +315,22 @@ impl<Wrapped, Tag> AstNodeWrapper<Wrapped, Tag> {
343
315
}
344
316
}
345
317
346
- impl < Wrapped , Tag > AstDeref for AstNodeWrapper < Wrapped , Tag > {
347
- type Target = Wrapped ;
348
- fn ast_deref ( & self ) -> & Self :: Target {
349
- & self . wrapped
318
+ impl < Wrapped : HasNodeId , Tag > HasNodeId for AstNodeWrapper < Wrapped , Tag > {
319
+ fn node_id ( & self ) -> NodeId {
320
+ self . wrapped . node_id ( )
321
+ }
322
+ fn node_id_mut ( & mut self ) -> & mut NodeId {
323
+ self . wrapped . node_id_mut ( )
324
+ }
325
+ }
326
+
327
+ impl < Wrapped : HasAttrs , Tag > HasAttrs for AstNodeWrapper < Wrapped , Tag > {
328
+ const SUPPORTS_CUSTOM_INNER_ATTRS : bool = Wrapped :: SUPPORTS_CUSTOM_INNER_ATTRS ;
329
+ fn attrs ( & self ) -> & [ Attribute ] {
330
+ self . wrapped . attrs ( )
350
331
}
351
- fn ast_deref_mut ( & mut self ) -> & mut Self :: Target {
352
- & mut self . wrapped
332
+ fn visit_attrs ( & mut self , f : impl FnOnce ( & mut AttrVec ) ) {
333
+ self . wrapped . visit_attrs ( f ) ;
353
334
}
354
335
}
355
336
0 commit comments