@@ -6,12 +6,13 @@ use super::{AssocItem, Expr, ForeignItem, Item, Local, MacCallStmt};
66use super :: { AttrItem , AttrKind , Block , Pat , Path , Ty , Visibility } ;
77use super :: { AttrVec , Attribute , Stmt , StmtKind } ;
88
9- use std:: fmt:: Debug ;
9+ use std:: fmt;
10+ use std:: marker:: PhantomData ;
1011
1112/// An `AstLike` represents an AST node (or some wrapper around
1213/// and AST node) which stores some combination of attributes
1314/// and tokens.
14- pub trait AstLike : Sized + Debug {
15+ pub trait AstLike : Sized + fmt :: Debug {
1516 /// This is `true` if this `AstLike` might support 'custom' (proc-macro) inner
1617 /// attributes. Attributes like `#![cfg]` and `#![cfg_attr]` are not
1718 /// considered 'custom' attributes
@@ -285,3 +286,37 @@ derive_has_attrs_no_tokens! {
285286derive_has_tokens_no_attrs ! {
286287 Ty , Block , AttrItem , Pat , Path , Visibility
287288}
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