Async Entity Component System based on the ideas of specs (https://github.com/amethyst/specs)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 rivejä
983 B

  1. use std::mem::MaybeUninit;
  2. use crate::entity::Index;
  3. use super::{DistinctStorage, Storage};
  4. pub struct VecStorage<T>(Vec<MaybeUninit<T>>);
  5. impl<T> Storage<T> for VecStorage<T> {
  6. fn get(&self, index: Index) -> &T {
  7. unsafe { &*self.0.get_unchecked(index as usize).as_ptr() }
  8. }
  9. fn get_mut(&mut self, index: Index) -> &mut T {
  10. unsafe { &mut *self.0.get_unchecked_mut(index as usize).as_mut_ptr() }
  11. }
  12. fn insert(&mut self, index: Index, value: T) {
  13. let index = index as usize;
  14. if self.0.len() <= index {
  15. let delta = index + 1 - self.0.len();
  16. self.0.reserve(delta);
  17. unsafe {
  18. self.0.set_len(index + 1);
  19. }
  20. }
  21. unsafe {
  22. *self.0.get_unchecked_mut(index) = MaybeUninit::new(value);
  23. }
  24. }
  25. }
  26. impl<T> DistinctStorage for VecStorage<T> {}
  27. impl<T> Default for VecStorage<T> {
  28. fn default() -> Self {
  29. Self(Vec::new())
  30. }
  31. }