Hi,
I have a problem with lifetime bounds that I was not able to resolve.
The simplified code:
trait Bar {
type Assoc;
}
impl<'a, T, A> Bar for (T, fn(T) -> Box<dyn Bar<Assoc = A> + 'a>) {
type Assoc = A;
}
fn wrap<'a, T: 'a, A>(value: T, func: fn(T) -> Box<dyn Bar<Assoc = A> + 'a>) -> Box<dyn Bar<Assoc = A> + 'a> {
Box::new((value, func))
}
The error:
error[E0309]: the parameter type `A` may not live long enough
--> src/main.rs:22:5
|
21 | fn wrap<'a, T: 'a, A>(value: T, func: fn(T) -> Box<dyn Bar<Assoc = A> + 'a>) -> Box<dyn Bar<Assoc = A> + 'a> {
| -- the parameter type `A` must be valid for the lifetime `'a` as defined here...
22 | Box::new((value, func))
| ^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `A` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound
|
21 | fn wrap<'a, T: 'a, A: 'a>(value: T, func: fn(T) -> Box<dyn Bar<Assoc = A> + 'a>) -> Box<dyn Bar<Assoc = A> + 'a> {
| ++++
For more information about this error, try `rustc --explain E0309`.
I do not understand where does the lifetime requirement come from. In my actual code there are more associated types (like Bar::Assoc), some are used as input to associated functions, some as output, some are used only for their associated items and some are there only to carry the type information. In any way there should be (i.e. that is my intention) no relation between A and 'a, at least not in this fragment of code.
So ... how can I make it work without the A: 'a bound? Or is there something I am missing?