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

Return a boolean from set_if_neq #9801

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Changes from all 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
14 changes: 9 additions & 5 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ pub trait DetectChangesMut: DetectChanges {
/// you are trying to synchronize representations using change detection and need to avoid infinite recursion.
fn bypass_change_detection(&mut self) -> &mut Self::Inner;

/// Overwrites this smart pointer with the given value, if and only if `*self != value`
/// Overwrites this smart pointer with the given value, if and only if `*self != value`.
/// Returns `true` if the value was overwritten, and returns `false` if it was not.
///
/// This is useful to ensure change detection is only triggered when the underlying value
/// changes, instead of every time it is mutably accessed.
///
/// If you need to handle the previous value, use [`replace_if_neq`](DetectChangesMut::replace_if_neq).
/// If you need the previous value, use [`replace_if_neq`](DetectChangesMut::replace_if_neq).
///
/// # Examples
///
Expand Down Expand Up @@ -160,24 +161,27 @@ pub trait DetectChangesMut: DetectChanges {
/// # assert!(!score_changed.run((), &mut world));
/// ```
#[inline]
fn set_if_neq(&mut self, value: Self::Inner)
fn set_if_neq(&mut self, value: Self::Inner) -> bool
where
Self::Inner: Sized + PartialEq,
{
let old = self.bypass_change_detection();
if *old != value {
*old = value;
self.set_changed();
true
} else {
false
}
}

/// Overwrites this smart pointer with the given value, if and only if `*self != value`
/// Overwrites this smart pointer with the given value, if and only if `*self != value`,
/// returning the previous value if this occurs.
///
/// This is useful to ensure change detection is only triggered when the underlying value
/// changes, instead of every time it is mutably accessed.
///
/// If you don't need to handle the previous value, use [`set_if_neq`](DetectChangesMut::set_if_neq).
/// If you don't need the previous value, use [`set_if_neq`](DetectChangesMut::set_if_neq).
///
/// # Examples
///
Expand Down