I'm writing a library that uses generators to hold continuations. Sometimes I want to pass a closure with no suspension points, or no yield
s, but the compiler complains that the closure doesn't implement the Generator
trait.
I want to compile the following code without adding a yield
to the closure; how can I let the compiler treat the closure as a generator?
#![feature(generators, generator_trait)]
use std::ops::Generator;
fn library_func(mut g: Box<dyn Generator<Yield = (), Return = ()>>) {
let x = unsafe { g.resume() };
println!("{:?}", x);
}
fn main() {
// a closure without yield
let x = Box::new(|| {
// uncommenting this line makes it compile, but changes the behavior
// yield ();
});
library_func(x);
}
error[E0277]: the trait bound `[closure@src/main.rs:12:22: 15:6]: std::ops::Generator` is not satisfied
--> src/main.rs:17:18
|
17 | library_func(x);
| ^ the trait `std::ops::Generator` is not implemented for `[closure@src/main.rs:12:22: 15:6]`
|
= note: required for the cast to the object type `dyn std::ops::Generator<Yield=(), Return=()>`