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
Changes from 4 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
38 changes: 38 additions & 0 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,44 @@ impl<'a, T: ?Sized> Ref<'a, T> {
pub fn into_inner(self) -> &'a T {
self.value
}
/// Map `Ref` to a different type using `f`.
///
/// This doesn't do anything else than call `f` on the wrapped value.
/// This is equivalent to [`Mut::map_unchanged`].
pub fn map<U>(self, f: impl FnOnce(&T) -> &U) -> Ref<'a, U> {
Ref {
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

/// Create a new `Ref` using provided values.
///
/// This is an advanced feature, `Ref`s are designed to be _created_ by
/// engine-internal code and _consumed_ by end-user code.
///
/// - `value` - The value wrapped by `Ref`.
/// - `added` - A [`Tick`] that stores the tick when the wrapped value was created.
/// - `changed` - A [`Tick`] that stores the last time the wrapped value was changed.
/// - `last_run` - A [`Tick`], occurring before `this_run`, which is used
/// as a reference to determine whether the wrapped value is newly added or changed.
/// - `this_run` - A [`Tick`] corresponding to the current point in time -- "now".
pub fn new(
value: &'a T,
added: &'a Tick,
changed: &'a Tick,
last_run: Tick,
this_run: Tick,
) -> Ref<'a, T> {
Ref {
value,
ticks: Ticks {
added,
changed,
last_run,
this_run,
},
}
}
}

impl<'w, 'a, T> IntoIterator for &'a Ref<'w, T>
Expand Down