I have a trait and I want to implement it for all types that implement std::ops::Index
. This code works (as I would expect):
use std::ops::Index;
use std::fmt::Display;
trait Foo {
fn foo(&self, i: usize) -> &Display;
}
impl<C> Foo for C
where
C: Index<usize>,
C::Output: Display + Sized,
{
fn foo(&self, i: usize) -> &Display {
&self[i]
}
}
However, once I introduce a generic parameter to my trait, I get a strange lifetime error. This is the code (Playground):
trait Foo<T> {
fn foo(&self, i: T) -> &Display;
}
impl<C, T> Foo<T> for C
where
C: Index<T>,
C::Output: Display + Sized,
{
fn foo(&self, i: T) -> &Display {
&self[i]
}
}
And the strange error (apparently it's one error repeated three times in slightly different versions):
error[E0311]: the associated type `<C as std::ops::Index<T>>::Output` may not live long enough
--> src/main.rs:15:9
|
15 | &self[i]
| ^^^^^^^^
|
= help: consider adding an explicit lifetime bound for `<C as std::ops::Index<T>>::Output`
note: the associated type `<C as std::ops::Index<T>>::Output` must be valid for the anonymous lifetime #1 defined on the method body at 14:5...
--> src/main.rs:14:5
|
14 | / fn foo(&self, i: T) -> &Display {
15 | | &self[i]
16 | | }
| |_____^
note: ...so that the type `<C as std::ops::Index<T>>::Output` is not borrowed for too long
--> src/main.rs:15:9
|
15 | &self[i]
| ^^^^^^^^
error[E0311]: the associated type `<C as std::ops::Index<T>>::Output` may not live long enough
--> src/main.rs:15:9
|
15 | &self[i]
| ^^^^^^^^
|
= help: consider adding an explicit lifetime bound for `<C as std::ops::Index<T>>::Output`
note: the associated type `<C as std::ops::Index<T>>::Output` must be valid for the anonymous lifetime #1 defined on the method body at 14:5...
--> src/main.rs:14:5
|
14 | / fn foo(&self, i: T) -> &Display {
15 | | &self[i]
16 | | }
| |_____^
note: ...so that the type `<C as std::ops::Index<T>>::Output` will meet its required lifetime bounds
--> src/main.rs:15:9
|
15 | &self[i]
| ^^^^^^^^
error[E0311]: the associated type `<C as std::ops::Index<T>>::Output` may not live long enough
--> src/main.rs:15:10
|
15 | &self[i]
| ^^^^^^^
|
= help: consider adding an explicit lifetime bound for `<C as std::ops::Index<T>>::Output`
note: the associated type `<C as std::ops::Index<T>>::Output` must be valid for the anonymous lifetime #1 defined on the method body at 14:5...
--> src/main.rs:14:5
|
14 | / fn foo(&self, i: T) -> &Display {
15 | | &self[i]
16 | | }
| |_____^
note: ...so that the reference type `&<C as std::ops::Index<T>>::Output` does not outlive the data it points at
--> src/main.rs:15:10
|
15 | &self[i]
| ^^^^^^^
I don't understand the error at all. Especially since the error talks about the lifetime of C::Output
which (as I understand it) has nothing to do with the additional parameter K
.
Interestingly, not returning the trait object &Display
, but adding an associated type to Foo
which is returned, makes the lifetime error go away (Playground). However, this is not a solution for me.
What does this error mean? Does it make sense? Is it a compiler bug? What does the parameter K
has to do with the lifetime of C::Output
?