Hi all,
I would like to store a function (or a closure) in a struct, and this function should take a method implemented on the struct as parameter and use it to store (or update) a generic type defined in the struct. Here is what I want to do:
struct Handler {
v: Option<T>,
f: Fn(Fn(T) -> ()),
}
impl Handler {
fn new(f: Fn(T) -> ())-> Handler {
Handler { v: None; f }
}
fn run(&mut self) -> () {
(self.f)(self.my_cb);
}
fn my_cb(&mut self, v: T) {
self.v = Some(v);
}
}
fn main() {
let mut h = Handler::new(|cb| { cb(5); });
h.run();
}
I removed all type constraints to make the code clear, but I tried a lot of combinations.
I know it's pretty convoluted but I keep running into lifetime issues, or "Not Sized" errors, or "Cannot be applied to Trait objects", etc. I lost count of the experiments I did...
Thank you for any help, also if it is "it can't be done".