ecs: make RefMut a tracking pointer

This commit is contained in:
Carter Anderson 2020-07-18 01:03:47 -07:00
parent 85ec31bb65
commit 23b96a48a6

View file

@ -105,6 +105,7 @@ impl<'a, T: Component> Deref for Ref<'a, T> {
pub struct RefMut<'a, T: Component> {
archetype: &'a Archetype,
target: NonNull<T>,
modified: &'a mut bool,
}
impl<'a, T: Component> RefMut<'a, T> {
@ -118,7 +119,16 @@ impl<'a, T: Component> RefMut<'a, T> {
.add(index as usize),
);
archetype.borrow_mut::<T>();
Ok(Self { archetype, target })
let modified = archetype
.get_modified::<T>()
.unwrap()
.as_ptr()
.add(index as usize);
Ok(Self {
archetype,
target,
modified: &mut *modified,
})
}
}
@ -140,6 +150,7 @@ impl<'a, T: Component> Deref for RefMut<'a, T> {
impl<'a, T: Component> DerefMut for RefMut<'a, T> {
fn deref_mut(&mut self) -> &mut T {
*self.modified = true;
unsafe { self.target.as_mut() }
}
}