I would like to analyze the value of the incoming items and adjust/group outgoing items accordingly. For that, I need to ensure that my extension can only be called on the iterator wrapping items of a particular type.
My naive approach is to pin the item type in my extension trait:
trait Tags: Sized + Iterator<Item = i32> {
fn tags(self) -> TagsIterator<Self> {
TagsIterator(self)
}
}
impl<I: Sized + Iterator<Item = i32>> Tags for I {}
struct TagGroup;
struct TagsIterator<I: Sized + Iterator<Item = i32>>(I);
impl<I: Sized + Iterator<Item = i32>> Iterator for TagsIterator<I> {
type Item = TagGroup;
fn next(&mut self) -> Option<Self::Item> {
unimplemented!()
}
}
Unfortunately it doesn't work:
let test = vec![0_i32, 1_i32, 2_i32];
test.iter().tags();
error[E0599]: the method `tags` exists for struct `std::slice::Iter<'_, i32>`, but its trait bounds were not satisfied
--> src/main.rs:20:17
|
20 | test.iter().tags();
| ^^^^ method cannot be called on `std::slice::Iter<'_, i32>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`<std::slice::Iter<'_, i32> as Iterator>::Item = i32`
which is required by `std::slice::Iter<'_, i32>: Tags`
`<&std::slice::Iter<'_, i32> as Iterator>::Item = i32`
which is required by `&std::slice::Iter<'_, i32>: Tags`
`&std::slice::Iter<'_, i32>: Iterator`
which is required by `&std::slice::Iter<'_, i32>: Tags`
`<&mut std::slice::Iter<'_, i32> as Iterator>::Item = i32`
which is required by `&mut std::slice::Iter<'_, i32>: Tags`
What am I doing wrong? What is the correct way to restrict an extension trait's incoming items type?