I would like to store a callback/closure, but when I try the following reduced code:
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);
}
I get the following error:
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);
^~~~~~
I didn't specify any lifetime parameters. What's wrong here? Should I wrap the closure in a Box
?