@@ -11,6 +11,7 @@ use rustc_macros::HashStable;
11
11
use crate :: ty:: subst:: { InternalSubsts , Subst , SubstsRef , Kind , UnpackedKind } ;
12
12
use crate :: ty:: { self , AdtDef , DefIdTree , TypeFlags , Ty , TyCtxt , TypeFoldable } ;
13
13
use crate :: ty:: { List , TyS , ParamEnvAnd , ParamEnv } ;
14
+ use crate :: ty:: layout:: VariantIdx ;
14
15
use crate :: util:: captures:: Captures ;
15
16
use crate :: mir:: interpret:: { Scalar , Pointer } ;
16
17
@@ -466,7 +467,44 @@ impl<'tcx> GeneratorSubsts<'tcx> {
466
467
}
467
468
468
469
impl < ' 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
+
469
506
/// The type of the state "discriminant" used in the generator type.
507
+ #[ inline]
470
508
pub fn discr_ty ( & self , tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> Ty < ' tcx > {
471
509
tcx. types . u32
472
510
}
@@ -477,6 +515,7 @@ impl<'a, 'gcx, 'tcx> GeneratorSubsts<'tcx> {
477
515
///
478
516
/// The locals are grouped by their variant number. Note that some locals may
479
517
/// be repeated in multiple variants.
518
+ #[ inline]
480
519
pub fn state_tys ( self , def_id : DefId , tcx : TyCtxt < ' a , ' gcx , ' tcx > ) ->
481
520
impl Iterator < Item =impl Iterator < Item =Ty < ' tcx > > + Captures < ' gcx > + ' a >
482
521
{
@@ -487,6 +526,7 @@ impl<'a, 'gcx, 'tcx> GeneratorSubsts<'tcx> {
487
526
488
527
/// This is the types of the fields of a generator which are not stored in a
489
528
/// variant.
529
+ #[ inline]
490
530
pub fn prefix_tys ( self , def_id : DefId , tcx : TyCtxt < ' a , ' gcx , ' tcx > ) ->
491
531
impl Iterator < Item =Ty < ' tcx > > + ' a
492
532
{
0 commit comments