use rustc_index::vec::{Idx, IndexVec}; pub fn iter( first: Option, links: &Ls, ) -> impl Iterator + '_ where Ls: Links, { VecLinkedListIterator { links, current: first } } pub struct VecLinkedListIterator where Ls: Links, { links: Ls, current: Option, } impl Iterator for VecLinkedListIterator where Ls: Links, { type Item = Ls::LinkIndex; fn next(&mut self) -> Option { if let Some(c) = self.current { self.current = ::next(&self.links, c); Some(c) } else { None } } } pub trait Links { type LinkIndex: Copy; fn next(links: &Self, index: Self::LinkIndex) -> Option; } impl Links for &Ls where Ls: Links, { type LinkIndex = Ls::LinkIndex; fn next(links: &Self, index: Ls::LinkIndex) -> Option { ::next(links, index) } } pub trait LinkElem { type LinkIndex: Copy; fn next(elem: &Self) -> Option; } impl Links for IndexVec where E: LinkElem, L: Idx, { type LinkIndex = L; fn next(links: &Self, index: L) -> Option { ::next(&links[index]) } }