@@ -586,6 +586,24 @@ pub enum Clause<'tcx> {
586586 ConstArgHasType ( Const < ' tcx > , Ty < ' tcx > ) ,
587587}
588588
589+ impl < ' tcx > Binder < ' tcx , Clause < ' tcx > > {
590+ pub fn as_trait_clause ( self ) -> Option < Binder < ' tcx , TraitPredicate < ' tcx > > > {
591+ if let ty:: Clause :: Trait ( trait_clause) = self . skip_binder ( ) {
592+ Some ( self . rebind ( trait_clause) )
593+ } else {
594+ None
595+ }
596+ }
597+
598+ pub fn as_projection_clause ( self ) -> Option < Binder < ' tcx , ProjectionPredicate < ' tcx > > > {
599+ if let ty:: Clause :: Projection ( projection_clause) = self . skip_binder ( ) {
600+ Some ( self . rebind ( projection_clause) )
601+ } else {
602+ None
603+ }
604+ }
605+ }
606+
589607#[ derive( Clone , Copy , PartialEq , Eq , Hash , TyEncodable , TyDecodable ) ]
590608#[ derive( HashStable , TypeFoldable , TypeVisitable , Lift ) ]
591609pub enum PredicateKind < ' tcx > {
@@ -1203,6 +1221,17 @@ impl<'tcx> ToPredicate<'tcx> for TraitRef<'tcx> {
12031221 }
12041222}
12051223
1224+ impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , Clause < ' tcx > > > for TraitRef < ' tcx > {
1225+ #[ inline( always) ]
1226+ fn to_predicate ( self , _tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , Clause < ' tcx > > {
1227+ Binder :: dummy ( Clause :: Trait ( TraitPredicate {
1228+ trait_ref : self ,
1229+ constness : ty:: BoundConstness :: NotConst ,
1230+ polarity : ty:: ImplPolarity :: Positive ,
1231+ } ) )
1232+ }
1233+ }
1234+
12061235impl < ' tcx > ToPredicate < ' tcx > for Binder < ' tcx , TraitRef < ' tcx > > {
12071236 #[ inline( always) ]
12081237 fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Predicate < ' tcx > {
@@ -1211,6 +1240,14 @@ impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, TraitRef<'tcx>> {
12111240 }
12121241}
12131242
1243+ impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , Clause < ' tcx > > > for Binder < ' tcx , TraitRef < ' tcx > > {
1244+ #[ inline( always) ]
1245+ fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , Clause < ' tcx > > {
1246+ let pred: PolyTraitPredicate < ' tcx > = self . to_predicate ( tcx) ;
1247+ pred. to_predicate ( tcx)
1248+ }
1249+ }
1250+
12141251impl < ' tcx > ToPredicate < ' tcx , PolyTraitPredicate < ' tcx > > for Binder < ' tcx , TraitRef < ' tcx > > {
12151252 #[ inline( always) ]
12161253 fn to_predicate ( self , _: TyCtxt < ' tcx > ) -> PolyTraitPredicate < ' tcx > {
@@ -1240,6 +1277,12 @@ impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> {
12401277 }
12411278}
12421279
1280+ impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , Clause < ' tcx > > > for PolyTraitPredicate < ' tcx > {
1281+ fn to_predicate ( self , _tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , Clause < ' tcx > > {
1282+ self . map_bound ( |p| Clause :: Trait ( p) )
1283+ }
1284+ }
1285+
12431286impl < ' tcx > ToPredicate < ' tcx > for PolyRegionOutlivesPredicate < ' tcx > {
12441287 fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Predicate < ' tcx > {
12451288 self . map_bound ( |p| PredicateKind :: Clause ( Clause :: RegionOutlives ( p) ) ) . to_predicate ( tcx)
@@ -1258,6 +1301,12 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
12581301 }
12591302}
12601303
1304+ impl < ' tcx > ToPredicate < ' tcx , Binder < ' tcx , Clause < ' tcx > > > for PolyProjectionPredicate < ' tcx > {
1305+ fn to_predicate ( self , _tcx : TyCtxt < ' tcx > ) -> Binder < ' tcx , Clause < ' tcx > > {
1306+ self . map_bound ( |p| Clause :: Projection ( p) )
1307+ }
1308+ }
1309+
12611310impl < ' tcx > ToPredicate < ' tcx > for TraitPredicate < ' tcx > {
12621311 fn to_predicate ( self , tcx : TyCtxt < ' tcx > ) -> Predicate < ' tcx > {
12631312 PredicateKind :: Clause ( Clause :: Trait ( self ) ) . to_predicate ( tcx)
@@ -1327,6 +1376,23 @@ impl<'tcx> Predicate<'tcx> {
13271376 | PredicateKind :: TypeWellFormedFromEnv ( ..) => None ,
13281377 }
13291378 }
1379+
1380+ pub fn as_clause ( self ) -> Option < Binder < ' tcx , Clause < ' tcx > > > {
1381+ let predicate = self . kind ( ) ;
1382+ match predicate. skip_binder ( ) {
1383+ PredicateKind :: Clause ( clause) => Some ( predicate. rebind ( clause) ) ,
1384+ PredicateKind :: AliasRelate ( ..)
1385+ | PredicateKind :: Subtype ( ..)
1386+ | PredicateKind :: Coerce ( ..)
1387+ | PredicateKind :: WellFormed ( ..)
1388+ | PredicateKind :: ObjectSafe ( ..)
1389+ | PredicateKind :: ClosureKind ( ..)
1390+ | PredicateKind :: ConstEvaluatable ( ..)
1391+ | PredicateKind :: ConstEquate ( ..)
1392+ | PredicateKind :: Ambiguous
1393+ | PredicateKind :: TypeWellFormedFromEnv ( ..) => None ,
1394+ }
1395+ }
13301396}
13311397
13321398/// Represents the bounds declared on a particular set of type
0 commit comments