@@ -6,12 +6,13 @@ use super::{AssocItem, Expr, ForeignItem, Item, Local, MacCallStmt};
6
6
use super :: { AttrItem , AttrKind , Block , Pat , Path , Ty , Visibility } ;
7
7
use super :: { AttrVec , Attribute , Stmt , StmtKind } ;
8
8
9
- use std:: fmt:: Debug ;
9
+ use std:: fmt;
10
+ use std:: marker:: PhantomData ;
10
11
11
12
/// An `AstLike` represents an AST node (or some wrapper around
12
13
/// and AST node) which stores some combination of attributes
13
14
/// and tokens.
14
- pub trait AstLike : Sized + Debug {
15
+ pub trait AstLike : Sized + fmt :: Debug {
15
16
/// This is `true` if this `AstLike` might support 'custom' (proc-macro) inner
16
17
/// attributes. Attributes like `#![cfg]` and `#![cfg_attr]` are not
17
18
/// considered 'custom' attributes
@@ -285,3 +286,37 @@ derive_has_attrs_no_tokens! {
285
286
derive_has_tokens_no_attrs ! {
286
287
Ty , Block , AttrItem , Pat , Path , Visibility
287
288
}
289
+
290
+ /// A newtype around an `AstLike` node that implements `AstLike` itself.
291
+ pub struct AstLikeWrapper < Wrapped , Tag > {
292
+ pub wrapped : Wrapped ,
293
+ pub tag : PhantomData < Tag > ,
294
+ }
295
+
296
+ impl < Wrapped , Tag > AstLikeWrapper < Wrapped , Tag > {
297
+ pub fn new ( wrapped : Wrapped , _tag : Tag ) -> AstLikeWrapper < Wrapped , Tag > {
298
+ AstLikeWrapper { wrapped, tag : Default :: default ( ) }
299
+ }
300
+ }
301
+
302
+ impl < Wrapped : fmt:: Debug , Tag > fmt:: Debug for AstLikeWrapper < Wrapped , Tag > {
303
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
304
+ f. debug_struct ( "AstLikeWrapper" )
305
+ . field ( "wrapped" , & self . wrapped )
306
+ . field ( "tag" , & self . tag )
307
+ . finish ( )
308
+ }
309
+ }
310
+
311
+ impl < Wrapped : AstLike , Tag > AstLike for AstLikeWrapper < Wrapped , Tag > {
312
+ const SUPPORTS_CUSTOM_INNER_ATTRS : bool = Wrapped :: SUPPORTS_CUSTOM_INNER_ATTRS ;
313
+ fn attrs ( & self ) -> & [ Attribute ] {
314
+ self . wrapped . attrs ( )
315
+ }
316
+ fn visit_attrs ( & mut self , f : impl FnOnce ( & mut Vec < Attribute > ) ) {
317
+ self . wrapped . visit_attrs ( f)
318
+ }
319
+ fn tokens_mut ( & mut self ) -> Option < & mut Option < LazyTokenStream > > {
320
+ self . wrapped . tokens_mut ( )
321
+ }
322
+ }
0 commit comments