Expose set_changed() on ResMut and Mut (#2208)

This new api stems from this [discord conversation](https://discord.com/channels/691052431525675048/742569353878437978/844057268172357663).

This exposes a public facing `set_changed` method on `ResMut` and `Mut`.

As a side note: `ResMut` and `Mut` have a lot of duplicated code, I have a PR I may put up later that refactors these commonalities into a trait.

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
This commit is contained in:
Nathan Ward 2021-05-18 19:25:58 +00:00
parent 93cc7219bc
commit 9eb1aeee48
2 changed files with 22 additions and 2 deletions

View file

@ -369,6 +369,16 @@ impl<'w, T: Component> ResMut<'w, T> {
self.ticks
.is_changed(self.last_change_tick, self.change_tick)
}
/// Manually flags this resource as having been changed. This normally isn't
/// required because accessing this pointer mutably automatically flags this
/// resource as "changed".
///
/// **Note**: This operation is irreversible.
#[inline]
pub fn set_changed(&mut self) {
self.ticks.set_changed(self.change_tick);
}
}
impl<'w, T: Component> Deref for ResMut<'w, T> {
@ -381,7 +391,7 @@ impl<'w, T: Component> Deref for ResMut<'w, T> {
impl<'w, T: Component> DerefMut for ResMut<'w, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.ticks.set_changed(self.change_tick);
self.set_changed();
self.value
}
}

View file

@ -14,6 +14,16 @@ impl<'a, T> Mut<'a, T> {
self.component_ticks.set_changed(self.change_tick);
self.value
}
/// Manually flags this value as having been changed. This normally isn't
/// required because accessing this pointer mutably automatically flags this
/// value as "changed".
///
/// **Note**: This operation is irreversible.
#[inline]
pub fn set_changed(&mut self) {
self.component_ticks.set_changed(self.change_tick);
}
}
impl<'a, T> Deref for Mut<'a, T> {
@ -28,7 +38,7 @@ impl<'a, T> Deref for Mut<'a, T> {
impl<'a, T> DerefMut for Mut<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
self.component_ticks.set_changed(self.change_tick);
self.set_changed();
self.value
}
}