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

Add new and map methods to Ref #8797

Merged
merged 5 commits into from
Jun 10, 2023
Merged

Conversation

nicopap
Copy link
Contributor

@nicopap nicopap commented Jun 9, 2023

Objective

Ref is a useful way of accessing change detection data.

However, unlike Mut, it doesn't expose a constructor or even a way to go from Ref<A> to Ref<B>.

Such methods could be useful, for example, to 3rd party crates that want to expose change detection information in a clean way.

My use case is to map a Ref<T> into a Ref<dyn Reflect>, and keep change detection info to avoid running expansive routines.

Solution

We add the new and map methods. Since similar methods exist on Mut where they are much more footgunny to use, I judged that it was acceptable to create such methods.

Workaround

Currently, it's not possible to create/project Refs. One can define their own Ref and implement ChangeDetection on it. One would then use ChangeTrackers to populate the custom Ref with tick data.


Changelog

  • Added the Ref::map and Ref::new methods for more ergonomic Refs

Copy link
Member

@MrGVSV MrGVSV left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few nitpicks, but looks good overall!

crates/bevy_ecs/src/change_detection.rs Outdated Show resolved Hide resolved
crates/bevy_ecs/src/change_detection.rs Outdated Show resolved Hide resolved
crates/bevy_ecs/src/change_detection.rs Outdated Show resolved Hide resolved
nicopap and others added 2 commits June 9, 2023 17:33
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Jun 9, 2023
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great docs, well-motivated. Code is correct.

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jun 9, 2023
value: f(self.value),
ticks: self.ticks,
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind adding an extra empty line to separate these new functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jun 10, 2023
Merged via the queue into bevyengine:main with commit 50bc785 Jun 10, 2023
@nicopap nicopap deleted the ref_map branch August 30, 2023 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants