Skip to main content

goud_engine/ecs/storage/
impls.rs

1//! [`SparseSet`] implementations of the component storage traits.
2//!
3//! This module provides the concrete implementations of [`ComponentStorage`]
4//! and [`AnyComponentStorage`] for [`SparseSet`].
5
6use crate::ecs::{Component, Entity, SparseSet};
7
8use super::traits::{AnyComponentStorage, ComponentStorage};
9
10// =============================================================================
11// SparseSet — ComponentStorage Implementation
12// =============================================================================
13
14impl<T: Component> ComponentStorage for SparseSet<T> {
15    type Item = T;
16
17    #[inline]
18    fn insert(&mut self, entity: Entity, value: T) -> Option<T> {
19        SparseSet::insert(self, entity, value)
20    }
21
22    #[inline]
23    fn remove(&mut self, entity: Entity) -> Option<T> {
24        SparseSet::remove(self, entity)
25    }
26
27    #[inline]
28    fn get(&self, entity: Entity) -> Option<&T> {
29        SparseSet::get(self, entity)
30    }
31
32    #[inline]
33    fn get_mut(&mut self, entity: Entity) -> Option<&mut T> {
34        SparseSet::get_mut(self, entity)
35    }
36
37    #[inline]
38    fn contains(&self, entity: Entity) -> bool {
39        SparseSet::contains(self, entity)
40    }
41
42    #[inline]
43    fn len(&self) -> usize {
44        SparseSet::len(self)
45    }
46
47    #[inline]
48    fn is_empty(&self) -> bool {
49        SparseSet::is_empty(self)
50    }
51}
52
53// =============================================================================
54// SparseSet — AnyComponentStorage Implementation
55// =============================================================================
56
57impl<T: Component> AnyComponentStorage for SparseSet<T> {
58    #[inline]
59    fn contains_entity(&self, entity: Entity) -> bool {
60        self.contains(entity)
61    }
62
63    #[inline]
64    fn remove_entity(&mut self, entity: Entity) -> bool {
65        self.remove(entity).is_some()
66    }
67
68    #[inline]
69    fn storage_len(&self) -> usize {
70        self.len()
71    }
72
73    #[inline]
74    fn storage_is_empty(&self) -> bool {
75        self.is_empty()
76    }
77
78    #[inline]
79    fn clear(&mut self) {
80        SparseSet::clear(self)
81    }
82
83    #[inline]
84    fn component_type_name(&self) -> &'static str {
85        std::any::type_name::<T>()
86    }
87}