Let's suppose I have code like this:
struct Struct0 {
s1: Struct1,
v: Vec<Box<dyn Tr>>
}
impl Struct0 {
fn a(&mut self) {
self.v = self.s1.func();
}
}
struct Struct1 {}
impl<'a> Struct1 {
fn func(&'a self) -> Vec<Box<dyn Tr + 'a>> {
vec![Box::new(Struct2 { some_reference: &self } )]
}
}
trait Tr {
//...
}
struct Struct2<'a> {
some_reference: &'a Struct1
}
impl Tr for Struct2<'_> {
//...
}
fn main() {
}
This gives the following error:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/main.rs:8:26
|
8 | self.v = self.s1.func();
| ^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 7:5...
--> src/main.rs:7:5
|
7 | / fn a(&mut self) {
8 | | self.v = self.s1.func();
9 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:8:18
|
8 | self.v = self.s1.func();
| ^^^^^^^
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the expression is assignable
--> src/main.rs:8:18
|
8 | self.v = self.s1.func();
| ^^^^^^^^^^^^^^
= note: expected `std::vec::Vec<std::boxed::Box<(dyn Tr + 'static)>>`
found `std::vec::Vec<std::boxed::Box<dyn Tr>>`
How can I get this assignment to work?