shard_ecs/entity_registry/
iterator.rs

1use super::entry::EntityEntry;
2use super::Entity;
3use core::iter::FusedIterator;
4
5pub struct EntityIter<'a> {
6    entities: &'a [EntityEntry],
7    current: usize,
8}
9
10impl<'a> EntityIter<'a> {
11    pub(super) fn new(entities: &'a [EntityEntry]) -> Self {
12        Self {
13            entities,
14            current: 0,
15        }
16    }
17}
18
19impl<'a> Iterator for EntityIter<'a> {
20    type Item = Entity;
21
22    fn next(&mut self) -> Option<Self::Item> {
23        while self.current < self.entities.len() {
24            self.current += 1;
25            let entry = &self.entities[self.current - 1];
26            if entry.is_valid() {
27                return unsafe {
28                    Some(Entity::new_unchecked(
29                        (self.current - 1) as u32,
30                        entry.version(),
31                    ))
32                };
33            }
34        }
35        None
36    }
37}
38
39impl<'a> FusedIterator for EntityIter<'a> {}