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

# Objective

Closes #11825

## Solution

Change return type of `get_resource_ref` and `resource_ref` from `Res`
to `Ref` and implement `From Res<T> for Ref<T>`.
This commit is contained in:
poopy 2024-09-21 21:11:13 +02:00 committed by GitHub
parent 417e6ccaf1
commit 66a474a9d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 7 deletions

View File

@ -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> impl<'w, 'a, T: Resource> IntoIterator for &'a Res<'w, T>
where where
&'a T: IntoIterator, &'a T: IntoIterator,

View File

@ -40,7 +40,7 @@ use crate::{
removal_detection::RemovedComponentEvents, removal_detection::RemovedComponentEvents,
schedule::{Schedule, ScheduleLabel, Schedules}, schedule::{Schedule, ScheduleLabel, Schedules},
storage::{ResourceData, Storages}, storage::{ResourceData, Storages},
system::{Commands, Res, Resource}, system::{Commands, Resource},
world::{command_queue::RawCommandQueue, error::TryRunScheduleError}, world::{command_queue::RawCommandQueue, error::TryRunScheduleError},
}; };
use bevy_ptr::{OwningPtr, Ptr}; use bevy_ptr::{OwningPtr, Ptr};
@ -1612,7 +1612,7 @@ impl World {
/// use [`get_resource_or_insert_with`](World::get_resource_or_insert_with). /// use [`get_resource_or_insert_with`](World::get_resource_or_insert_with).
#[inline] #[inline]
#[track_caller] #[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() { match self.get_resource_ref() {
Some(x) => x, Some(x) => x,
None => panic!( None => panic!(
@ -1660,7 +1660,7 @@ impl World {
/// Gets a reference including change detection to the resource of the given type if it exists. /// Gets a reference including change detection to the resource of the given type if it exists.
#[inline] #[inline]
pub fn get_resource_ref<R: Resource>(&self) -> Option<Res<R>> { pub fn get_resource_ref<R: Resource>(&self) -> Option<Ref<R>> {
// SAFETY: // SAFETY:
// - `as_unsafe_world_cell_readonly` gives permission to access everything immutably // - `as_unsafe_world_cell_readonly` gives permission to access everything immutably
// - `&self` ensures nothing in world is borrowed mutably // - `&self` ensures nothing in world is borrowed mutably
@ -2400,7 +2400,7 @@ impl World {
} }
/// Runs both [`clear_entities`](Self::clear_entities) and [`clear_resources`](Self::clear_resources), /// 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) { pub fn clear_all(&mut self) {
self.clear_entities(); self.clear_entities();
self.clear_resources(); self.clear_resources();

View File

@ -14,7 +14,7 @@ use crate::{
query::{DebugCheckedUnwrap, ReadOnlyQueryData}, query::{DebugCheckedUnwrap, ReadOnlyQueryData},
removal_detection::RemovedComponentEvents, removal_detection::RemovedComponentEvents,
storage::{ComponentSparseSet, Storages, Table}, storage::{ComponentSparseSet, Storages, Table},
system::{Res, Resource}, system::Resource,
world::RawCommandQueue, world::RawCommandQueue,
}; };
use bevy_ptr::Ptr; use bevy_ptr::Ptr;
@ -353,7 +353,7 @@ impl<'w> UnsafeWorldCell<'w> {
/// - the [`UnsafeWorldCell`] has permission to access the resource /// - the [`UnsafeWorldCell`] has permission to access the resource
/// - no mutable reference to the resource exists at the same time /// - no mutable reference to the resource exists at the same time
#[inline] #[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>())?; let component_id = self.components().get_resource_id(TypeId::of::<R>())?;
// SAFETY: caller ensures `self` has permission to access the resource // SAFETY: caller ensures `self` has permission to access the resource
@ -371,7 +371,7 @@ impl<'w> UnsafeWorldCell<'w> {
#[cfg(feature = "track_change_detection")] #[cfg(feature = "track_change_detection")]
let caller = unsafe { _caller.deref() }; let caller = unsafe { _caller.deref() };
Some(Res { Some(Ref {
value, value,
ticks, ticks,
#[cfg(feature = "track_change_detection")] #[cfg(feature = "track_change_detection")]