我正在编写一个使用生成器来保存延续的库。有时我想传递一个没有悬挂点或没有yields 的闭包,但编译器抱怨闭包没有实现该Generator特征。
我想编译以下代码而不yield在闭包中添加 a; 如何让编译器将闭包视为生成器?
#![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);
}
Run Code Online (Sandbox Code Playgroud)
#![feature(generators, generator_trait)]
use std::ops::Generator;
fn library_func(mut g: Box<dyn Generator<Yield = (), Return = ()>>) …Run Code Online (Sandbox Code Playgroud)