Hi all,
I'm trying to implement an API based on Iterator
s that should look like the following:
pub trait Marker {}
pub trait MarkerIter: Iterator
where
Self::Item: Marker,
{}
impl<I> MarkerIter for I
where
I: Iterator,
I::Item: Marker,
{}
impl Marker for i32 {}
pub fn requires_marker<M: Marker>(m: M) {
println!("Got marker");
}
pub fn consume_marker_iter<MI>(mi: MI)
where
MI: MarkerIter,
{
mi.for_each(requires_marker);
}
For me this should work just fine, the compiler should understand that MatkerIter
s have always Item: Marker
. However, I get the following error:
error[E0277]: the trait bound `<MI as std::iter::Iterator>::Item: Marker` is not satisfied
--> src/lib.rs:20:1
|
3 | / pub trait MarkerIter: Iterator
4 | | where
5 | | Self::Item: Marker,
6 | | {}
| |__- required by `MarkerIter`
...
20 | / pub fn consume_marker_iter<MI>(mi: MI)
21 | | where
22 | | MI: MarkerIter,
23 | | {
24 | | mi.for_each(requires_marker);
25 | | }
| |_^ the trait `Marker` is not implemented for `<MI as std::iter::Iterator>::Item`
|
= help: consider adding a `where <MI as std::iter::Iterator>::Item: Marker` bound
Indeed, adding the where clause solves the problem but then I could also abandon MarkerIter
completly
Has anyone an idea how to get this work?
Here is a playgound link if you want to play around on your own.