I require for a trait to have a function that returns an iterator over some value-type. A struct implementing this trait may simply store a (read-only) collection of values we want to iterate over.
I attempted a contrived example below:
use std::iter::Copied;
use std::collections::{hash_set, HashSet};
trait MyTrait<I: IntoIterator<Item = usize>> {
fn iter(&self) -> I;
}
struct MyStruct{
set: HashSet<usize>,
}
type Iter<'a> = Copied<hash_set::Iter<'a, usize>>;
impl MyTrait<Iter<'_>> for MyStruct {
fn iter(&self) -> Iter<'_> {
self.set.iter().copied()
}
}
However, the above code yields the following compiler errors:
error: `impl` item signature doesn't match `trait` item signature
--> src/lib.rs:15:5
|
5 | fn iter(&self) -> I;
| -------------------- expected `fn(&MyStruct) -> Copied<std::collections::hash_set::Iter<'_, usize>>`
...
15 | fn iter(&self) -> Iter<'_> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&MyStruct) -> Copied<std::collections::hash_set::Iter<'_, usize>>`
|
= note: expected `fn(&MyStruct) -> Copied<std::collections::hash_set::Iter<'_, _>>`
found `fn(&MyStruct) -> Copied<std::collections::hash_set::Iter<'_, _>>`
help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
--> src/lib.rs:5:23
|
5 | fn iter(&self) -> I;
| ^ consider borrowing this type parameter in the trait
The "expected" and "found" appear identical, so maybe this is an issue with anonymous lifetimes? How can I get this to work?