Skip to content

Commit 66a474a

Browse files
authored
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>`.
1 parent 417e6cc commit 66a474a

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

crates/bevy_ecs/src/change_detection.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,19 @@ impl<'w, T: Resource> From<ResMut<'w, T>> for Res<'w, T> {
587587
}
588588
}
589589

590+
impl<'w, T: Resource> From<Res<'w, T>> for Ref<'w, T> {
591+
/// Convert a `Res` into a `Ref`. This allows keeping the change-detection feature of `Ref`
592+
/// while losing the specificity of `Res` for resources.
593+
fn from(res: Res<'w, T>) -> Self {
594+
Self {
595+
value: res.value,
596+
ticks: res.ticks,
597+
#[cfg(feature = "track_change_detection")]
598+
changed_by: res.changed_by,
599+
}
600+
}
601+
}
602+
590603
impl<'w, 'a, T: Resource> IntoIterator for &'a Res<'w, T>
591604
where
592605
&'a T: IntoIterator,

crates/bevy_ecs/src/world/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::{
4040
removal_detection::RemovedComponentEvents,
4141
schedule::{Schedule, ScheduleLabel, Schedules},
4242
storage::{ResourceData, Storages},
43-
system::{Commands, Res, Resource},
43+
system::{Commands, Resource},
4444
world::{command_queue::RawCommandQueue, error::TryRunScheduleError},
4545
};
4646
use bevy_ptr::{OwningPtr, Ptr};
@@ -1612,7 +1612,7 @@ impl World {
16121612
/// use [`get_resource_or_insert_with`](World::get_resource_or_insert_with).
16131613
#[inline]
16141614
#[track_caller]
1615-
pub fn resource_ref<R: Resource>(&self) -> Res<R> {
1615+
pub fn resource_ref<R: Resource>(&self) -> Ref<R> {
16161616
match self.get_resource_ref() {
16171617
Some(x) => x,
16181618
None => panic!(
@@ -1660,7 +1660,7 @@ impl World {
16601660

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

24022402
/// Runs both [`clear_entities`](Self::clear_entities) and [`clear_resources`](Self::clear_resources),
2403-
/// invalidating all [`Entity`] and resource fetches such as [`Res`], [`ResMut`](crate::system::ResMut)
2403+
/// invalidating all [`Entity`] and resource fetches such as [`Res`](crate::system::Res), [`ResMut`](crate::system::ResMut)
24042404
pub fn clear_all(&mut self) {
24052405
self.clear_entities();
24062406
self.clear_resources();

crates/bevy_ecs/src/world/unsafe_world_cell.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
query::{DebugCheckedUnwrap, ReadOnlyQueryData},
1515
removal_detection::RemovedComponentEvents,
1616
storage::{ComponentSparseSet, Storages, Table},
17-
system::{Res, Resource},
17+
system::Resource,
1818
world::RawCommandQueue,
1919
};
2020
use bevy_ptr::Ptr;
@@ -353,7 +353,7 @@ impl<'w> UnsafeWorldCell<'w> {
353353
/// - the [`UnsafeWorldCell`] has permission to access the resource
354354
/// - no mutable reference to the resource exists at the same time
355355
#[inline]
356-
pub unsafe fn get_resource_ref<R: Resource>(self) -> Option<Res<'w, R>> {
356+
pub unsafe fn get_resource_ref<R: Resource>(self) -> Option<Ref<'w, R>> {
357357
let component_id = self.components().get_resource_id(TypeId::of::<R>())?;
358358

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

374-
Some(Res {
374+
Some(Ref {
375375
value,
376376
ticks,
377377
#[cfg(feature = "track_change_detection")]

0 commit comments

Comments
 (0)