[−][src]Trait next_gen::Generator
The trait implemented by GeneratorFn
s.
Generators, also commonly referred to as coroutines, provide an ergonomic definition for iterators and other primitives, allowing to write iterators and iterator adapters in a much more imperative way, which may sometimes improve the readability of such iterators / iterator adapters.
Example
use ::next_gen::{prelude::*, GeneratorState}; fn main () { #[generator(i32)] fn generator_fn () -> &'static str { yield_!(1); return "foo" } mk_gen!(let mut generator = generator_fn()); match generator.as_mut().resume() { | GeneratorState::Yield(1) => {} | _ => panic!("unexpected return from resume"), } match generator.as_mut().resume() { | GeneratorState::Return("foo") => {} | _ => panic!("unexpected yield from resume"), } }
Associated Types
type Yield
The type of value this generator yields.
This associated type corresponds to the yield_!
expression and the
values which are allowed to be returned each time a generator yields.
For example an iterator-as-a-generator would likely have this type as
T
, the type being iterated over.
type Return
The type of value this generator returns.
This corresponds to the type returned from a generator either with a
return
statement or implicitly as the last expression of a generator
literal.
Required methods
fn resume(self: Pin<&mut Self>) -> GeneratorState<Self::Yield, Self::Return>
Resumes the execution of this generator.
This function will resume execution of the generator or start execution
if it hasn't already. This call will return back into the generator's
last suspension point, resuming execution from the latest yield_!
.
The generator will continue executing until it either yields or returns,
at which point this function will return.
Return value
The GeneratorState
enum returned from this function indicates what
state the generator is in upon returning.
If the Yield
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 resumption at a later
point.
If Return
is returned then the generator has completely finished
with the value provided. It is invalid for the generator to be resumed
again.
Panics
This function may panic if it is called after the Return
variant has
been returned previously. While generator literals in the language are
guaranteed to panic on resuming after Return
, this is not guaranteed
for all implementations of the Generator
trait.
Implementations on Foreign Types
impl<'a, G: ?Sized + 'a> Generator for Pin<&'a mut G> where
G: Generator,
[src]
G: Generator,
type Yield = G::Yield
type Return = G::Return
fn resume(
self: Pin<&mut Pin<&'a mut G>>
) -> GeneratorState<Self::Yield, Self::Return>
[src]
self: Pin<&mut Pin<&'a mut G>>
) -> GeneratorState<Self::Yield, Self::Return>
impl<'a, G: ?Sized + 'a> Generator for &'a mut G where
G: Generator + Unpin,
[src]
G: Generator + Unpin,
type Yield = G::Yield
type Return = G::Return
fn resume(
self: Pin<&mut &'a mut G>
) -> GeneratorState<Self::Yield, Self::Return>
[src]
self: Pin<&mut &'a mut G>
) -> GeneratorState<Self::Yield, Self::Return>