Supertrait for parameterized trait

How can I define the signature of a supertrait for a trait that has a type parameter?

Like this:

pub trait Foo < T > : Trait1 + Trait2 {...}

pub trait Bar: Trait3 + Trait4 + Foo < T > {...}

Doing like this gives the 'not found in scope' error for type T

Bar would also have to be generic over T to be able to supply a type parameter to Foo unless you want to use a concrete type.

trait Bar<T>: Trait3 + Trait4 + Foo<T> { /* */ }
trait Bar: Trait3 + Trait4 + Foo<u8> { /* */ }
1 Like

It is not possible to directly write down the constraint that in order to implement Bar, there must be a type T such that Self implements Foo<T> directly. You may attempt this:

trait AnyFoo {}

impl<T, U> AnyFoo for U
    U: Foo<T>,

but it will fail too, due to the fact that if U implements both Foo<T1> and Foo<T2> for two different types T1 and T2, you end up with two conflicting impls for one trait for one type.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.