J. Aarts
J. Aarts

Reputation: 166

conflicting implementation for `std::vec::Vec<_>` when using auto traits

I have 2 traits, trait A being a subtrait for trait B:

pub trait A {...}
pub trait B {...} //A+some methods

And an autotrait for everything not u8:

pub auto trait IsNotU8 {}
impl !IsNotU8 for u8 {}

An implementation of A for u8:

impl A for Vec<u8> {...}

And because for all types implementing A, all other methods in B all do nothing I implement B for all types implementing A:

impl<K: A> B for K {...} //already implemented here

And for a Vec:

impl<V: B + IsNotU8> B for Vec<V> {...} //ERROR already implemented

I get conflicting implementation for `std::vec::Vec<_>` even thought the second implementation only works for values that are NOT u8, And the first implementation is only for types implementing A. Since only Vec is implementing A this should not be a problem because that none of the types overlap.

Why am I getting this error? When I don't use the indirection of implementing B for A this works, but with it doesn't. Is this a bug in the compiler when using auto traits?

Upvotes: 1

Views: 213

Answers (1)

jplatte
jplatte

Reputation: 1141

While there's only an A implementation for Vec<u8> in the code you showed, it could be added later, and then your B implementations would be conflicting. More generally, the compiler will always assume that any missing trait implementation could be added later (except of course your case of an auto trait that is explicitly not implemented for some type).

If you're using nightly features anyway, maybe your problem can be modeled with a const generic function or trait (with a boolean constant)?

Upvotes: 0

Related Questions