//! A double-ended iterator over entity references and entities. use crate::EntityRef; use alloc::vec; use core::iter::Enumerate; use core::marker::PhantomData; use core::slice; /// Iterate over all keys in order. pub struct Iter<'a, K: EntityRef, V> where V: 'a, { enumerate: Enumerate>, unused: PhantomData, } impl<'a, K: EntityRef, V> Iter<'a, K, V> { /// Create an `Iter` iterator that visits the `PrimaryMap` keys and values /// of `iter`. pub fn new(iter: slice::Iter<'a, V>) -> Self { Self { enumerate: iter.enumerate(), unused: PhantomData, } } } impl<'a, K: EntityRef, V> Iterator for Iter<'a, K, V> { type Item = (K, &'a V); fn next(&mut self) -> Option { self.enumerate.next().map(|(i, v)| (K::new(i), v)) } fn size_hint(&self) -> (usize, Option) { self.enumerate.size_hint() } } impl<'a, K: EntityRef, V> DoubleEndedIterator for Iter<'a, K, V> { fn next_back(&mut self) -> Option { self.enumerate.next_back().map(|(i, v)| (K::new(i), v)) } } impl<'a, K: EntityRef, V> ExactSizeIterator for Iter<'a, K, V> {} /// Iterate over all keys in order. pub struct IterMut<'a, K: EntityRef, V> where V: 'a, { enumerate: Enumerate>, unused: PhantomData, } impl<'a, K: EntityRef, V> IterMut<'a, K, V> { /// Create an `IterMut` iterator that visits the `PrimaryMap` keys and values /// of `iter`. pub fn new(iter: slice::IterMut<'a, V>) -> Self { Self { enumerate: iter.enumerate(), unused: PhantomData, } } } impl<'a, K: EntityRef, V> Iterator for IterMut<'a, K, V> { type Item = (K, &'a mut V); fn next(&mut self) -> Option { self.enumerate.next().map(|(i, v)| (K::new(i), v)) } fn size_hint(&self) -> (usize, Option) { self.enumerate.size_hint() } } impl<'a, K: EntityRef, V> DoubleEndedIterator for IterMut<'a, K, V> { fn next_back(&mut self) -> Option { self.enumerate.next_back().map(|(i, v)| (K::new(i), v)) } } impl<'a, K: EntityRef, V> ExactSizeIterator for IterMut<'a, K, V> {} /// Iterate over all keys in order. pub struct IntoIter { enumerate: Enumerate>, unused: PhantomData, } impl IntoIter { /// Create an `IntoIter` iterator that visits the `PrimaryMap` keys and values /// of `iter`. pub fn new(iter: vec::IntoIter) -> Self { Self { enumerate: iter.enumerate(), unused: PhantomData, } } } impl Iterator for IntoIter { type Item = (K, V); fn next(&mut self) -> Option { self.enumerate.next().map(|(i, v)| (K::new(i), v)) } fn size_hint(&self) -> (usize, Option) { self.enumerate.size_hint() } } impl DoubleEndedIterator for IntoIter { fn next_back(&mut self) -> Option { self.enumerate.next_back().map(|(i, v)| (K::new(i), v)) } } impl ExactSizeIterator for IntoIter {}