Reputation: 63
When I run this code that uses higher-ranked trait bounds:
pub trait MyTrait<'a> {
type Output: 'a;
fn gimme_value(&self) -> Self::Output;
}
pub fn meow<T: for<'a> MyTrait<'a> + 'static>(val: &T) -> T::Output {
val.gimme_value()
}
I'm seeing this error:
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context
How can I make my function meow
return this associated type, while still allowing T
to be a higher ranked trait bound?
Upvotes: 0
Views: 107
Reputation: 63
Just add a new generic to the meow
function — let's call it R
. When adding the trait restriction for T
, define that T::Output
is equal to R
. Then, have the function return R
instead of T::Output
:
pub trait MyTrait<'a> {
type Output: 'a;
fn gimme_value(&self) -> Self::Output;
}
pub fn meow<R, T: for<'a> MyTrait<'a, Output=R> + 'static>(val: &T) -> R {
val.gimme_value()
}
Upvotes: 1