Skip to content

Commit 3533c3d

Browse files
authored
Improve documentation for set_if_neq (#8254)
# Objective Reword the documentation for `set_if_neq` and add a doctest.
1 parent d9113cc commit 3533c3d

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

crates/bevy_ecs/src/change_detection.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,38 @@ pub trait DetectChangesMut: DetectChanges {
119119
/// you are trying to synchronize representations using change detection and need to avoid infinite recursion.
120120
fn bypass_change_detection(&mut self) -> &mut Self::Inner;
121121

122-
/// Sets `self` to `value`, if and only if `*self != *value`
123-
///
124-
/// `T` is the type stored within the smart pointer (e.g. [`Mut`] or [`ResMut`]).
122+
/// Overwrites this smart pointer with the given value, if and only if `*self != value`
125123
///
126124
/// This is useful to ensure change detection is only triggered when the underlying value
127-
/// changes, instead of every time [`DerefMut`] is used.
125+
/// changes, instead of every time it is mutably accessed.
126+
///
127+
/// # Examples
128+
///
129+
/// ```
130+
/// # use bevy_ecs::{prelude::*, schedule::common_conditions::resource_changed};
131+
/// #[derive(Resource, PartialEq, Eq)]
132+
/// pub struct Score(u32);
133+
///
134+
/// fn reset_score(mut score: ResMut<Score>) {
135+
/// // Set the score to zero, unless it is already zero.
136+
/// score.set_if_neq(Score(0));
137+
/// }
138+
/// # let mut world = World::new();
139+
/// # world.insert_resource(Score(1));
140+
/// # let mut score_changed = IntoSystem::into_system(resource_changed::<Score>());
141+
/// # score_changed.initialize(&mut world);
142+
/// # score_changed.run((), &mut world);
143+
/// #
144+
/// # let mut schedule = Schedule::new();
145+
/// # schedule.add_systems(reset_score);
146+
/// #
147+
/// # // first time `reset_score` runs, the score is changed.
148+
/// # schedule.run(&mut world);
149+
/// # assert!(score_changed.run((), &mut world));
150+
/// # // second time `reset_score` runs, the score is not changed.
151+
/// # schedule.run(&mut world);
152+
/// # assert!(!score_changed.run((), &mut world));
153+
/// ```
128154
#[inline]
129155
fn set_if_neq(&mut self, value: Self::Inner)
130156
where

0 commit comments

Comments
 (0)