Compiling playground v0.0.1 (/playground)
error: lifetime may not live long enough
--> src/lib.rs:9:9
|
6 | impl<'a> Foobar<'a> {
| -- lifetime `'a` defined here
7 | fn borrow(&mut self) -> &'a mut u32 {
| - let's call the lifetime of this reference `'1`
8 | //do_complex_stuff_with(self.foo);
9 | self.foo
| ^^^^^^^^ method was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`
Is there a way to express what I'm trying to do in a way the borrow checker understands?
You could return the original if you removed it from the struct and gave away your only copy.
Otherwise Rust must prevent this method from being called more than once at a time, and the source of the reborrow must stay borrowed.
If you could return a "copy" of a &mut reference without freezing its owner, it would allow calling the
method more than once and break exclusivity of &mut.
Since you are dropping FooBar, you might as well consume it as below. But to get better feedback you should post a more realistic example so we can understand the constraints you have. (Also please post something that compiles.)
Not even &mut &mut u32 can do that; it's completely unsound.
impl<'a> Foobar<'a> {
fn reborrow<'s>(&'s mut self) -> &'s mut u32
where
*self.foo remains borrowed for `'s` but self does not
(to such an extent `*self` can be dropped without
invalidating the returned reborrow)