Hello and thanks in advance
I am trying to get the the trait in the following simplified example implemented, but it fails with 'cannot infer an appropriate lifetime'.
The general intention is to implement a trait which passes a FnOnce that expects to be called with an mutable reference to an instance of the same type (ideal trait notation - in terms of simplicity) :
trait DoStuff {
fn do_with_magic_computation_scope<R, F: FnOnce(&mut Self) -> R>(&mut self, f: F) -> R;
}
I want the trait to be implemented on a Context
struct that has a reference to a buffer and a scope variable.
In the actual implementation the new value for the scope variable depends on the function of the trait that was called and other parameters. The Context owns the buffer and the scope is changed before and after calling the callback with mem::replace
(so two calls per callback call). But this is too slow, thus the idea of utilizing the stack where only the buffer-reference has to be 'moved' (so far the idea). Ideally/in the next iteration, the context would no longer own but reference the scope variable (Option<&Scope>
) as well.
I cannot get the lifetimes figured out in the example above - if this is possible at all. I would prefer to adjust the trait as little as possible to not leak implementation details onto the interface.