@@ -11,6 +11,7 @@ use rustc_macros::HashStable;
1111use crate :: ty:: subst:: { InternalSubsts , Subst , SubstsRef , Kind , UnpackedKind } ;
1212use crate :: ty:: { self , AdtDef , DefIdTree , TypeFlags , Ty , TyCtxt , TypeFoldable } ;
1313use crate :: ty:: { List , TyS , ParamEnvAnd , ParamEnv } ;
14+ use crate :: ty:: layout:: VariantIdx ;
1415use crate :: util:: captures:: Captures ;
1516use crate :: mir:: interpret:: { Scalar , Pointer } ;
1617
@@ -466,7 +467,44 @@ impl<'tcx> GeneratorSubsts<'tcx> {
466467}
467468
468469impl < ' a , ' gcx , ' tcx > GeneratorSubsts < ' tcx > {
470+ /// Generator have not been resumed yet
471+ pub const UNRESUMED : usize = 0 ;
472+ /// Generator has returned / is completed
473+ pub const RETURNED : usize = 1 ;
474+ /// Generator has been poisoned
475+ pub const POISONED : usize = 2 ;
476+
477+ const UNRESUMED_NAME : & ' static str = "Unresumed" ;
478+ const RETURNED_NAME : & ' static str = "Returned" ;
479+ const POISONED_NAME : & ' static str = "Panicked" ;
480+
481+ /// The variants of this Generator.
482+ #[ inline]
483+ pub fn variants ( & self , def_id : DefId , tcx : TyCtxt < ' a , ' gcx , ' tcx > ) ->
484+ impl Iterator < Item = VariantIdx >
485+ {
486+ // FIXME requires optimized MIR
487+ let num_variants = self . state_tys ( def_id, tcx) . count ( ) ;
488+ ( 0 ..num_variants) . map ( VariantIdx :: new)
489+ }
490+
491+ /// Calls `f` with a reference to the name of the enumerator for the given
492+ /// variant `v`.
493+ #[ inline]
494+ pub fn map_variant_name < R > ( & self , v : VariantIdx , f : impl FnOnce ( & str ) -> R ) -> R {
495+ let name = match v. as_usize ( ) {
496+ Self :: UNRESUMED => Self :: UNRESUMED_NAME ,
497+ Self :: RETURNED => Self :: RETURNED_NAME ,
498+ Self :: POISONED => Self :: POISONED_NAME ,
499+ _ => {
500+ return f ( & format ! ( "variant#{}" , v. as_usize( ) ) ) ;
501+ }
502+ } ;
503+ f ( name)
504+ }
505+
469506 /// The type of the state "discriminant" used in the generator type.
507+ #[ inline]
470508 pub fn discr_ty ( & self , tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> Ty < ' tcx > {
471509 tcx. types . u32
472510 }
@@ -477,6 +515,7 @@ impl<'a, 'gcx, 'tcx> GeneratorSubsts<'tcx> {
477515 ///
478516 /// The locals are grouped by their variant number. Note that some locals may
479517 /// be repeated in multiple variants.
518+ #[ inline]
480519 pub fn state_tys ( self , def_id : DefId , tcx : TyCtxt < ' a , ' gcx , ' tcx > ) ->
481520 impl Iterator < Item =impl Iterator < Item =Ty < ' tcx > > + Captures < ' gcx > + ' a >
482521 {
@@ -487,6 +526,7 @@ impl<'a, 'gcx, 'tcx> GeneratorSubsts<'tcx> {
487526
488527 /// This is the types of the fields of a generator which are not stored in a
489528 /// variant.
529+ #[ inline]
490530 pub fn prefix_tys ( self , def_id : DefId , tcx : TyCtxt < ' a , ' gcx , ' tcx > ) ->
491531 impl Iterator < Item =Ty < ' tcx > > + ' a
492532 {
0 commit comments