In this snippet:
struct Token /* { ... } */;
pub trait Tokenize {
fn tokens(&self) -> Vec<Token>;
}
impl<T: Tokenize> Tokenize for Vec<T> {
fn tokens(&self) -> Vec<Token> { self.iter().tokens() }
}
impl<IT, T> Tokenize for IT
where IT: Iterator<Item=T>, T: Tokenize
{
fn tokens(&self) -> Vec<Token> { self.flat_map(|t| t.tokens()).collect() }
}
The compiler complains that the implementation for Vec<T>
conflicts with the more general Iterator
implementation. I'm guessing this is something affected by Rebalancing coherence. by nikomatsakis · Pull Request #1023 · rust-lang/rfcs · GitHub, because Vec<T>
might implement Iterator
in future (despite this being very unlikely in practice).
So, 3 things:
- Is my diagnosis right?
- Could the compiler error message be more explicit about the problem? Because on the face of it, it doesn't make much sense without knowing about the negative reasoning rules. Perhaps the explanation for E0119 could be expanded to include this case.
- What future extensions would allow something like this to be implemented? Is this something HKT could solve? Would it be possible to add negative implementations to tell the compiler that
Vec<T>
will never implementIterator
?
Thanks, J