dynamecs/storages/
versioned_vec_storage.rs

1use crate::join::IntoJoinable;
2use crate::storages::vec_storage::VecStorageJoinable;
3use crate::storages::Version;
4use crate::storages::{VecStorage, VersionedVecStorage};
5use crate::{Entity, GetComponentForEntity, GetComponentForEntityMut, InsertComponentForEntity};
6use std::ops::Deref;
7
8impl<Component> Default for VersionedVecStorage<Component> {
9    fn default() -> Self {
10        Self {
11            storage: Default::default(),
12            versions: Default::default(),
13            storage_version: Default::default(),
14        }
15    }
16}
17
18impl<Component> Deref for VersionedVecStorage<Component> {
19    type Target = VecStorage<Component>;
20
21    fn deref(&self) -> &Self::Target {
22        &self.storage
23    }
24}
25
26impl<Component> VersionedVecStorage<Component> {
27    /// Inserts a component associated with the given entity, and returns the index in the
28    /// storage.
29    ///
30    /// If a component already exists, it is replaced, and the version associated with the entity
31    /// is advanced.
32    pub fn insert(&mut self, entity: Entity, component: Component) -> usize {
33        self.storage_version.advance();
34        let idx = self.storage.insert(entity, component);
35        // idx can be one-past the current length, but not greater
36        if let Some(rev) = self.versions.get_mut(idx) {
37            rev.advance();
38        } else {
39            assert_eq!(idx, self.versions.len());
40            self.versions.push(Version::new());
41        }
42        idx
43    }
44
45    /// Returns a mutable reference to the component associated with the given entity.
46    ///
47    /// If the component exists, the storage version and the version associated with the
48    /// component are advanced.
49    pub fn get_component_mut(&mut self, id: Entity) -> Option<&mut Component> {
50        self.storage.get_index(id).map(|idx| {
51            self.storage_version.advance();
52            self.versions[idx].advance();
53            &mut self.storage.components_mut()[idx]
54        })
55    }
56
57    /// Returns a mutable slice to the components.
58    ///
59    /// Advances the storage version and *all* component versions.
60    pub fn components_mut(&mut self) -> &mut [Component] {
61        self.storage_version.advance();
62        for version in &mut self.versions {
63            version.advance();
64        }
65        self.storage.components_mut()
66    }
67
68    pub fn get_component_version(&self, id: Entity) -> Option<Version<Component>> {
69        self.storage
70            .get_index(id)
71            .map(|idx| self.versions[idx].clone())
72    }
73
74    pub fn storage_version(&self) -> Version<Self> {
75        self.storage_version
76    }
77
78    pub fn versions(&self) -> &[Version<Component>] {
79        &self.versions
80    }
81}
82
83impl<'a, Component> IntoJoinable<'a> for &'a VersionedVecStorage<Component> {
84    type Joinable = VecStorageJoinable<'a, Component>;
85
86    fn into_joinable(self) -> Self::Joinable {
87        self.storage.into_joinable()
88    }
89}
90
91impl<C> GetComponentForEntity<C> for VersionedVecStorage<C> {
92    fn get_component_for_entity(&self, id: Entity) -> Option<&C> {
93        self.get_component(id)
94    }
95}
96
97impl<C> GetComponentForEntityMut<C> for VersionedVecStorage<C> {
98    fn get_component_for_entity_mut(&mut self, id: Entity) -> Option<&mut C> {
99        self.get_component_mut(id)
100    }
101}
102
103impl<C> InsertComponentForEntity<C> for VersionedVecStorage<C> {
104    fn insert_component_for_entity(&mut self, entity: Entity, component: C) {
105        self.insert(entity, component);
106    }
107}