1use crate::entity;
4use crate::iter;
5use crate::sparse;
6
7pub struct ComponentVec<T> {
40 entities: sparse::Set,
41 components: Vec<T>,
42}
43
44impl<T> ComponentVec<T> {
45 #[must_use]
49 #[inline]
50 pub fn new() -> Self {
51 Self {
52 components: Vec::new(),
53 entities: sparse::Set::new(),
54 }
55 }
56
57 #[must_use]
59 #[inline]
60 pub fn len(&self) -> usize {
61 self.components.len()
62 }
63
64 #[must_use]
66 #[inline]
67 pub fn is_empty(&self) -> bool {
68 self.components.is_empty()
69 }
70
71 #[must_use]
73 #[inline]
74 pub fn contains(&self, entity: entity::Entity) -> bool {
75 self.entities.contains(entity)
76 }
77
78 #[must_use]
81 #[inline]
82 pub fn get(&self, entity: entity::Entity) -> Option<&T> {
83 let index = self.entities.index_of(entity)?;
84 self.components.get(index)
85 }
86
87 #[must_use]
90 #[inline]
91 pub fn get_mut(&mut self, entity: entity::Entity) -> Option<&mut T> {
92 let index = self.entities.index_of(entity)?;
93 self.components.get_mut(index)
94 }
95
96 #[inline]
101 pub fn insert(&mut self, entity: entity::Entity, component: T) -> Option<T> {
102 if let Some(value) = self.get_mut(entity) {
103 Some(std::mem::replace(value, component))
104 } else {
105 self.entities.insert(entity);
106 self.components.push(component);
107 None
108 }
109 }
110
111 #[inline]
113 pub fn remove(&mut self, entity: entity::Entity) -> Option<T> {
114 if let Some(index) = self.entities.index_of(entity) {
115 self.entities.remove(entity);
116 Some(self.components.swap_remove(index))
117 } else {
118 None
119 }
120 }
121
122 #[inline]
124 pub fn reserve(&mut self, additional: usize) {
125 self.entities.reserve(additional);
126 self.components.reserve(additional);
127 }
128
129 #[inline]
131 pub fn iter(&self) -> Iter<'_, T> {
132 self.entities.iter().zip(self.components.iter())
133 }
134
135 #[inline]
137 pub fn iter_mut(&mut self) -> IterMut<'_, T> {
138 self.entities.iter().zip(self.components.iter_mut())
139 }
140}
141
142impl<T> Default for ComponentVec<T> {
143 #[inline]
144 fn default() -> ComponentVec<T> {
145 ComponentVec::new()
146 }
147}
148
149use std::iter::Zip;
150use std::slice;
151
152pub type Iter<'a, T> = Zip<iter::Iter<'a, entity::Entity>, slice::Iter<'a, T>>;
154
155pub type IterMut<'a, T> = Zip<iter::Iter<'a, entity::Entity>, slice::IterMut<'a, T>>;
157
158impl<'a, T> IntoIterator for &'a ComponentVec<T> {
159 type IntoIter = Iter<'a, T>;
160 type Item = (entity::Entity, &'a T);
161
162 #[inline]
163 fn into_iter(self) -> Self::IntoIter {
164 self.iter()
165 }
166}
167
168impl<'a, T> IntoIterator for &'a mut ComponentVec<T> {
169 type IntoIter = IterMut<'a, T>;
170 type Item = (entity::Entity, &'a mut T);
171
172 #[inline]
173 fn into_iter(self) -> Self::IntoIter {
174 self.iter_mut()
175 }
176}
177
178mod sealed {
179 use super::ComponentVec;
180
181 pub trait Sealed {}
182
183 impl<T> Sealed for &ComponentVec<T> {}
184 impl<T> Sealed for &mut ComponentVec<T> {}
185}
186
187use sealed::Sealed;
188
189pub struct RefOrMut<'a, T> {
198 pub(crate) entities: &'a sparse::Set,
199 components: T,
200}
201
202pub trait IntoRefOrMut<'a>: Sealed {
204 type Components;
206
207 fn into_ref_or_mut(self) -> RefOrMut<'a, Self::Components>;
209}
210
211impl<'a, T> IntoRefOrMut<'a> for &'a ComponentVec<T> {
212 type Components = &'a [T];
213
214 #[inline]
215 fn into_ref_or_mut(self) -> RefOrMut<'a, Self::Components> {
216 RefOrMut {
217 entities: &self.entities,
218 components: &self.components,
219 }
220 }
221}
222
223impl<'a, T> IntoRefOrMut<'a> for &'a mut ComponentVec<T> {
224 type Components = &'a mut [T];
225
226 #[inline]
227 fn into_ref_or_mut(self) -> RefOrMut<'a, Self::Components> {
228 RefOrMut {
229 entities: &self.entities,
230 components: &mut self.components,
231 }
232 }
233}
234
235impl<'l, 'a, T> iter::Lifetime<'l> for RefOrMut<'a, &'a [T]> {
236 type Item = &'l T;
237}
238
239impl<'l, 'a, T> iter::Lifetime<'l> for RefOrMut<'a, &'a mut [T]> {
240 type Item = &'l mut T;
241}
242
243pub trait GetRefOrMut: for<'l> iter::Lifetime<'l> {
245 fn get_ref_or_mut(
248 &mut self,
249 entity: entity::Entity,
250 ) -> Option<<Self as iter::Lifetime<'_>>::Item>;
251}
252
253impl<'a, T> GetRefOrMut for RefOrMut<'a, &'a [T]> {
254 #[inline]
255 fn get_ref_or_mut(
256 &mut self,
257 entity: entity::Entity,
258 ) -> Option<<Self as iter::Lifetime<'_>>::Item> {
259 let index = self.entities.index_of(entity)?;
260 self.components.get(index)
261 }
262}
263
264impl<'a, T> GetRefOrMut for RefOrMut<'a, &'a mut [T]> {
265 #[inline]
266 fn get_ref_or_mut(
267 &mut self,
268 entity: entity::Entity,
269 ) -> Option<<Self as iter::Lifetime<'_>>::Item> {
270 let index = self.entities.index_of(entity)?;
271 self.components.get_mut(index)
272 }
273}
274
275#[cfg(test)]
276mod tests {
277 use super::*;
278
279 #[test]
280 fn components_i32_put_then_delete() {
281 let mut components = ComponentVec::new();
282
283 let e0 = entity::Entity::from(0);
284 let e1 = entity::Entity::from(1);
285
286 components.insert(e0, 42);
287 assert_eq!(components.components.len(), 1);
288
289 components.insert(e1, 17);
290 assert_eq!(components.components.len(), 2);
291
292 assert_eq!(components.get(e0), Some(&42));
293 assert_eq!(components.get(e1), Some(&17));
294 assert_eq!(components.get_mut(e0), Some(&mut 42));
295 assert_eq!(components.get_mut(e1), Some(&mut 17));
296
297 components.remove(e0);
298 assert_eq!(components.components.len(), 1);
299
300 assert_eq!(components.get(e0), None);
301 assert_eq!(components.get(e1), Some(&17));
302 assert_eq!(components.get_mut(e0), None);
303 assert_eq!(components.get_mut(e1), Some(&mut 17));
304
305 components.remove(e1);
306 assert_eq!(components.components.len(), 0);
307
308 assert_eq!(components.get(e0), None);
309 assert_eq!(components.get(e1), None);
310 assert_eq!(components.get_mut(e0), None);
311 assert_eq!(components.get_mut(e1), None);
312 }
313
314 #[test]
315 fn reserve() {
316 let mut vec = ComponentVec::<i32>::new();
317
318 assert_eq!(vec.components.capacity(), 0);
319
320 let additional = 100;
321 vec.reserve(additional);
322
323 assert!(vec.components.capacity() >= vec.components.len() + additional);
324 }
325
326 #[test]
327 fn contains() {
328 let mut components = ComponentVec::<i32>::new();
329
330 let e0 = entity::Entity::from(0);
331 let e1 = entity::Entity::from(1);
332 let e2 = entity::Entity::from(2);
333
334 components.insert(e0, 42);
335 components.insert(e2, 17);
336
337 assert_eq!(components.contains(e0), true);
338 assert_eq!(components.contains(e1), false);
339 assert_eq!(components.contains(e2), true);
340 }
341}