Skip to content

Commit

Permalink
Switch DenseVecStorage::data_id to Vec<MaybeUninit<Index>>
Browse files Browse the repository at this point in the history
This vector contains uninitialized data, so it should use MaybeUninit.
  • Loading branch information
willglynn committed Sep 27, 2019
1 parent 2010379 commit d79e603
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/storage/storages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ unsafe impl<T> DistinctStorage for HashMapStorage<T> {}
pub struct DenseVecStorage<T> {
data: Vec<T>,
entity_id: Vec<Index>,
data_id: Vec<Index>,
data_id: Vec<MaybeUninit<Index>>,
}

impl<T> SliceAccess<T> for DenseVecStorage<T> {
Expand Down Expand Up @@ -141,12 +141,12 @@ impl<T> UnprotectedStorage<T> for DenseVecStorage<T> {
}

unsafe fn get(&self, id: Index) -> &T {
let did = *self.data_id.get_unchecked(id as usize);
let did = self.data_id.get_unchecked(id as usize).assume_init();
self.data.get_unchecked(did as usize)
}

unsafe fn get_mut(&mut self, id: Index) -> &mut T {
let did = *self.data_id.get_unchecked(id as usize);
let did = self.data_id.get_unchecked(id as usize).assume_init();
self.data.get_unchecked_mut(did as usize)
}

Expand All @@ -157,15 +157,15 @@ impl<T> UnprotectedStorage<T> for DenseVecStorage<T> {
self.data_id.reserve(delta);
self.data_id.set_len(id + 1);
}
*self.data_id.get_unchecked_mut(id) = self.data.len() as Index;
self.data_id.get_unchecked_mut(id).as_mut_ptr().write(self.data.len() as Index);
self.entity_id.push(id as Index);
self.data.push(v);
}

unsafe fn remove(&mut self, id: Index) -> T {
let did = *self.data_id.get_unchecked(id as usize);
let did = self.data_id.get_unchecked(id as usize).assume_init();
let last = *self.entity_id.last().unwrap();
*self.data_id.get_unchecked_mut(last as usize) = did;
self.data_id.get_unchecked_mut(last as usize).as_mut_ptr().write(did);
self.entity_id.swap_remove(did as usize);
self.data.swap_remove(did as usize)
}
Expand Down

0 comments on commit d79e603

Please sign in to comment.