1
1
use crate :: marker:: Unpin ;
2
2
use crate :: pin:: Pin ;
3
3
4
- /// The result of a generator resumption.
4
+ /// The result of a coroutine resumption.
5
5
///
6
6
/// This enum is returned from the `Coroutine::resume` method and indicates the
7
- /// possible return values of a generator . Currently this corresponds to either
7
+ /// possible return values of a coroutine . Currently this corresponds to either
8
8
/// a suspension point (`Yielded`) or a termination point (`Complete`).
9
9
#[ derive( Clone , Copy , PartialEq , PartialOrd , Eq , Ord , Debug , Hash ) ]
10
- #[ lang = "generator_state" ]
11
- #[ unstable( feature = "generator_trait" , issue = "43122" ) ]
10
+ #[ cfg_attr( bootstrap, lang = "generator_state" ) ]
11
+ #[ cfg_attr( not( bootstrap) , lang = "coroutine_state" ) ]
12
+ #[ unstable( feature = "coroutine_trait" , issue = "43122" ) ]
12
13
pub enum CoroutineState < Y , R > {
13
- /// The generator suspended with a value.
14
+ /// The coroutine suspended with a value.
14
15
///
15
- /// This state indicates that a generator has been suspended, and typically
16
+ /// This state indicates that a coroutine has been suspended, and typically
16
17
/// corresponds to a `yield` statement. The value provided in this variant
17
- /// corresponds to the expression passed to `yield` and allows generators to
18
+ /// corresponds to the expression passed to `yield` and allows coroutines to
18
19
/// provide a value each time they yield.
19
20
Yielded ( Y ) ,
20
21
21
- /// The generator completed with a return value.
22
+ /// The coroutine completed with a return value.
22
23
///
23
- /// This state indicates that a generator has finished execution with the
24
- /// provided value. Once a generator has returned `Complete` it is
24
+ /// This state indicates that a coroutine has finished execution with the
25
+ /// provided value. Once a coroutine has returned `Complete` it is
25
26
/// considered a programmer error to call `resume` again.
26
27
Complete ( R ) ,
27
28
}
28
29
29
- /// The trait implemented by builtin generator types.
30
+ /// The trait implemented by builtin coroutine types.
30
31
///
31
32
/// Coroutines, also commonly referred to as coroutines, are currently an
32
- /// experimental language feature in Rust. Added in [RFC 2033] generators are
33
+ /// experimental language feature in Rust. Added in [RFC 2033] coroutines are
33
34
/// currently intended to primarily provide a building block for async/await
34
35
/// syntax but will likely extend to also providing an ergonomic definition for
35
36
/// iterators and other primitives.
36
37
///
37
- /// The syntax and semantics for generators is unstable and will require a
38
+ /// The syntax and semantics for coroutines is unstable and will require a
38
39
/// further RFC for stabilization. At this time, though, the syntax is
39
40
/// closure-like:
40
41
///
41
42
/// ```rust
42
- /// #![feature(generators, generator_trait )]
43
+ /// #![feature(coroutines, coroutine_trait )]
43
44
///
44
45
/// use std::ops::{Coroutine, CoroutineState};
45
46
/// use std::pin::Pin;
46
47
///
47
48
/// fn main() {
48
- /// let mut generator = || {
49
+ /// let mut coroutine = || {
49
50
/// yield 1;
50
51
/// "foo"
51
52
/// };
52
53
///
53
- /// match Pin::new(&mut generator ).resume(()) {
54
+ /// match Pin::new(&mut coroutine ).resume(()) {
54
55
/// CoroutineState::Yielded(1) => {}
55
56
/// _ => panic!("unexpected return from resume"),
56
57
/// }
57
- /// match Pin::new(&mut generator ).resume(()) {
58
+ /// match Pin::new(&mut coroutine ).resume(()) {
58
59
/// CoroutineState::Complete("foo") => {}
59
60
/// _ => panic!("unexpected return from resume"),
60
61
/// }
61
62
/// }
62
63
/// ```
63
64
///
64
- /// More documentation of generators can be found in the [unstable book].
65
+ /// More documentation of coroutines can be found in the [unstable book].
65
66
///
66
67
/// [RFC 2033]: https://github.com/rust-lang/rfcs/pull/2033
67
- /// [unstable book]: ../../unstable-book/language-features/generators.html
68
- #[ lang = "generator" ]
69
- #[ unstable( feature = "generator_trait" , issue = "43122" ) ]
68
+ /// [unstable book]: ../../unstable-book/language-features/coroutines.html
69
+ #[ cfg_attr( bootstrap, lang = "generator" ) ]
70
+ #[ cfg_attr( not( bootstrap) , lang = "coroutine" ) ]
71
+ #[ unstable( feature = "coroutine_trait" , issue = "43122" ) ]
70
72
#[ fundamental]
71
73
pub trait Coroutine < R = ( ) > {
72
- /// The type of value this generator yields.
74
+ /// The type of value this coroutine yields.
73
75
///
74
76
/// This associated type corresponds to the `yield` expression and the
75
- /// values which are allowed to be returned each time a generator yields.
76
- /// For example an iterator-as-a-generator would likely have this type as
77
+ /// values which are allowed to be returned each time a coroutine yields.
78
+ /// For example an iterator-as-a-coroutine would likely have this type as
77
79
/// `T`, the type being iterated over.
78
80
type Yield ;
79
81
80
- /// The type of value this generator returns.
82
+ /// The type of value this coroutine returns.
81
83
///
82
- /// This corresponds to the type returned from a generator either with a
83
- /// `return` statement or implicitly as the last expression of a generator
84
+ /// This corresponds to the type returned from a coroutine either with a
85
+ /// `return` statement or implicitly as the last expression of a coroutine
84
86
/// literal. For example futures would use this as `Result<T, E>` as it
85
87
/// represents a completed future.
86
88
type Return ;
87
89
88
- /// Resumes the execution of this generator .
90
+ /// Resumes the execution of this coroutine .
89
91
///
90
- /// This function will resume execution of the generator or start execution
91
- /// if it hasn't already. This call will return back into the generator 's
92
+ /// This function will resume execution of the coroutine or start execution
93
+ /// if it hasn't already. This call will return back into the coroutine 's
92
94
/// last suspension point, resuming execution from the latest `yield`. The
93
- /// generator will continue executing until it either yields or returns, at
95
+ /// coroutine will continue executing until it either yields or returns, at
94
96
/// which point this function will return.
95
97
///
96
98
/// # Return value
97
99
///
98
100
/// The `CoroutineState` enum returned from this function indicates what
99
- /// state the generator is in upon returning. If the `Yielded` variant is
100
- /// returned then the generator has reached a suspension point and a value
101
+ /// state the coroutine is in upon returning. If the `Yielded` variant is
102
+ /// returned then the coroutine has reached a suspension point and a value
101
103
/// has been yielded out. Coroutines in this state are available for
102
104
/// resumption at a later point.
103
105
///
104
- /// If `Complete` is returned then the generator has completely finished
105
- /// with the value provided. It is invalid for the generator to be resumed
106
+ /// If `Complete` is returned then the coroutine has completely finished
107
+ /// with the value provided. It is invalid for the coroutine to be resumed
106
108
/// again.
107
109
///
108
110
/// # Panics
109
111
///
110
112
/// This function may panic if it is called after the `Complete` variant has
111
- /// been returned previously. While generator literals in the language are
113
+ /// been returned previously. While coroutine literals in the language are
112
114
/// guaranteed to panic on resuming after `Complete`, this is not guaranteed
113
115
/// for all implementations of the `Coroutine` trait.
114
116
fn resume ( self : Pin < & mut Self > , arg : R ) -> CoroutineState < Self :: Yield , Self :: Return > ;
115
117
}
116
118
117
- #[ unstable( feature = "generator_trait " , issue = "43122" ) ]
119
+ #[ unstable( feature = "coroutine_trait " , issue = "43122" ) ]
118
120
impl < G : ?Sized + Coroutine < R > , R > Coroutine < R > for Pin < & mut G > {
119
121
type Yield = G :: Yield ;
120
122
type Return = G :: Return ;
@@ -124,7 +126,7 @@ impl<G: ?Sized + Coroutine<R>, R> Coroutine<R> for Pin<&mut G> {
124
126
}
125
127
}
126
128
127
- #[ unstable( feature = "generator_trait " , issue = "43122" ) ]
129
+ #[ unstable( feature = "coroutine_trait " , issue = "43122" ) ]
128
130
impl < G : ?Sized + Coroutine < R > + Unpin , R > Coroutine < R > for & mut G {
129
131
type Yield = G :: Yield ;
130
132
type Return = G :: Return ;
0 commit comments