1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::{ArchetypeStorage, Component, EntityId};

/// A immutable entry of an entity in an `ArchetypeStorage`.
/// Provides convenient and faster access to entity components.
pub struct Entry<'a> {
    pub(crate) arch: &'a ArchetypeStorage,
    pub(crate) entity: EntityId,
}

impl<'a> Entry<'a> {
    /// Returns underlying entity.
    pub fn entity(&self) -> &EntityId {
        &self.entity
    }

    /// Returns a reference to the component `C` of the specified entity.
    pub fn get<C: Component>(&self) -> Option<&C> {
        let comp = self.arch.component::<C>()?;
        Some(unsafe { comp.get_unchecked(self.entity.id) })
    }
}

/// A mutable entry of an entity in an `ArchetypeStorage`.
/// Provides convenient and faster access to entity components.
pub struct EntryMut<'a> {
    pub(crate) arch: &'a mut ArchetypeStorage,
    pub(crate) entity: EntityId,
}

impl EntryMut<'_> {
    /// Returns underlying entity.
    pub fn entity(&self) -> &EntityId {
        &self.entity
    }

    /// Returns a reference to the component `C` of the specified entity.
    pub fn get<C: Component>(&self) -> Option<&C> {
        let comp = self.arch.component::<C>()?;
        Some(unsafe { comp.get_unchecked(self.entity.id) })
    }

    /// Returns a mutable reference to the component `C` of the specified entity.
    pub fn get_mut<C: Component>(&mut self) -> Option<&mut C> {
        let mut comp = self.arch.component_mut::<C>()?;
        Some(unsafe { comp.get_unchecked_mut(self.entity.id) })
    }
}