Struct genawaiter::sync::Gen
source · pub struct Gen<Y, R, F: Future> { /* private fields */ }
Expand description
This is a generator which can be shared between threads.
Implementations§
source§impl<Y, R, C> Gen<Y, R, Pin<Box<dyn Future<Output = C> + Send>>>
impl<Y, R, C> Gen<Y, R, Pin<Box<dyn Future<Output = C> + Send>>>
sourcepub fn new_boxed<F>(producer: impl FnOnce(Co<Y, R>) -> F) -> Self
pub fn new_boxed<F>(producer: impl FnOnce(Co<Y, R>) -> F) -> Self
Creates a new generator with a boxed future, so it can be stored in a
static
.
This works exactly the same as Gen::new
with an immediately boxed future.
This method exists solely to help the compiler with type inference. These two lines are equivalent, except that the compiler cannot infer the correct type on the second line:
let _: GenBoxed<i32> = Gen::new_boxed(|co| producer(co));
let _: GenBoxed<i32> = Gen::new(|co| Box::pin(producer(co)));
source§impl<Y, R, F: Future> Gen<Y, R, F>
impl<Y, R, F: Future> Gen<Y, R, F>
sourcepub fn new(producer: impl FnOnce(Co<Y, R>) -> F) -> Self
pub fn new(producer: impl FnOnce(Co<Y, R>) -> F) -> Self
Creates a new generator from a function.
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 returnPoll::Pending
. - Drop the
Co
, and then returnPoll::Ready
.
Typically this exchange will happen in the context of an async fn
.
sourcepub fn resume_with(&mut self, arg: R) -> GeneratorState<Y, F::Output>
pub fn resume_with(&mut self, arg: R) -> GeneratorState<Y, F::Output>
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.
source§impl<Y, F: Future> Gen<Y, (), F>
impl<Y, F: Future> Gen<Y, (), F>
sourcepub fn resume(&mut self) -> GeneratorState<Y, F::Output>
pub fn resume(&mut self) -> GeneratorState<Y, F::Output>
Resumes execution of the generator.
If the generator yields a value, Yielded
is returned. Otherwise,
Completed
is returned.
sourcepub fn async_resume(
&mut self,
) -> impl Future<Output = GeneratorState<Y, F::Output>> + '_
pub fn async_resume( &mut self, ) -> impl Future<Output = GeneratorState<Y, F::Output>> + '_
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.