Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add Mut::reborrow #7114

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@ macro_rules! impl_methods {
self.value
}

/// Returns a `Mut<>` with a smaller lifetime.
/// This is useful if you have `&mut Mut<T>`, but you need a `Mut<T>`.
JoJoJet marked this conversation as resolved.
Show resolved Hide resolved
///
/// Note that calling [`DetectChanges::set_last_changed`] on the returned value
/// will not affect the original.
pub fn reborrow(&mut self) -> Mut<'_, $target> {
Mut {
value: self.value,
ticks: Ticks {
added: self.ticks.added,
changed: self.ticks.changed,
last_change_tick: self.ticks.last_change_tick,
change_tick: self.ticks.change_tick,
}
}
}

/// Maps to an inner value by applying a function to the contained reference, without flagging a change.
///
/// You should never modify the argument passed to the closure -- if you want to modify the data
Expand Down Expand Up @@ -430,6 +447,24 @@ impl<'a> MutUntyped<'a> {
self.value
}

/// Returns a [`MutUntyped`] with a smaller lifetime.
/// This is useful if you have `&mut MutUntyped`, but you need a `MutUntyped`.
///
/// Note that calling [`DetectChanges::set_last_changed`] on the returned value
/// will not affect the original.
#[inline]
pub fn reborrow(&mut self) -> MutUntyped {
MutUntyped {
value: self.value.reborrow(),
ticks: Ticks {
added: self.ticks.added,
changed: self.ticks.changed,
last_change_tick: self.ticks.last_change_tick,
change_tick: self.ticks.change_tick,
},
}
}

/// Returns a pointer to the value without taking ownership of this smart pointer, marking it as changed.
///
/// In order to avoid marking the value as changed, you need to call [`bypass_change_detection`](DetectChanges::bypass_change_detection).
Expand Down