Skip to main content

shipyard/entities/
iterator.rs

1use super::Entities;
2use crate::entity_id::EntityId;
3
4/// Iterator over [`Entities`].
5///
6/// [`Entities`]: crate::entities::Entities
7#[allow(clippy::type_complexity)]
8pub struct EntitiesIter<'a>(
9    core::iter::FilterMap<
10        core::iter::Enumerate<core::slice::Iter<'a, EntityId>>,
11        fn((usize, &EntityId)) -> Option<EntityId>,
12    >,
13);
14
15impl<'a> IntoIterator for &'a Entities {
16    type Item = EntityId;
17    type IntoIter = EntitiesIter<'a>;
18
19    fn into_iter(self) -> Self::IntoIter {
20        EntitiesIter(self.data.iter().enumerate().filter_map(filter_map))
21    }
22}
23
24fn filter_map((i, &entity): (usize, &EntityId)) -> Option<EntityId> {
25    if i == entity.uindex() {
26        Some(entity)
27    } else {
28        None
29    }
30}
31
32impl<'a> Iterator for EntitiesIter<'a> {
33    type Item = EntityId;
34
35    fn next(&mut self) -> Option<Self::Item> {
36        self.0.next()
37    }
38    fn size_hint(&self) -> (usize, Option<usize>) {
39        self.0.size_hint()
40    }
41}