-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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 a method for mapping Mut<T>
-> Mut<U>
#6199
Conversation
I don't quite understand why this is necessary? What are you planning to use it for? |
I think #4413 is related. |
Clever. If we can improve the docs I'm on board. |
Should this method be |
I don't think there's any need for it to be unsafe, but I'm willing to change it if the consensus disagrees with me. |
This is not |
@TheRawMeatball should this close #4413 if it's merged? |
bors r+ |
# Objective When designing an API, you may wish to provide access only to a specific field of a component or resource. The current options for doing this in safe code are * `*Mut::into_inner`, which flags a change no matter what. * `*Mut::bypass_change_detection`, which misses all changes. ## Solution Add the method `map_unchanged`. ### Example ```rust // When run, zeroes the translation of every entity. fn reset_all(mut transforms: Query<&mut Transform>) { for transform in &mut transforms { // We pinky promise not to modify `t` within the closure. let translation = transform.map_unchanged(|t| &mut t.translation); // Only reset the translation if it isn't already zero. translation.set_if_not_equal(Vec2::ZERO); } } ``` --- ## Changelog + Added the method `map_unchanged` to types `Mut<T>`, `ResMut<T>`, and `NonSendMut<T>`.
Pull request successfully merged into main. Build succeeded: |
Mut<T>
-> Mut<U>
Mut<T>
-> Mut<U>
# Objective When designing an API, you may wish to provide access only to a specific field of a component or resource. The current options for doing this in safe code are * `*Mut::into_inner`, which flags a change no matter what. * `*Mut::bypass_change_detection`, which misses all changes. ## Solution Add the method `map_unchanged`. ### Example ```rust // When run, zeroes the translation of every entity. fn reset_all(mut transforms: Query<&mut Transform>) { for transform in &mut transforms { // We pinky promise not to modify `t` within the closure. let translation = transform.map_unchanged(|t| &mut t.translation); // Only reset the translation if it isn't already zero. translation.set_if_not_equal(Vec2::ZERO); } } ``` --- ## Changelog + Added the method `map_unchanged` to types `Mut<T>`, `ResMut<T>`, and `NonSendMut<T>`.
# Objective When designing an API, you may wish to provide access only to a specific field of a component or resource. The current options for doing this in safe code are * `*Mut::into_inner`, which flags a change no matter what. * `*Mut::bypass_change_detection`, which misses all changes. ## Solution Add the method `map_unchanged`. ### Example ```rust // When run, zeroes the translation of every entity. fn reset_all(mut transforms: Query<&mut Transform>) { for transform in &mut transforms { // We pinky promise not to modify `t` within the closure. let translation = transform.map_unchanged(|t| &mut t.translation); // Only reset the translation if it isn't already zero. translation.set_if_not_equal(Vec2::ZERO); } } ``` --- ## Changelog + Added the method `map_unchanged` to types `Mut<T>`, `ResMut<T>`, and `NonSendMut<T>`.
# Objective When designing an API, you may wish to provide access only to a specific field of a component or resource. The current options for doing this in safe code are * `*Mut::into_inner`, which flags a change no matter what. * `*Mut::bypass_change_detection`, which misses all changes. ## Solution Add the method `map_unchanged`. ### Example ```rust // When run, zeroes the translation of every entity. fn reset_all(mut transforms: Query<&mut Transform>) { for transform in &mut transforms { // We pinky promise not to modify `t` within the closure. let translation = transform.map_unchanged(|t| &mut t.translation); // Only reset the translation if it isn't already zero. translation.set_if_not_equal(Vec2::ZERO); } } ``` --- ## Changelog + Added the method `map_unchanged` to types `Mut<T>`, `ResMut<T>`, and `NonSendMut<T>`.
# Objective When designing an API, you may wish to provide access only to a specific field of a component or resource. The current options for doing this in safe code are * `*Mut::into_inner`, which flags a change no matter what. * `*Mut::bypass_change_detection`, which misses all changes. ## Solution Add the method `map_unchanged`. ### Example ```rust // When run, zeroes the translation of every entity. fn reset_all(mut transforms: Query<&mut Transform>) { for transform in &mut transforms { // We pinky promise not to modify `t` within the closure. let translation = transform.map_unchanged(|t| &mut t.translation); // Only reset the translation if it isn't already zero. translation.set_if_not_equal(Vec2::ZERO); } } ``` --- ## Changelog + Added the method `map_unchanged` to types `Mut<T>`, `ResMut<T>`, and `NonSendMut<T>`.
Objective
When designing an API, you may wish to provide access only to a specific field of a component or resource. The current options for doing this in safe code are
*Mut::into_inner
, which flags a change no matter what.*Mut::bypass_change_detection
, which misses all changes.Solution
Add the method
map_unchanged
.Example
Changelog
map_unchanged
to typesMut<T>
,ResMut<T>
, andNonSendMut<T>
.