//! A double-ended iterator over entity references. //! //! When `core::iter::Step` is stabilized, `Keys` could be implemented as a wrapper around //! `core::ops::Range`, but for now, we implement it manually. use crate::EntityRef; use core::marker::PhantomData; /// Iterate over all keys in order. pub struct Keys { pos: usize, rev_pos: usize, unused: PhantomData, } impl Keys { /// Create a `Keys` iterator that visits `len` entities starting from 0. pub fn with_len(len: usize) -> Self { Self { pos: 0, rev_pos: len, unused: PhantomData, } } } impl Iterator for Keys { type Item = K; fn next(&mut self) -> Option { if self.pos < self.rev_pos { let k = K::new(self.pos); self.pos += 1; Some(k) } else { None } } fn size_hint(&self) -> (usize, Option) { let size = self.rev_pos - self.pos; (size, Some(size)) } } impl DoubleEndedIterator for Keys { fn next_back(&mut self) -> Option { if self.rev_pos > self.pos { let k = K::new(self.rev_pos - 1); self.rev_pos -= 1; Some(k) } else { None } } } impl ExactSizeIterator for Keys {}