Disallow returning from generators. It includes explicit return
expression and "reaching the end" i.e. the body must be !
(never type).
Remove GeneratorState
type. Output
type is the argument of yield
expression.
- Currently, generators will panic when resumed after completed. Panics should be explicit and visible, not default and invisible.
- Reduce types. Only
FnPin
trait is needed. - The expected "No
Yielded
afterComplete
" is not guaranteed in the type system. -
Future
trait used to haveError
type. It is now removed in favor of an explicit way. The situation is somewhat similar. - More symmetry of coroutine and function (routine). Also more appropriate for "generator" name.
- When we adopt
try
block,?
operator can be used for the kind of control flow.
// Generators implement this trait.
trait FnPin<Args> : FnOnce<Args> {
fn call_pin(self: Pin<&mut Self>, args: Args) -> Self::Output;
}
// implements FnPin<(), Output=Option<i32>>
let iterator_like = || {
yield Some(1);
yield Some(2);
loop { yield None } // This is naturally a fused iterator.
};
// implements for<'a> FnPin<&'a mut Context, Output=Poll<T>>
let future_like = |ctx| {
let value = loop {
match sub_future.poll(ctx) {
Poll::Ready(value) => break value,
Poll::Pending => yield Poll::Pending,
}
};
let result = some_work(value);
yield Poll::Ready(result);
panic!("polled after ready");
};
Links
- Original generator RFC: eRFC: Experimentally add coroutines to Rust #2033.
- Talking interaction of generator and iterator: [Pre-RFC]: Generator integration with for loops.
- New RFC on generator argument: Unified coroutines a.k.a. Generator resume arguments.