async_ecs/storage/
hash_map_storage.rs

1use hashbrown::HashMap;
2use hibitset::BitSetLike;
3
4use crate::entity::Index;
5
6use super::{DistinctStorage, Storage};
7
8/// `HashMap`-based storage. Best suited for rare components.
9///
10/// This uses the [hashbrown::HashMap] internally.
11pub struct HashMapStorage<T>(HashMap<Index, T>);
12
13impl<T> Default for HashMapStorage<T> {
14    fn default() -> Self {
15        Self(Default::default())
16    }
17}
18
19impl<T> Storage<T> for HashMapStorage<T> {
20    unsafe fn get(&self, id: Index) -> &T {
21        &self.0[&id]
22    }
23
24    unsafe fn get_mut(&mut self, id: Index) -> &mut T {
25        self.0.get_mut(&id).unwrap()
26    }
27
28    unsafe fn insert(&mut self, id: Index, v: T) {
29        self.0.insert(id, v);
30    }
31
32    unsafe fn remove(&mut self, index: Index) -> T {
33        self.0.remove(&index).unwrap()
34    }
35
36    unsafe fn clean<B>(&mut self, _has: B)
37    where
38        B: BitSetLike,
39    {
40        // No Op
41    }
42}
43
44impl<T> DistinctStorage for HashMapStorage<T> {}