[][src]Struct genawaiter::stack::Gen

pub struct Gen<'s, Y, R, F: Future> { /* fields omitted */ }

This is a generator which can be stack-allocated.

See the module-level docs for examples.

Methods

impl<'s, Y, R, F: Future> Gen<'s, Y, R, F>[src]

pub unsafe fn new(
    shelf: &'s mut Shelf<Y, R, F>,
    producer: impl FnOnce(Co<'s, Y, R>) -> F
) -> Self
[src]

Creates a new generator from a function.

The state of the generator is stored in shelf, which will be pinned in place while this generator exists. The generator itself is movable, since it just holds a reference to the pinned state.

The function accepts a Co object, and returns a future. Every time the generator is resumed, the future is polled. Each time the future is polled, it should do one of two things:

  • Call co.yield_(), and then return Poll::Pending.
  • Drop the Co, and then return Poll::Ready.

Typically this exchange will happen in the context of an async fn.

Safety

The Co object must not outlive the returned Gen. By time the generator completes (i.e., by time the producer's Future returns Poll::Ready), the Co object should already have been dropped. If this invariant is not upheld, memory unsafety can result.

Afaik, the Rust compiler is not flexible enough to let you express this invariant in the type system, but I would love to be proven wrong!

Examples

let mut shelf = Shelf::new();
let gen = unsafe { Gen::new(&mut shelf, producer) };

pub fn resume_with(&mut self, arg: R) -> GeneratorState<Y, F::Output>[src]

Resumes execution of the generator.

arg is the resume argument. If the generator was previously paused by awaiting a future returned from co.yield(), that future will complete, and return arg.

If the generator yields a value, Yielded is returned. Otherwise, Completed is returned.

See the module-level docs for examples.

impl<'s, Y, F: Future> Gen<'s, Y, (), F>[src]

pub fn resume(&mut self) -> GeneratorState<Y, F::Output>[src]

Resumes execution of the generator.

If the generator yields a value, Yielded is returned. Otherwise, Completed is returned.

See the module-level docs for examples.

pub fn async_resume(
    &mut self
) -> impl Future<Output = GeneratorState<Y, F::Output>> + '_
[src]

Resumes execution of the generator.

If the generator pauses without yielding, Poll::Pending is returned. If the generator yields a value, Poll::Ready(Yielded) is returned. Otherwise, Poll::Ready(Completed) is returned.

See the module-level docs for examples.

Trait Implementations

impl<'s, Y, R, F: Future> Coroutine for Gen<'s, Y, R, F>[src]

type Yield = Y

The type of value this generator yields.

type Resume = R

The type of value this generator accepts as a resume argument.

type Return = F::Output

The type of value this generator returns upon completion.

impl<'s, Y, R, F: Future> Drop for Gen<'s, Y, R, F>[src]

impl<'s, Y, F: Future<Output = ()>> IntoIterator for Gen<'s, Y, (), F>[src]

type Item = Y

The type of the elements being iterated over.

type IntoIter = IntoIter<'s, Y, F>

Which kind of iterator are we turning this into?

impl<'r, 's, Y, F: Future<Output = ()>> IntoIterator for &'r mut Gen<'s, Y, (), F>[src]

type Item = Y

The type of the elements being iterated over.

type IntoIter = MutIntoIter<'r, 's, Y, F>

Which kind of iterator are we turning this into?

Auto Trait Implementations

impl<'s, Y, R, F> !RefUnwindSafe for Gen<'s, Y, R, F>

impl<'s, Y, R, F> Send for Gen<'s, Y, R, F> where
    F: Send,
    R: Send,
    Y: Send

impl<'s, Y, R, F> !Sync for Gen<'s, Y, R, F>

impl<'s, Y, R, F> Unpin for Gen<'s, Y, R, F>

impl<'s, Y, R, F> !UnwindSafe for Gen<'s, Y, R, F>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.