我想存储一个回调/闭包,但是当我尝试以下简化代码时:
trait Stream<F> {
fn set_cb(&mut self, f: F)
where F: Fn(&Stream<F>);
}
pub struct SomeStream<F> {
cb: Option<F>,
}
impl<F> Stream<F> for SomeStream<F> {
fn set_cb(&mut self, f: F)
where F: Fn(&Stream<F>)
{
self.cb = Some(f)
}
}
fn main() {
let ss = SomeStream { cb: None };
let cb = |stream| {};
ss.set_cb(cb);
}
我收到以下错误:
error: type mismatch resolving `for<'r> <[closure] as core::ops::FnOnce<(&'r Stream<[closure]> + 'r,)>>::Output == ()`:
expected bound lifetime parameter ,
found concrete lifetime [E0271]
ss.set_cb(cb);
^~~~~~
help: see the detailed explanation for E0271
error: type mismatch: the type `[closure]` implements the trait `core::ops::Fn<(_,)>`, but the trait `for<'r> core::ops::Fn<(&'r Stream<[closure]> + 'r,)>` is required (expected concrete lifetime, found bound lifetime parameter ) [E0281]
ss.set_cb(cb);
^~~~~~
我没有指定任何生命周期参数。这里有什么问题?我应该用Box
包裹封口吗?