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] - Use a default implementation for set_if_neq #7660

Closed
Closed
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
39 changes: 10 additions & 29 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,17 @@ pub trait DetectChangesMut: DetectChanges {
///
/// This is useful to ensure change detection is only triggered when the underlying value
/// changes, instead of every time [`DerefMut`] is used.
fn set_if_neq<Target>(&mut self, value: Target)
#[inline]
fn set_if_neq(&mut self, value: Self::Inner)
where
Self: Deref<Target = Target> + DerefMut<Target = Target>,
Target: PartialEq;
Self::Inner: Sized + PartialEq,
{
let old = self.bypass_change_detection();
if *old != value {
*old = value;
self.set_changed();
}
}
}

macro_rules! change_detection_impl {
Expand Down Expand Up @@ -195,19 +202,6 @@ macro_rules! change_detection_mut_impl {
fn bypass_change_detection(&mut self) -> &mut Self::Inner {
self.value
}

#[inline]
fn set_if_neq<Target>(&mut self, value: Target)
where
Self: Deref<Target = Target> + DerefMut<Target = Target>,
Target: PartialEq,
{
// This dereference is immutable, so does not trigger change detection
if *<Self as Deref>::deref(self) != value {
// `DerefMut` usage triggers change detection
*<Self as DerefMut>::deref_mut(self) = value;
}
}
}

impl<$($generics),* : ?Sized $(+ $traits)?> DerefMut for $name<$($generics),*> {
Expand Down Expand Up @@ -682,19 +676,6 @@ impl<'a> DetectChangesMut for MutUntyped<'a> {
fn bypass_change_detection(&mut self) -> &mut Self::Inner {
&mut self.value
}

#[inline]
fn set_if_neq<Target>(&mut self, value: Target)
where
Self: Deref<Target = Target> + DerefMut<Target = Target>,
Target: PartialEq,
{
// This dereference is immutable, so does not trigger change detection
if *<Self as Deref>::deref(self) != value {
// `DerefMut` usage triggers change detection
*<Self as DerefMut>::deref_mut(self) = value;
}
}
}

impl std::fmt::Debug for MutUntyped<'_> {
Expand Down