I'm rather puzzled by this error:
trait Foo {
type T<'a>;
fn foo<'a>(t: Self::T<'a>) -> Self::T<'a>;
}
impl Foo for () {
type T<'a> = &'a u8;
// ERROR: lifetime params do not match
// Expected: OK because Self::T<'a> = &'a u8
fn foo<'a>(t: &'a u8) -> Self::T<'a> {
t
}
}
impl Foo for u8 {
type T<'a> = &'a u8;
// this works
fn foo<'a>(t: Self::T<'a>) -> Self::T<'a> {
t
}
}
error[E0195]: lifetime parameters or bounds on associated function `foo` do not match the trait declaration
--> src/lib.rs:11:11
|
3 | fn foo<'a>(t: Self::T<'a>) -> Self::T<'a>;
| ---- lifetimes in impl do not match this associated function in trait
...
11 | fn foo<'a>(t: &'a u8) -> Self::T<'a> {
| ^^^^ lifetimes do not match associated function in trait
For more information about this error, try `rustc --explain E0195`.
Since Self::T<'a> = &'a u8
, I expected to be able to use &'a u8
in place of Self::T<'a>
in the signature of the implementation, but it seems like Self::T<'a>
is treated as distinct from &'a u8
.
This substitution works if there's no return type for the method (playground).