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
48
mod registry;
mod slab_storage;
mod vec_storage;

use crate::slab;
use slab::RawSlab;
use slab::RawSlabKey;

use crate::entity;
use entity::EntityHandle;

pub trait ComponentStorageBase {
    fn on_entity_free(&mut self, entity: &EntityHandle);
    fn has_component_for_entity(&self, entity: &EntityHandle);
}

//TODO: Make these take some sort of private index type to prevent someone from
// trying to fetch components directly (these are not checking generation.. it's assumed
// we are calling through the entity code, which does a gen check there
pub trait ComponentStorage<T>: Send + Sync {
    fn new() -> Self;
    fn allocate(&mut self, entity: &EntityHandle, data: T);
    fn free(&mut self, entity: &EntityHandle);
    fn free_if_exists(&mut self, entity: &EntityHandle);
    fn get(&self, entity: &EntityHandle) -> Option<&T>;
    fn get_mut(&mut self, entity: &EntityHandle) -> Option<&mut T>;
}

impl<T> ComponentStorageBase for T
where
    T: ComponentStorage<T>,
{
    fn on_entity_free(&mut self, entity: &EntityHandle) {
        self.free(entity);
    }

    fn has_component_for_entity(&self, entity: &EntityHandle) {
        self.get(entity).is_some();
    }
}

pub trait Component: Sized + Send + Sync + 'static {
    type Storage: ComponentStorage<Self>;
}

pub use registry::ComponentRegistry;
pub use slab_storage::SlabComponentStorage;
pub use vec_storage::VecComponentStorage;