I have a trait Surface: 'static
that I want to implement for a struct Obj<'a>
. The trait needs to be 'static
because I want to store objects from type Surface
in a Vec<Box<Surface>>
.
In the first step I tried this.
impl<'a> Surface for Obj<'a> {}
This will not work because of a lifetime mismatch between 'static
and 'a
. In other words: Surface
can live longer than Obj
because Surface
is 'static
.
I changed my implementation as follows.
impl<'a> Surface for Obj<'a> where 'a: 'static {}
As far as I understand the documentation correctly, what I'm doing is, 'a
can outlive 'static
. Do I want this?
If I transfer the ownership of Obj<'a>
, the compiler tells me that a mutable reference inside Obj
will not live long enough and is still borrowed.
Here is a short example.
trait Surface: 'static {}
struct Manager {
storage: Vec<Box<Surface>>,
}
impl Manager {
fn add(&mut self, surface: impl Surface) {
self.storage.push(Box::new(surface));
}
}
struct SomeOtherStruct {}
struct Obj<'a> {
data: &'a mut SomeOtherStruct,
}
impl<'a> Obj<'a> {
fn new(some_struct: &'a mut SomeOtherStruct) -> Self {
Obj { data: some_struct }
}
}
impl<'a> Surface for Obj<'a> where 'a: 'static {}
fn main() {
let mut some_struct = SomeOtherStruct {};
let mut manager = Manager {
storage: Vec::new(),
};
let obj = Obj::new(&mut some_struct);
manager.add(obj);
}
error[E0597]: `some_struct` does not live long enough
--> src/main.rs:33:24
|
33 | let obj = Obj::new(&mut some_struct);
| ---------^^^^^^^^^^^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `some_struct` is borrowed for `'static`
34 | manager.add(obj);
35 | }
| - `some_struct` dropped here while still borrowed
In other words &mut some_struct
is lifetime 'a
but needs 'static
. Ok it's clear because some_struct
lives in Obj<'a>
so it cannot be 'static
?
Is this what I'm trying to do "Rust like"? I've no idea how to get it to work. Its really confusing with the lifetimes. I think I can get around this by using a Rc<T>
, but this will make things more complex.