pub trait Coroutine<R = ()> {
type Yield;
type Return;
// Required method
fn resume(
self: Pin<&mut Self>,
arg: R,
) -> CoroutineState<Self::Yield, Self::Return>;
}
coroutine_trait
)Expand description
The trait implemented by builtin coroutine types.
Coroutines are currently an experimental language feature in Rust. Added in RFC 2033 coroutines are currently intended to primarily provide a building block for async/await syntax but will likely extend to also providing an ergonomic definition for iterators and other primitives.
The syntax and semantics for coroutines is unstable and will require a further RFC for stabilization. At this time, though, the syntax is closure-like:
#![feature(coroutines)]
#![feature(coroutine_trait)]
#![feature(stmt_expr_attributes)]
use std::ops::{Coroutine, CoroutineState};
use std::pin::Pin;
fn main() {
let mut coroutine = #[coroutine] || {
yield 1;
"foo"
};
match Pin::new(&mut coroutine).resume(()) {
CoroutineState::Yielded(1) => {}
_ => panic!("unexpected return from resume"),
}
match Pin::new(&mut coroutine).resume(()) {
CoroutineState::Complete("foo") => {}
_ => panic!("unexpected return from resume"),
}
}
More documentation of coroutines can be found in the unstable book.
Required Associated Types§
sourcetype Yield
🔬This is a nightly-only experimental API. (coroutine_trait
)
type Yield
coroutine_trait
)The type of value this coroutine yields.
This associated type corresponds to the yield
expression and the
values which are allowed to be returned each time a coroutine yields.
For example an iterator-as-a-coroutine would likely have this type as
T
, the type being iterated over.