How can I implement a trait for iterators with different kinds of values?

Viewed 70

I am trying to create a trait that adds a method for all iterators of (K: Ord, V) tuples, and I want it to work whether the tuple is owned or borrowed. I ran into a problem where the compiler says there are conflicting implementations, and I don't understand why. Here is the minimal version of my code:

struct MyStruct<I> {
    iter: I
}

trait MyTrait<K: Ord, I> {
    fn my_func(self) -> MyStruct<I>;
}

impl<K: Ord, V, I: Iterator<Item=(K, V)>> MyTrait<K, I> for I {
    fn my_func(self) -> MyStruct<I> {
        unimplemented!()
    }
}

impl<'a, K: Ord, V, I: Iterator<Item=&'a (K, V)>> MyTrait<K, I> for I {
    fn my_func(self) -> MyStruct<I> {
        unimplemented!()
    }
}

Given that &'a(K, V) has no overlap with (K, V), I thought this should be fine. Why isn't this code valid, and how can it be changed to make it compile?

0 Answers
Related