1
Fork 0

s/Generator/Coroutine/

This commit is contained in:
Oli Scherer 2023-10-19 16:06:43 +00:00
parent 96027d945b
commit 60956837cf
310 changed files with 1271 additions and 1271 deletions

View file

@ -38,7 +38,7 @@ pub use poll_fn::{poll_fn, PollFn};
/// This type is needed because:
///
/// a) Generators cannot implement `for<'a, 'b> Generator<&'a mut Context<'b>>`, so we need to pass
/// a) Coroutines cannot implement `for<'a, 'b> Coroutine<&'a mut Context<'b>>`, so we need to pass
/// a raw pointer (see <https://github.com/rust-lang/rust/issues/68923>).
/// b) Raw pointers and `NonNull` aren't `Send` or `Sync`, so that would make every single future
/// non-Send/Sync as well, and we don't want that.

View file

@ -1,5 +1,5 @@
use crate::fmt;
use crate::ops::{Generator, GeneratorState};
use crate::ops::{Coroutine, CoroutineState};
use crate::pin::Pin;
/// Creates a new iterator where each iteration calls the provided generator.
@ -24,8 +24,8 @@ use crate::pin::Pin;
/// ```
#[inline]
#[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")]
pub fn from_generator<G: Generator<Return = ()> + Unpin>(generator: G) -> FromGenerator<G> {
FromGenerator(generator)
pub fn from_generator<G: Coroutine<Return = ()> + Unpin>(generator: G) -> FromCoroutine<G> {
FromCoroutine(generator)
}
/// An iterator over the values yielded by an underlying generator.
@ -36,23 +36,23 @@ pub fn from_generator<G: Generator<Return = ()> + Unpin>(generator: G) -> FromGe
/// [`iter::from_generator()`]: from_generator
#[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")]
#[derive(Clone)]
pub struct FromGenerator<G>(G);
pub struct FromCoroutine<G>(G);
#[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")]
impl<G: Generator<Return = ()> + Unpin> Iterator for FromGenerator<G> {
impl<G: Coroutine<Return = ()> + Unpin> Iterator for FromCoroutine<G> {
type Item = G::Yield;
fn next(&mut self) -> Option<Self::Item> {
match Pin::new(&mut self.0).resume(()) {
GeneratorState::Yielded(n) => Some(n),
GeneratorState::Complete(()) => None,
CoroutineState::Yielded(n) => Some(n),
CoroutineState::Complete(()) => None,
}
}
}
#[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")]
impl<G> fmt::Debug for FromGenerator<G> {
impl<G> fmt::Debug for FromCoroutine<G> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FromGenerator").finish()
f.debug_struct("FromCoroutine").finish()
}
}

View file

@ -3,13 +3,13 @@ use crate::pin::Pin;
/// The result of a generator resumption.
///
/// This enum is returned from the `Generator::resume` method and indicates the
/// This enum is returned from the `Coroutine::resume` method and indicates the
/// possible return values of a generator. Currently this corresponds to either
/// a suspension point (`Yielded`) or a termination point (`Complete`).
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
#[lang = "generator_state"]
#[unstable(feature = "generator_trait", issue = "43122")]
pub enum GeneratorState<Y, R> {
pub enum CoroutineState<Y, R> {
/// The generator suspended with a value.
///
/// This state indicates that a generator has been suspended, and typically
@ -28,7 +28,7 @@ pub enum GeneratorState<Y, R> {
/// The trait implemented by builtin generator types.
///
/// Generators, also commonly referred to as coroutines, are currently an
/// Coroutines, also commonly referred to as coroutines, are currently an
/// experimental language feature in Rust. Added in [RFC 2033] generators are
/// currently intended to primarily provide a building block for async/await
/// syntax but will likely extend to also providing an ergonomic definition for
@ -41,7 +41,7 @@ pub enum GeneratorState<Y, R> {
/// ```rust
/// #![feature(generators, generator_trait)]
///
/// use std::ops::{Generator, GeneratorState};
/// use std::ops::{Coroutine, CoroutineState};
/// use std::pin::Pin;
///
/// fn main() {
@ -51,11 +51,11 @@ pub enum GeneratorState<Y, R> {
/// };
///
/// match Pin::new(&mut generator).resume(()) {
/// GeneratorState::Yielded(1) => {}
/// CoroutineState::Yielded(1) => {}
/// _ => panic!("unexpected return from resume"),
/// }
/// match Pin::new(&mut generator).resume(()) {
/// GeneratorState::Complete("foo") => {}
/// CoroutineState::Complete("foo") => {}
/// _ => panic!("unexpected return from resume"),
/// }
/// }
@ -68,7 +68,7 @@ pub enum GeneratorState<Y, R> {
#[lang = "generator"]
#[unstable(feature = "generator_trait", issue = "43122")]
#[fundamental]
pub trait Generator<R = ()> {
pub trait Coroutine<R = ()> {
/// The type of value this generator yields.
///
/// This associated type corresponds to the `yield` expression and the
@ -95,10 +95,10 @@ pub trait Generator<R = ()> {
///
/// # Return value
///
/// The `GeneratorState` enum returned from this function indicates what
/// The `CoroutineState` enum returned from this function indicates what
/// state the generator is in upon returning. If the `Yielded` variant is
/// returned then the generator has reached a suspension point and a value
/// has been yielded out. Generators in this state are available for
/// has been yielded out. Coroutines in this state are available for
/// resumption at a later point.
///
/// If `Complete` is returned then the generator has completely finished
@ -110,26 +110,26 @@ pub trait Generator<R = ()> {
/// This function may panic if it is called after the `Complete` variant has
/// been returned previously. While generator literals in the language are
/// guaranteed to panic on resuming after `Complete`, this is not guaranteed
/// for all implementations of the `Generator` trait.
fn resume(self: Pin<&mut Self>, arg: R) -> GeneratorState<Self::Yield, Self::Return>;
/// for all implementations of the `Coroutine` trait.
fn resume(self: Pin<&mut Self>, arg: R) -> CoroutineState<Self::Yield, Self::Return>;
}
#[unstable(feature = "generator_trait", issue = "43122")]
impl<G: ?Sized + Generator<R>, R> Generator<R> for Pin<&mut G> {
impl<G: ?Sized + Coroutine<R>, R> Coroutine<R> for Pin<&mut G> {
type Yield = G::Yield;
type Return = G::Return;
fn resume(mut self: Pin<&mut Self>, arg: R) -> GeneratorState<Self::Yield, Self::Return> {
fn resume(mut self: Pin<&mut Self>, arg: R) -> CoroutineState<Self::Yield, Self::Return> {
G::resume((*self).as_mut(), arg)
}
}
#[unstable(feature = "generator_trait", issue = "43122")]
impl<G: ?Sized + Generator<R> + Unpin, R> Generator<R> for &mut G {
impl<G: ?Sized + Coroutine<R> + Unpin, R> Coroutine<R> for &mut G {
type Yield = G::Yield;
type Return = G::Return;
fn resume(mut self: Pin<&mut Self>, arg: R) -> GeneratorState<Self::Yield, Self::Return> {
fn resume(mut self: Pin<&mut Self>, arg: R) -> CoroutineState<Self::Yield, Self::Return> {
G::resume(Pin::new(&mut *self), arg)
}
}

View file

@ -199,7 +199,7 @@ pub use self::try_trait::Residual;
pub(crate) use self::try_trait::{ChangeOutputType, NeverShortCircuit};
#[unstable(feature = "generator_trait", issue = "43122")]
pub use self::generator::{Generator, GeneratorState};
pub use self::generator::{Coroutine, CoroutineState};
#[unstable(feature = "coerce_unsized", issue = "18598")]
pub use self::unsize::CoerceUnsized;

View file

@ -1085,16 +1085,16 @@ impl<P, U> DispatchFromDyn<Pin<U>> for Pin<P> where P: DispatchFromDyn<U> {}
/// # assert_eq!(42, block_on(async { 42 }));
/// ```
///
/// ### With `Generator`s
/// ### With `Coroutine`s
///
/// ```rust
/// #![feature(generators, generator_trait)]
/// use core::{
/// ops::{Generator, GeneratorState},
/// ops::{Coroutine, CoroutineState},
/// pin::pin,
/// };
///
/// fn generator_fn() -> impl Generator<Yield = usize, Return = ()> /* not Unpin */ {
/// fn generator_fn() -> impl Coroutine<Yield = usize, Return = ()> /* not Unpin */ {
/// // Allow generator to be self-referential (not `Unpin`)
/// // vvvvvv so that locals can cross yield points.
/// static || {
@ -1109,16 +1109,16 @@ impl<P, U> DispatchFromDyn<Pin<U>> for Pin<P> where P: DispatchFromDyn<U> {}
/// fn main() {
/// let mut generator = pin!(generator_fn());
/// match generator.as_mut().resume(()) {
/// GeneratorState::Yielded(0) => {},
/// CoroutineState::Yielded(0) => {},
/// _ => unreachable!(),
/// }
/// match generator.as_mut().resume(()) {
/// GeneratorState::Yielded(3) => {},
/// CoroutineState::Yielded(3) => {},
/// _ => unreachable!(),
/// }
/// match generator.resume(()) {
/// GeneratorState::Yielded(_) => unreachable!(),
/// GeneratorState::Complete(()) => {},
/// CoroutineState::Yielded(_) => unreachable!(),
/// CoroutineState::Complete(()) => {},
/// }
/// }
/// ```

View file

@ -3,7 +3,7 @@
use core::fmt;
use core::future::Future;
use core::marker::Tuple;
use core::ops::{Generator, GeneratorState};
use core::ops::{Coroutine, CoroutineState};
use core::pin::Pin;
use core::task::{Context, Poll};
@ -207,15 +207,15 @@ where
}
#[unstable(feature = "generator_trait", issue = "43122")] // also #98407
impl<R, G> Generator<R> for Exclusive<G>
impl<R, G> Coroutine<R> for Exclusive<G>
where
G: Generator<R> + ?Sized,
G: Coroutine<R> + ?Sized,
{
type Yield = G::Yield;
type Return = G::Return;
#[inline]
fn resume(self: Pin<&mut Self>, arg: R) -> GeneratorState<Self::Yield, Self::Return> {
fn resume(self: Pin<&mut Self>, arg: R) -> CoroutineState<Self::Yield, Self::Return> {
G::resume(self.get_pin_mut(), arg)
}
}