When I tried to implement a Trait for Iterators with different Items i got an error.
Is there a way to get something like this to work?
It would be fine if the solution requires nightly feature flags, since I am using some others anyway.
I already tried the specialization feature flag, but that didn't help.
trait Test {
fn test(&self);
}
impl<T: Iterator<Item = i32>> Test for T {
fn test(&self) {
println!("{}", std::any::type_name::<Self>())
}
}
impl<T: Iterator<Item = char>> Test for T {
fn test(&self) {
println!("{}", std::any::type_name::<Self>())
}
}
fn main() {
vec![1,2,3,4].into_iter().test();
vec!['A', 'B', 'C'].into_iter().test();
}
failed to compile with error:
error[E0119]: conflicting implementations of trait `Test`
--> src/main.rs:11:1
|
6 | impl<T: Iterator<Item = i32>> Test for T {
| ---------------------------------------- first implementation here
...
11 | impl<T: Iterator<Item = char>> Test for T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation
I think you can work around this by introducing some (possibly sealed, if you want) helper trait that i32 and char implement, and then writing a single impl of Test for T where T::Item: HelperTrait.
This solution seems really nice, but it isn't really applicable to my actual issue which is more like this:
I want to do two different implementations depending if T is a trait object or not.
To do that I wanted to use the ptr_metadatafeature flag and the Ponintee trait to differentiate between them.
So i would like to do one implementation for Pointee<Metadata = DynMetadata<Dyn>> and one for Pointee<Metadata = ()>.
However I realized that I can just use two traits, although that is not ideal for usability.
Also is there any reason why this shouldn't work (and would adding this need a RFC)?
Ok i guess you are right.
I'm still not 100% happy with having to use this trait and forward all the calls, but I think its better than what i have currently so I'll mark it as a solution.
However I still don't understand why rust doesn't allow the direct way.
Here is a declined postponed RFC proposing that the coherence check allow such impls; you can read the discussion there for some background on why the feature was not accepted, links to further reading, etc.