From 66a474a9d9c8cc6de2221812fc78ff7648fb0d5b Mon Sep 17 00:00:00 2001 From: poopy Date: Sat, 21 Sep 2024 21:11:13 +0200 Subject: [PATCH 1/2] 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 for Ref`. --- crates/bevy_ecs/src/change_detection.rs | 13 +++++++++++++ crates/bevy_ecs/src/world/mod.rs | 8 ++++---- crates/bevy_ecs/src/world/unsafe_world_cell.rs | 6 +++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index b91bb9ea160a4..247825c285704 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -587,6 +587,19 @@ impl<'w, T: Resource> From> for Res<'w, T> { } } +impl<'w, T: Resource> From> 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, diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 2c55062751be1..6202785f8e332 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -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}; @@ -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(&self) -> Res { + pub fn resource_ref(&self) -> Ref { match self.get_resource_ref() { Some(x) => x, None => panic!( @@ -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(&self) -> Option> { + pub fn get_resource_ref(&self) -> Option> { // SAFETY: // - `as_unsafe_world_cell_readonly` gives permission to access everything immutably // - `&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), - /// 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(); diff --git a/crates/bevy_ecs/src/world/unsafe_world_cell.rs b/crates/bevy_ecs/src/world/unsafe_world_cell.rs index f1af0c11c2eec..7aac53742d458 100644 --- a/crates/bevy_ecs/src/world/unsafe_world_cell.rs +++ b/crates/bevy_ecs/src/world/unsafe_world_cell.rs @@ -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; @@ -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(self) -> Option> { + pub unsafe fn get_resource_ref(self) -> Option> { let component_id = self.components().get_resource_id(TypeId::of::())?; // SAFETY: caller ensures `self` has permission to access the resource @@ -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")] From 02a9ed4b0b0c231a71951543c98550a2de723d0e Mon Sep 17 00:00:00 2001 From: Benjamin Brienen Date: Sat, 21 Sep 2024 22:52:46 +0200 Subject: [PATCH 2/2] move ShortName to bevy_reflect (#15340) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective - Goal is to minimize bevy_utils #11478 ## Solution - Move the file short_name wholesale into bevy_reflect ## Testing - Unit tests - CI ## Migration Guide - References to `bevy_utils::ShortName` should instead now be `bevy_reflect::ShortName`. --------- Co-authored-by: François Mockers --- crates/bevy_asset/src/handle.rs | 3 +-- crates/bevy_ecs/src/schedule/schedule.rs | 16 ++++++++++++---- .../src/valid_parent_check_plugin.rs | 4 +++- crates/bevy_reflect/Cargo.toml | 3 ++- crates/bevy_reflect/src/lib.rs | 4 +++- .../src/short_name.rs} | 2 +- crates/bevy_utils/src/lib.rs | 2 -- 7 files changed, 22 insertions(+), 12 deletions(-) rename crates/{bevy_utils/src/short_names.rs => bevy_reflect/src/short_name.rs} (99%) diff --git a/crates/bevy_asset/src/handle.rs b/crates/bevy_asset/src/handle.rs index 5f78dc76c6d70..25bfee6cf7ca0 100644 --- a/crates/bevy_asset/src/handle.rs +++ b/crates/bevy_asset/src/handle.rs @@ -3,8 +3,7 @@ use crate::{ UntypedAssetId, }; use bevy_ecs::prelude::*; -use bevy_reflect::{std_traits::ReflectDefault, Reflect, TypePath}; -use bevy_utils::ShortName; +use bevy_reflect::{std_traits::ReflectDefault, Reflect, ShortName, TypePath}; use crossbeam_channel::{Receiver, Sender}; use std::{ any::TypeId, diff --git a/crates/bevy_ecs/src/schedule/schedule.rs b/crates/bevy_ecs/src/schedule/schedule.rs index 5b7756ab1369e..1185949388347 100644 --- a/crates/bevy_ecs/src/schedule/schedule.rs +++ b/crates/bevy_ecs/src/schedule/schedule.rs @@ -246,7 +246,7 @@ pub enum Chain { /// fn system_one() { println!("System 1 works!") } /// fn system_two() { println!("System 2 works!") } /// fn system_three() { println!("System 3 works!") } -/// +/// /// fn main() { /// let mut world = World::new(); /// let mut schedule = Schedule::default(); @@ -1582,7 +1582,7 @@ impl ScheduleGraph { #[inline] fn get_node_name_inner(&self, id: &NodeId, report_sets: bool) -> String { - let mut name = match id { + let name = match id { NodeId::System(_) => { let name = self.systems[id.index()].get().unwrap().name().to_string(); if report_sets { @@ -1607,9 +1607,15 @@ impl ScheduleGraph { } } }; - if self.settings.use_shortnames { - name = bevy_utils::ShortName(&name).to_string(); + #[cfg(feature = "bevy_reflect")] + { + if self.settings.use_shortnames { + bevy_reflect::ShortName(&name).to_string() + } else { + name + } } + #[cfg(not(feature = "bevy_reflect"))] name } @@ -2012,6 +2018,7 @@ pub struct ScheduleBuildSettings { /// If set to true, node names will be shortened instead of the fully qualified type path. /// /// Defaults to `true`. + #[cfg(feature = "bevy_reflect")] pub use_shortnames: bool, /// If set to true, report all system sets the conflicting systems are part of. /// @@ -2033,6 +2040,7 @@ impl ScheduleBuildSettings { ambiguity_detection: LogLevel::Ignore, hierarchy_detection: LogLevel::Warn, auto_insert_apply_deferred: true, + #[cfg(feature = "bevy_reflect")] use_shortnames: true, report_sets: true, } diff --git a/crates/bevy_hierarchy/src/valid_parent_check_plugin.rs b/crates/bevy_hierarchy/src/valid_parent_check_plugin.rs index b190885723be9..ef3aebd83d60d 100644 --- a/crates/bevy_hierarchy/src/valid_parent_check_plugin.rs +++ b/crates/bevy_hierarchy/src/valid_parent_check_plugin.rs @@ -4,7 +4,9 @@ use std::marker::PhantomData; use crate::Parent; use bevy_ecs::prelude::*; #[cfg(feature = "bevy_app")] -use bevy_utils::{HashSet, ShortName}; +use bevy_reflect::ShortName; +#[cfg(feature = "bevy_app")] +use bevy_utils::HashSet; /// When enabled, runs [`check_hierarchy_component_has_valid_parent`]. /// diff --git a/crates/bevy_reflect/Cargo.toml b/crates/bevy_reflect/Cargo.toml index c86a92e852603..4a52d9fd3cc0c 100644 --- a/crates/bevy_reflect/Cargo.toml +++ b/crates/bevy_reflect/Cargo.toml @@ -10,7 +10,7 @@ keywords = ["bevy"] rust-version = "1.76.0" [features] -default = ["smallvec", "debug"] +default = ["smallvec", "debug", "alloc"] # When enabled, provides Bevy-related reflection implementations bevy = ["smallvec", "smol_str"] glam = ["dep:glam"] @@ -25,6 +25,7 @@ debug_stack = [] documentation = ["bevy_reflect_derive/documentation"] # Enables function reflection functions = ["bevy_reflect_derive/functions"] +alloc = [] [dependencies] # bevy diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index 7af6020005ecd..c4326cff6f49c 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -549,6 +549,7 @@ mod reflect; mod reflectable; mod remote; mod set; +mod short_name; mod struct_trait; mod tuple; mod tuple_struct; @@ -615,6 +616,7 @@ pub use type_registry::*; pub use bevy_reflect_derive::*; pub use erased_serde; +pub use short_name::ShortName; extern crate alloc; @@ -2366,7 +2368,7 @@ bevy_reflect::tests::Test { fn short_type_path() -> &'static str { static CELL: GenericTypePathCell = GenericTypePathCell::new(); - CELL.get_or_insert::(|| bevy_utils::ShortName::of::().to_string()) + CELL.get_or_insert::(|| ShortName::of::().to_string()) } fn type_ident() -> Option<&'static str> { diff --git a/crates/bevy_utils/src/short_names.rs b/crates/bevy_reflect/src/short_name.rs similarity index 99% rename from crates/bevy_utils/src/short_names.rs rename to crates/bevy_reflect/src/short_name.rs index d14548b6815f2..24f01a8ca1946 100644 --- a/crates/bevy_utils/src/short_names.rs +++ b/crates/bevy_reflect/src/short_name.rs @@ -14,7 +14,7 @@ /// # Examples /// /// ```rust -/// # use bevy_utils::ShortName; +/// # use bevy_reflect::ShortName; /// # /// # mod foo { /// # pub mod bar { diff --git a/crates/bevy_utils/src/lib.rs b/crates/bevy_utils/src/lib.rs index eaa197c0384ea..53442f77222fa 100644 --- a/crates/bevy_utils/src/lib.rs +++ b/crates/bevy_utils/src/lib.rs @@ -25,8 +25,6 @@ pub mod prelude { } pub mod futures; -mod short_names; -pub use short_names::ShortName; pub mod synccell; pub mod syncunsafecell;