In the following code, the trait called LimitCollection has a function that returns a type that implements Iterator trait.
struct Limit {}
impl Limit {
fn is_violated(&self) -> bool {
todo!()
}
}
trait LimitCollection {
fn get_violated_limits<'a, I>(&'a self) -> I
where
I: Iterator<Item = &'a Limit>;
}
Now I'd like to make something that implements the trait but the compiler complains that there is mismatch between the type that I return and the generic return type: "expected type parameter I
, found struct Filter
"
struct ConcreteLimitCollection {
limits: Vec<Limit>,
}
impl LimitCollection for ConcreteLimitCollection {
fn get_violated_limits<'a, I>(&'a self) -> I
where
I: Iterator<Item = &'a Limit>,
{
self.limits.iter().filter(Limit::is_violated)
}
}
Is there any way to make this construct work, preferably without resorting to dynamic allocation?