mirror of
https://github.com/bevyengine/bevy
synced 2024-09-20 22:42:08 +00:00
Improve documentation for set_if_neq
(#8254)
# Objective Reword the documentation for `set_if_neq` and add a doctest.
This commit is contained in:
parent
d9113cca6f
commit
3533c3dae9
1 changed files with 30 additions and 4 deletions
|
@ -119,12 +119,38 @@ 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;
|
||||
|
||||
/// Sets `self` to `value`, if and only if `*self != *value`
|
||||
///
|
||||
/// `T` is the type stored within the smart pointer (e.g. [`Mut`] or [`ResMut`]).
|
||||
/// Overwrites this smart pointer with the given value, if and only if `*self != value`
|
||||
///
|
||||
/// This is useful to ensure change detection is only triggered when the underlying value
|
||||
/// changes, instead of every time [`DerefMut`] is used.
|
||||
/// changes, instead of every time it is mutably accessed.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use bevy_ecs::{prelude::*, schedule::common_conditions::resource_changed};
|
||||
/// #[derive(Resource, PartialEq, Eq)]
|
||||
/// pub struct Score(u32);
|
||||
///
|
||||
/// fn reset_score(mut score: ResMut<Score>) {
|
||||
/// // Set the score to zero, unless it is already zero.
|
||||
/// score.set_if_neq(Score(0));
|
||||
/// }
|
||||
/// # let mut world = World::new();
|
||||
/// # world.insert_resource(Score(1));
|
||||
/// # let mut score_changed = IntoSystem::into_system(resource_changed::<Score>());
|
||||
/// # score_changed.initialize(&mut world);
|
||||
/// # score_changed.run((), &mut world);
|
||||
/// #
|
||||
/// # let mut schedule = Schedule::new();
|
||||
/// # schedule.add_systems(reset_score);
|
||||
/// #
|
||||
/// # // first time `reset_score` runs, the score is changed.
|
||||
/// # schedule.run(&mut world);
|
||||
/// # assert!(score_changed.run((), &mut world));
|
||||
/// # // second time `reset_score` runs, the score is not changed.
|
||||
/// # schedule.run(&mut world);
|
||||
/// # assert!(!score_changed.run((), &mut world));
|
||||
/// ```
|
||||
#[inline]
|
||||
fn set_if_neq(&mut self, value: Self::Inner)
|
||||
where
|
||||
|
|
Loading…
Reference in a new issue