Skip to content

change return type of World::resource_ref to Ref #15263

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
merged 5 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,19 @@ impl<'w, T: Resource> From<ResMut<'w, T>> for Res<'w, T> {
}
}

impl<'w, T: Resource> From<Res<'w, T>> for Ref<'w, T> {
/// Convert a `Res` into a `Ref`. This allows keeping the change-detection feature of `Ref`
/// while losing the specificity of `Res` for resources.
fn from(res: Res<'w, T>) -> Self {
Self {
value: res.value,
ticks: res.ticks,
#[cfg(feature = "track_change_detection")]
changed_by: res.changed_by,
}
}
}

impl<'w, 'a, T: Resource> IntoIterator for &'a Res<'w, T>
where
&'a T: IntoIterator,
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::{
removal_detection::RemovedComponentEvents,
schedule::{Schedule, ScheduleLabel, Schedules},
storage::{ResourceData, Storages},
system::{Commands, Res, Resource},
system::{Commands, Resource},
world::{command_queue::RawCommandQueue, error::TryRunScheduleError},
};
use bevy_ptr::{OwningPtr, Ptr};
Expand Down Expand Up @@ -1612,7 +1612,7 @@ impl World {
/// use [`get_resource_or_insert_with`](World::get_resource_or_insert_with).
#[inline]
#[track_caller]
pub fn resource_ref<R: Resource>(&self) -> Res<R> {
pub fn resource_ref<R: Resource>(&self) -> Ref<R> {
match self.get_resource_ref() {
Some(x) => x,
None => panic!(
Expand Down Expand Up @@ -1660,7 +1660,7 @@ impl World {

/// Gets a reference including change detection to the resource of the given type if it exists.
#[inline]
pub fn get_resource_ref<R: Resource>(&self) -> Option<Res<R>> {
pub fn get_resource_ref<R: Resource>(&self) -> Option<Ref<R>> {
// SAFETY:
// - `as_unsafe_world_cell_readonly` gives permission to access everything immutably
// - `&self` ensures nothing in world is borrowed mutably
Expand Down Expand Up @@ -2400,7 +2400,7 @@ impl World {
}

/// Runs both [`clear_entities`](Self::clear_entities) and [`clear_resources`](Self::clear_resources),
/// invalidating all [`Entity`] and resource fetches such as [`Res`], [`ResMut`](crate::system::ResMut)
/// invalidating all [`Entity`] and resource fetches such as [`Res`](crate::system::Res), [`ResMut`](crate::system::ResMut)
pub fn clear_all(&mut self) {
self.clear_entities();
self.clear_resources();
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/world/unsafe_world_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
query::{DebugCheckedUnwrap, ReadOnlyQueryData},
removal_detection::RemovedComponentEvents,
storage::{ComponentSparseSet, Storages, Table},
system::{Res, Resource},
system::Resource,
world::RawCommandQueue,
};
use bevy_ptr::Ptr;
Expand Down Expand Up @@ -353,7 +353,7 @@ impl<'w> UnsafeWorldCell<'w> {
/// - the [`UnsafeWorldCell`] has permission to access the resource
/// - no mutable reference to the resource exists at the same time
#[inline]
pub unsafe fn get_resource_ref<R: Resource>(self) -> Option<Res<'w, R>> {
pub unsafe fn get_resource_ref<R: Resource>(self) -> Option<Ref<'w, R>> {
let component_id = self.components().get_resource_id(TypeId::of::<R>())?;

// SAFETY: caller ensures `self` has permission to access the resource
Expand All @@ -371,7 +371,7 @@ impl<'w> UnsafeWorldCell<'w> {
#[cfg(feature = "track_change_detection")]
let caller = unsafe { _caller.deref() };

Some(Res {
Some(Ref {
value,
ticks,
#[cfg(feature = "track_change_detection")]
Expand Down