I want to use closure for a callback-like design with a late setup and have problems coping with lifetime:
The following code does not compile:
And the suggestion by compiler to use static does not help either, because of the reference to self in the closure instance.
Is there a way to solve this?
You can't do this. You are trying to create a self-referential type and that's extremely hard to do in Rust. I would recommend trying a solution to your problem. What are you trying to do?
Note that you need to take foo out of the option in order to call it successfully:
fn call_foo(&mut self) {
// This uses Option::take to remove the Foo from self.
// This is needed to avoid self being borrowed when trying to give
// a reference to self to the closure
if let Some(inner) = self.foo.take() {
// Call the closure. Note that foo is None while calling the closure.
(inner.foo)(self);
// Put the closure back after calling it.
self.foo = Some(inner);
}
}
Thanks, this is an interesting approach, but I can't use Rc as my app is no-std.
Narrowing my goal even further, what I want to do is to create a boxed closure that will call a function in a struct. It seems that whatever way I approach it, the reference to the struct has to be 'static.
That said, if you really want, you can likely also solve the issue using raw pointers and unsafe, though then you'll have to manually make sure your references are not dangling.