@@ -413,21 +413,27 @@ ast_struct! {
413413 #[ cfg_attr( docsrs, doc( cfg( any( feature = "full" , feature = "derive" ) ) ) ) ]
414414 pub struct TraitBound {
415415 pub paren_token: Option <token:: Paren >,
416- pub modifier : TraitBoundModifier ,
416+ pub modifiers : TraitBoundModifiers ,
417417 /// The `for<'a>` in `for<'a> Foo<&'a T>`
418418 pub lifetimes: Option <BoundLifetimes >,
419419 /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>`
420420 pub path: Path ,
421421 }
422422}
423423
424- ast_enum ! {
424+ ast_struct ! {
425425 /// A modifier on a trait bound, currently only used for the `?` in
426426 /// `?Sized`.
427427 #[ cfg_attr( docsrs, doc( cfg( any( feature = "full" , feature = "derive" ) ) ) ) ]
428- pub enum TraitBoundModifier {
429- None ,
430- Maybe ( Token ![ ?] ) ,
428+ #[ non_exhaustive]
429+ pub struct TraitBoundModifiers {
430+ pub maybe: Option <Token ![ ?] >,
431+ }
432+ }
433+
434+ impl Default for TraitBoundModifiers {
435+ fn default ( ) -> Self {
436+ TraitBoundModifiers { maybe : None }
431437 }
432438}
433439
@@ -521,7 +527,7 @@ pub(crate) mod parsing {
521527 use crate :: ext:: IdentExt as _;
522528 use crate :: generics:: {
523529 BoundLifetimes , ConstParam , GenericParam , Generics , LifetimeParam , PredicateLifetime ,
524- PredicateType , TraitBound , TraitBoundModifier , TypeParam , TypeParamBound , WhereClause ,
530+ PredicateType , TraitBound , TraitBoundModifiers , TypeParam , TypeParamBound , WhereClause ,
525531 WherePredicate ,
526532 } ;
527533 #[ cfg( feature = "full" ) ]
@@ -858,8 +864,8 @@ pub(crate) mod parsing {
858864 }
859865 }
860866
861- let modifier : TraitBoundModifier = input. parse ( ) ?;
862- if lifetimes. is_none ( ) && matches ! ( modifier , TraitBoundModifier :: Maybe ( _ ) ) {
867+ let maybe : Option < Token ! [ ? ] > = input. parse ( ) ?;
868+ if lifetimes. is_none ( ) && maybe . is_some ( ) {
863869 lifetimes = input. parse ( ) ?;
864870 }
865871
@@ -874,12 +880,9 @@ pub(crate) mod parsing {
874880 }
875881
876882 if lifetimes. is_some ( ) {
877- match modifier {
878- TraitBoundModifier :: None => { }
879- TraitBoundModifier :: Maybe ( maybe) => {
880- let msg = "`for<...>` binder not allowed with `?` trait polarity modifier" ;
881- return Err ( Error :: new ( maybe. span , msg) ) ;
882- }
883+ if let Some ( maybe) = maybe {
884+ let msg = "`for<...>` binder not allowed with `?` trait polarity modifier" ;
885+ return Err ( Error :: new ( maybe. span , msg) ) ;
883886 }
884887 }
885888
@@ -888,25 +891,14 @@ pub(crate) mod parsing {
888891 } else {
889892 Ok ( Some ( TraitBound {
890893 paren_token : None ,
891- modifier ,
894+ modifiers : TraitBoundModifiers { maybe } ,
892895 lifetimes,
893896 path,
894897 } ) )
895898 }
896899 }
897900 }
898901
899- #[ cfg_attr( docsrs, doc( cfg( feature = "parsing" ) ) ) ]
900- impl Parse for TraitBoundModifier {
901- fn parse ( input : ParseStream ) -> Result < Self > {
902- if input. peek ( Token ! [ ?] ) {
903- input. parse ( ) . map ( TraitBoundModifier :: Maybe )
904- } else {
905- Ok ( TraitBoundModifier :: None )
906- }
907- }
908- }
909-
910902 #[ cfg_attr( docsrs, doc( cfg( feature = "parsing" ) ) ) ]
911903 impl Parse for ConstParam {
912904 fn parse ( input : ParseStream ) -> Result < Self > {
@@ -1154,8 +1146,8 @@ pub(crate) mod printing {
11541146 use crate :: fixup:: FixupContext ;
11551147 use crate :: generics:: {
11561148 BoundLifetimes , ConstParam , GenericParam , Generics , ImplGenerics , LifetimeParam ,
1157- PredicateLifetime , PredicateType , TraitBound , TraitBoundModifier , Turbofish , TypeGenerics ,
1158- TypeParam , WhereClause ,
1149+ PredicateLifetime , PredicateType , TraitBound , Turbofish , TypeGenerics , TypeParam ,
1150+ WhereClause ,
11591151 } ;
11601152 #[ cfg( feature = "full" ) ]
11611153 use crate :: generics:: { CapturedParam , PreciseCapture } ;
@@ -1347,7 +1339,7 @@ pub(crate) mod printing {
13471339 impl ToTokens for TraitBound {
13481340 fn to_tokens ( & self , tokens : & mut TokenStream ) {
13491341 let to_tokens = |tokens : & mut TokenStream | {
1350- self . modifier . to_tokens ( tokens) ;
1342+ self . modifiers . maybe . to_tokens ( tokens) ;
13511343 self . lifetimes . to_tokens ( tokens) ;
13521344 self . path . to_tokens ( tokens) ;
13531345 } ;
@@ -1358,16 +1350,6 @@ pub(crate) mod printing {
13581350 }
13591351 }
13601352
1361- #[ cfg_attr( docsrs, doc( cfg( feature = "printing" ) ) ) ]
1362- impl ToTokens for TraitBoundModifier {
1363- fn to_tokens ( & self , tokens : & mut TokenStream ) {
1364- match self {
1365- TraitBoundModifier :: None => { }
1366- TraitBoundModifier :: Maybe ( t) => t. to_tokens ( tokens) ,
1367- }
1368- }
1369- }
1370-
13711353 #[ cfg_attr( docsrs, doc( cfg( feature = "printing" ) ) ) ]
13721354 impl ToTokens for ConstParam {
13731355 fn to_tokens ( & self , tokens : & mut TokenStream ) {
0 commit comments