Issue with generic impl not compiling

This is an example of my code. The error says there are conflicting implementations of trait Foo<std::vec::Vec<u8>> but I don't see why this happens because the third impl should only be happening where S: Foo and I didn't impl Foo.

use std::io::Read;

fn main() {
}

trait Foo<T> {
    fn example() -> String;
}

impl<S> Foo<Vec<u8>> for S where S: Read {
    fn example() -> String {
        String::from("Vec<u8>")
    }
}

impl<S> Foo<u32> for S where S: Read {
    fn example() -> String {
        String::from("u32")
    }
}

impl<S,T> Foo<Vec<T>> for S where S: Foo<T> {
    fn example() -> String {
        String::from("anything")
    }
}

It works if in the last impl I switch from S to an actual type.
For example: Rust Playground

1 Like

In the third trait, replace T with u8, and the problem should be clear. Note that nothing says that a struct cannot implement both Read and Foo<u8>.

I think default impls RFC is supposed to help with it, but currently it's not implemented, even in nightly compiler.

1 Like