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