Here the code:
#[derive(Debug, Eq, PartialEq)]
struct Id(u128);
pub struct IdIter(Box<dyn Iterator<Item= Id>>);
impl IdIter {
pub fn new<I: Iterator<Item= Id>>(tmpl: I) -> Self {
Self(Box::new(tmpl))
}
}
impl Iterator for IdIter {
type Item = Id;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
pub trait Link {
fn iter_id(&self) -> IdIter;
}
pub fn links(it: impl Iterator<Item= impl Link>) -> impl Iterator<Item= Id> {
it.into_iter().flat_map(|l| l.iter_id())
}
#[cfg(test)]
mod test {
struct X(u128);
impl super::Link for X {
fn iter_id(&self) -> super::IdIter {
super::IdIter(Box::new(Some(super::Id(self.0)).into_iter()))
}
}
#[test]
fn test_links() {
let mut v = super::links([X(1234), X(4321)].into_iter());
assert_eq!(v.next(), Some(super::Id(1234u128)));
assert_eq!(v.next(), Some(super::Id(4321u128)));
assert_eq!(v.next(), None);
}
}
The compiler suggests the use of 'static lifetime to fix the problem but it's a little excessive.
What i want is offer a trait (here Link) which an iterator of that trait through the links function gives an iterator of Id instances. The trait is used as a marker trait too therefore i can't use another library.
How can i fix the compiler errors ?