diff --git a/benches/benches/bevy_reflect/struct.rs b/benches/benches/bevy_reflect/struct.rs index c368cc1168dea..68c11f9d8d053 100644 --- a/benches/benches/bevy_reflect/struct.rs +++ b/benches/benches/bevy_reflect/struct.rs @@ -160,14 +160,14 @@ fn concrete_struct_type_info(criterion: &mut Criterion) { BenchmarkId::new("NonGeneric", field_count), &standard, |bencher, s| { - bencher.iter(|| s.get_represented_type_info()); + bencher.iter(|| s.runtime_type_info()); }, ); group.bench_with_input( BenchmarkId::new("Generic", field_count), &generic, |bencher, s| { - bencher.iter(|| s.get_represented_type_info()); + bencher.iter(|| s.runtime_type_info()); }, ); } diff --git a/crates/bevy_asset/src/reflect.rs b/crates/bevy_asset/src/reflect.rs index 792aaddb6a2da..9a2237aef4398 100644 --- a/crates/bevy_asset/src/reflect.rs +++ b/crates/bevy_asset/src/reflect.rs @@ -360,12 +360,12 @@ impl ReflectSerializerProcessor for HandleSerializeProcessor { // This is a slow path. Users are unlikely to be intentionally serializing types without // reflection, especially in production apps, so we can afford to be slow and give // better diagnostics. - if let Some(type_info) = value_reflect.get_represented_type_info() - && type_info.type_path().starts_with("bevy_asset::Handle") + if let Some(ty) = value_reflect.runtime_type() + && ty.path().starts_with("bevy_asset::Handle") { warn!( - "HandleSerializeProcessor attempted to serialize a handle type \"{}\" without type data. This likely means the asset type was not registered.", - type_info.type_path() + "HandleSerializeProcessor attempted to serialize a handle type \"{:?}\" without type data. This likely means the asset type was not registered.", + ty ); } // Otherwise, fall back to the underlying serializer. Let it handle the error. diff --git a/crates/bevy_ecs/src/reflect/entity_commands.rs b/crates/bevy_ecs/src/reflect/entity_commands.rs index 0fd192110fbe7..d29f4b653e103 100644 --- a/crates/bevy_ecs/src/reflect/entity_commands.rs +++ b/crates/bevy_ecs/src/reflect/entity_commands.rs @@ -361,7 +361,7 @@ fn insert_reflect_with_registry_ref( component: Box, ) { let type_info = component - .get_represented_type_info() + .runtime_type_info() .expect("component should represent a type."); let type_path = type_info.type_path(); let Some(type_registration) = type_registry.get(type_info.type_id()) else { diff --git a/crates/bevy_reflect/derive/src/impls/enums.rs b/crates/bevy_reflect/derive/src/impls/enums.rs index c0fa9362fc884..802cc782375bc 100644 --- a/crates/bevy_reflect/derive/src/impls/enums.rs +++ b/crates/bevy_reflect/derive/src/impls/enums.rs @@ -184,7 +184,17 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream impl #impl_generics #bevy_reflect_path::PartialReflect for #enum_path #ty_generics #where_reflect_clause { #[inline] - fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> { + fn comptime_type(&self) -> #bevy_reflect_path::ty::Type { + #bevy_reflect_path::ty::Type::of::() + } + + #[inline] + fn runtime_type(&self) -> #FQOption<#bevy_reflect_path::ty::Type> { + Some(#bevy_reflect_path::ty::Type::of::()) + } + + #[inline] + fn runtime_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> { #FQOption::Some(::type_info()) } diff --git a/crates/bevy_reflect/derive/src/impls/opaque.rs b/crates/bevy_reflect/derive/src/impls/opaque.rs index 638963095d48e..5230d9c98dfa6 100644 --- a/crates/bevy_reflect/derive/src/impls/opaque.rs +++ b/crates/bevy_reflect/derive/src/impls/opaque.rs @@ -88,7 +88,17 @@ pub(crate) fn impl_opaque(meta: &ReflectMeta) -> proc_macro2::TokenStream { impl #impl_generics #bevy_reflect_path::PartialReflect for #type_path #ty_generics #where_reflect_clause { #[inline] - fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> { + fn comptime_type(&self) -> #bevy_reflect_path::ty::Type { + #bevy_reflect_path::ty::Type::of::() + } + + #[inline] + fn runtime_type(&self) -> #FQOption<#bevy_reflect_path::ty::Type> { + Some(#bevy_reflect_path::ty::Type::of::()) + } + + #[inline] + fn runtime_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> { #FQOption::Some(::type_info()) } diff --git a/crates/bevy_reflect/derive/src/impls/structs.rs b/crates/bevy_reflect/derive/src/impls/structs.rs index c172cd85ce5a6..4368b5217ea43 100644 --- a/crates/bevy_reflect/derive/src/impls/structs.rs +++ b/crates/bevy_reflect/derive/src/impls/structs.rs @@ -132,7 +132,7 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenS fn to_dynamic_struct(&self) -> #bevy_reflect_path::structs::DynamicStruct { let mut dynamic: #bevy_reflect_path::structs::DynamicStruct = #FQDefault::default(); - dynamic.set_represented_type(#bevy_reflect_path::PartialReflect::get_represented_type_info(self)); + dynamic.set_represented_type(#bevy_reflect_path::PartialReflect::runtime_type_info(self)); #(dynamic.insert_boxed(#field_names, #bevy_reflect_path::PartialReflect::to_dynamic(#fields_ref));)* dynamic } @@ -140,7 +140,17 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenS impl #impl_generics #bevy_reflect_path::PartialReflect for #struct_path #ty_generics #where_reflect_clause { #[inline] - fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> { + fn comptime_type(&self) -> #bevy_reflect_path::ty::Type { + #bevy_reflect_path::ty::Type::of::() + } + + #[inline] + fn runtime_type(&self) -> #FQOption<#bevy_reflect_path::ty::Type> { + Some(#bevy_reflect_path::ty::Type::of::()) + } + + #[inline] + fn runtime_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> { #FQOption::Some(::type_info()) } diff --git a/crates/bevy_reflect/derive/src/impls/tuple_structs.rs b/crates/bevy_reflect/derive/src/impls/tuple_structs.rs index c2739792881f9..ad44160523385 100644 --- a/crates/bevy_reflect/derive/src/impls/tuple_structs.rs +++ b/crates/bevy_reflect/derive/src/impls/tuple_structs.rs @@ -92,7 +92,7 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> proc_macro2:: fn to_dynamic_tuple_struct(&self) -> #bevy_reflect_path::tuple_struct::DynamicTupleStruct { let mut dynamic: #bevy_reflect_path::tuple_struct::DynamicTupleStruct = #FQDefault::default(); - dynamic.set_represented_type(#bevy_reflect_path::PartialReflect::get_represented_type_info(self)); + dynamic.set_represented_type(#bevy_reflect_path::PartialReflect::runtime_type_info(self)); #(dynamic.insert_boxed(#bevy_reflect_path::PartialReflect::to_dynamic(#fields_ref));)* dynamic } @@ -100,7 +100,17 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> proc_macro2:: impl #impl_generics #bevy_reflect_path::PartialReflect for #struct_path #ty_generics #where_reflect_clause { #[inline] - fn get_represented_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> { + fn comptime_type(&self) -> #bevy_reflect_path::ty::Type { + #bevy_reflect_path::ty::Type::of::() + } + + #[inline] + fn runtime_type(&self) -> #FQOption<#bevy_reflect_path::ty::Type> { + Some(#bevy_reflect_path::ty::Type::of::()) + } + + #[inline] + fn runtime_type_info(&self) -> #FQOption<&'static #bevy_reflect_path::TypeInfo> { #FQOption::Some(::type_info()) } diff --git a/crates/bevy_reflect/derive/src/impls/typed.rs b/crates/bevy_reflect/derive/src/impls/typed.rs index 791dfcea4fd40..715dfa1747cee 100644 --- a/crates/bevy_reflect/derive/src/impls/typed.rs +++ b/crates/bevy_reflect/derive/src/impls/typed.rs @@ -150,7 +150,7 @@ pub(crate) fn impl_typed( let where_reflect_clause = where_clause_options.extend_where_clause(where_clause); // The `[inline(never)]` is a binary size optimization, although it may - // slightly increase the cost of calling `get_represented_type_info`. + // slightly increase the cost of calling `runtime_type_info`. quote! { impl #impl_generics #bevy_reflect_path::Typed for #type_path #ty_generics #where_reflect_clause { #[inline(never)] diff --git a/crates/bevy_reflect/src/array.rs b/crates/bevy_reflect/src/array.rs index 29d1df8fd88d9..6257c7987f0c6 100644 --- a/crates/bevy_reflect/src/array.rs +++ b/crates/bevy_reflect/src/array.rs @@ -74,14 +74,14 @@ pub trait Array: PartialReflect { /// Creates a new [`DynamicArray`] from this array. fn to_dynamic_array(&self) -> DynamicArray { DynamicArray { - represented_type: self.get_represented_type_info(), + represented_type: self.runtime_type_info(), values: self.iter().map(PartialReflect::to_dynamic).collect(), } } /// Will return `None` if [`TypeInfo`] is not available. fn get_represented_array_info(&self) -> Option<&'static ArrayInfo> { - self.get_represented_type_info()?.as_array().ok() + self.runtime_type_info()?.as_array().ok() } } @@ -200,10 +200,20 @@ impl DynamicArray { impl PartialReflect for DynamicArray { #[inline] - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { self.represented_type } + #[inline] + fn runtime_type(&self) -> Option { + self.represented_type.map(TypeInfo::ty).copied() + } + #[inline] fn into_partial_reflect(self: Box) -> Box { self diff --git a/crates/bevy_reflect/src/enums/dynamic_enum.rs b/crates/bevy_reflect/src/enums/dynamic_enum.rs index 40e4c8963e983..53c50ff19741f 100644 --- a/crates/bevy_reflect/src/enums/dynamic_enum.rs +++ b/crates/bevy_reflect/src/enums/dynamic_enum.rs @@ -12,6 +12,7 @@ use crate::{ }; use alloc::{boxed::Box, string::String}; +use bevy_reflect::Type; use core::fmt::Formatter; use derive_more::derive::From; @@ -174,7 +175,7 @@ impl DynamicEnum { /// /// This is functionally the same as [`DynamicEnum::from`] except it takes a reference. pub fn from_ref(value: &TEnum) -> Self { - let type_info = value.get_represented_type_info(); + let type_info = value.runtime_type_info(); let mut dyn_enum = match value.variant_type() { VariantType::Unit => DynamicEnum::new_with_index( value.variant_index(), @@ -291,10 +292,20 @@ impl Enum for DynamicEnum { impl PartialReflect for DynamicEnum { #[inline] - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { self.represented_type } + #[inline] + fn runtime_type(&self) -> Option { + self.represented_type.map(TypeInfo::ty).copied() + } + #[inline] fn into_partial_reflect(self: Box) -> Box { self diff --git a/crates/bevy_reflect/src/enums/enum_trait.rs b/crates/bevy_reflect/src/enums/enum_trait.rs index 70499d0e8aa46..e4a0e3e4f2fb3 100644 --- a/crates/bevy_reflect/src/enums/enum_trait.rs +++ b/crates/bevy_reflect/src/enums/enum_trait.rs @@ -141,7 +141,7 @@ pub trait Enum: PartialReflect { /// /// [`TypeInfo`]: crate::TypeInfo fn get_represented_enum_info(&self) -> Option<&'static EnumInfo> { - self.get_represented_type_info()?.as_enum().ok() + self.runtime_type_info()?.as_enum().ok() } } diff --git a/crates/bevy_reflect/src/func/dynamic_function.rs b/crates/bevy_reflect/src/func/dynamic_function.rs index ecc91eb28575a..4d97a0a607dba 100644 --- a/crates/bevy_reflect/src/func/dynamic_function.rs +++ b/crates/bevy_reflect/src/func/dynamic_function.rs @@ -12,6 +12,7 @@ use crate::{ }; use alloc::{borrow::Cow, boxed::Box}; use bevy_platform::sync::Arc; +use bevy_reflect::Type; use bevy_reflect_derive::impl_type_path; use core::fmt::{Debug, Formatter}; @@ -364,7 +365,18 @@ impl Function for DynamicFunction<'static> { } impl PartialReflect for DynamicFunction<'static> { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { + #[inline] + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { + None + } + + #[inline] + fn runtime_type(&self) -> Option { None } diff --git a/crates/bevy_reflect/src/func/signature.rs b/crates/bevy_reflect/src/func/signature.rs index cedeaca952492..3846739c189b3 100644 --- a/crates/bevy_reflect/src/func/signature.rs +++ b/crates/bevy_reflect/src/func/signature.rs @@ -76,7 +76,7 @@ impl<'a, 'b> ArgListSignature<'a, 'b> { pub fn iter(&self) -> impl ExactSizeIterator { self.0.iter().map(|arg| { arg.value() - .get_represented_type_info() + .runtime_type_info() .unwrap_or_else(|| { panic!("no `TypeInfo` found for argument: {:?}", arg); }) @@ -101,7 +101,7 @@ impl Hash for ArgListSignature<'_, '_> { fn hash(&self, state: &mut H) { self.0.iter().for_each(|arg| { arg.value() - .get_represented_type_info() + .runtime_type_info() .unwrap_or_else(|| { panic!("no `TypeInfo` found for argument: {:?}", arg); }) @@ -189,7 +189,7 @@ impl From<&ArgList<'_>> for ArgumentSignature { args.iter() .map(|arg| { arg.value() - .get_represented_type_info() + .runtime_type_info() .unwrap_or_else(|| { panic!("no `TypeInfo` found for argument: {:?}", arg); }) diff --git a/crates/bevy_reflect/src/impls/alloc/borrow.rs b/crates/bevy_reflect/src/impls/alloc/borrow.rs index 93e854434b0c8..735a0eca9c995 100644 --- a/crates/bevy_reflect/src/impls/alloc/borrow.rs +++ b/crates/bevy_reflect/src/impls/alloc/borrow.rs @@ -10,6 +10,7 @@ use crate::{ TypeRegistration, TypeRegistry, }, utility::{reflect_hasher, GenericTypeInfoCell, NonGenericTypeInfoCell}, + Type, }; use alloc::borrow::Cow; use alloc::vec::Vec; @@ -22,8 +23,19 @@ use core::hash::{Hash, Hasher}; impl_type_path!(::alloc::borrow::Cow<'a: 'static, T: ToOwned + ?Sized>); impl PartialReflect for Cow<'static, str> { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + #[inline] + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn runtime_type(&self) -> Option { + Some(Type::of::()) } #[inline] @@ -51,6 +63,19 @@ impl PartialReflect for Cow<'static, str> { Some(self) } + fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { + if let Some(value) = value.try_downcast_ref::() { + self.clone_from(value); + } else { + return Err(ApplyError::MismatchedTypes { + from_type: value.reflect_type_path().into(), + // If we invoke the reflect_type_path on self directly the borrow checker complains that the lifetime of self must outlive 'static + to_type: Self::type_path().into(), + }); + } + Ok(()) + } + fn reflect_kind(&self) -> ReflectKind { ReflectKind::Opaque } @@ -97,19 +122,6 @@ impl PartialReflect for Cow<'static, str> { fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(self, f) } - - fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { - if let Some(value) = value.try_downcast_ref::() { - self.clone_from(value); - } else { - return Err(ApplyError::MismatchedTypes { - from_type: value.reflect_type_path().into(), - // If we invoke the reflect_type_path on self directly the borrow checker complains that the lifetime of self must outlive 'static - to_type: Self::type_path().into(), - }); - } - Ok(()) - } } impl_full_reflect!(for Cow<'static, str>); @@ -203,8 +215,19 @@ impl List impl PartialReflect for Cow<'static, [T]> { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + #[inline] + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn runtime_type(&self) -> Option { + Some(Type::of::()) } #[inline] @@ -232,6 +255,14 @@ impl Parti Some(self) } + fn apply(&mut self, value: &dyn PartialReflect) { + crate::list::list_apply(self, value); + } + + fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { + crate::list::list_try_apply(self, value) + } + fn reflect_kind(&self) -> ReflectKind { ReflectKind::List } @@ -263,14 +294,6 @@ impl Parti fn reflect_partial_cmp(&self, value: &dyn PartialReflect) -> Option<::core::cmp::Ordering> { crate::list::list_partial_cmp(self, value) } - - fn apply(&mut self, value: &dyn PartialReflect) { - crate::list::list_apply(self, value); - } - - fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { - crate::list::list_try_apply(self, value) - } } impl_full_reflect!( diff --git a/crates/bevy_reflect/src/impls/alloc/collections/btree/map.rs b/crates/bevy_reflect/src/impls/alloc/collections/btree/map.rs index d184df6b61db8..8b5059ae31688 100644 --- a/crates/bevy_reflect/src/impls/alloc/collections/btree/map.rs +++ b/crates/bevy_reflect/src/impls/alloc/collections/btree/map.rs @@ -11,6 +11,7 @@ use crate::{ }; use alloc::vec::Vec; use bevy_platform::prelude::*; +use bevy_reflect::Type; use bevy_reflect_derive::impl_type_path; impl Map for ::alloc::collections::BTreeMap @@ -97,9 +98,21 @@ where K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord, V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration, { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + #[inline] + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn runtime_type(&self) -> Option { + Some(Type::of::()) } + #[inline] fn into_partial_reflect(self: Box) -> Box { self @@ -124,6 +137,14 @@ where fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> { Some(self) } + fn apply(&mut self, value: &dyn PartialReflect) { + map_apply(self, value); + } + + fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { + map_try_apply(self, value) + } + fn reflect_kind(&self) -> ReflectKind { ReflectKind::Map } @@ -158,14 +179,6 @@ where fn reflect_partial_cmp(&self, value: &dyn PartialReflect) -> Option<::core::cmp::Ordering> { map_partial_cmp(self, value) } - - fn apply(&mut self, value: &dyn PartialReflect) { - map_apply(self, value); - } - - fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { - map_try_apply(self, value) - } } impl_full_reflect!( diff --git a/crates/bevy_reflect/src/impls/core/panic.rs b/crates/bevy_reflect/src/impls/core/panic.rs index c12139e2f9292..e0961c08da24f 100644 --- a/crates/bevy_reflect/src/impls/core/panic.rs +++ b/crates/bevy_reflect/src/impls/core/panic.rs @@ -9,6 +9,7 @@ use crate::{ utility::{reflect_hasher, NonGenericTypeInfoCell}, }; use bevy_platform::prelude::*; +use bevy_reflect::{MaybeTyped, Type}; use core::any::Any; use core::hash::{Hash, Hasher}; use core::panic::Location; @@ -24,8 +25,19 @@ impl TypePath for &'static Location<'static> { } impl PartialReflect for &'static Location<'static> { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + #[inline] + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn runtime_type(&self) -> Option { + Some(Type::of::()) } #[inline] @@ -53,6 +65,18 @@ impl PartialReflect for &'static Location<'static> { Some(self) } + fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { + if let Some(value) = value.try_downcast_ref::() { + self.clone_from(value); + Ok(()) + } else { + Err(ApplyError::MismatchedTypes { + from_type: value.reflect_type_path().into(), + to_type: ::reflect_type_path(self).into(), + }) + } + } + fn reflect_kind(&self) -> ReflectKind { ReflectKind::Opaque } @@ -95,18 +119,6 @@ impl PartialReflect for &'static Location<'static> { None } } - - fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { - if let Some(value) = value.try_downcast_ref::() { - self.clone_from(value); - Ok(()) - } else { - Err(ApplyError::MismatchedTypes { - from_type: value.reflect_type_path().into(), - to_type: ::reflect_type_path(self).into(), - }) - } - } } impl Reflect for &'static Location<'static> { diff --git a/crates/bevy_reflect/src/impls/core/primitives.rs b/crates/bevy_reflect/src/impls/core/primitives.rs index 1661608d07b27..7a554aabd6f1f 100644 --- a/crates/bevy_reflect/src/impls/core/primitives.rs +++ b/crates/bevy_reflect/src/impls/core/primitives.rs @@ -10,6 +10,7 @@ use crate::{ TypeRegistration, TypeRegistry, }, utility::{reflect_hasher, GenericTypeInfoCell, GenericTypePathCell, NonGenericTypeInfoCell}, + Type, }; use bevy_platform::prelude::*; use bevy_reflect_derive::{impl_reflect_opaque, impl_type_path}; @@ -318,8 +319,19 @@ impl_reflect_opaque!(f64( impl_type_path!(str); impl PartialReflect for &'static str { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + #[inline] + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn runtime_type(&self) -> Option { + Some(Type::of::()) } #[inline] @@ -347,6 +359,18 @@ impl PartialReflect for &'static str { Some(self) } + fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { + if let Some(value) = value.try_downcast_ref::() { + self.clone_from(value); + } else { + return Err(ApplyError::MismatchedTypes { + from_type: value.reflect_type_path().into(), + to_type: Self::type_path().into(), + }); + } + Ok(()) + } + fn reflect_ref(&self) -> ReflectRef<'_> { ReflectRef::Opaque(self) } @@ -389,18 +413,6 @@ impl PartialReflect for &'static str { fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self, f) } - - fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { - if let Some(value) = value.try_downcast_ref::() { - self.clone_from(value); - } else { - return Err(ApplyError::MismatchedTypes { - from_type: value.reflect_type_path().into(), - to_type: Self::type_path().into(), - }); - } - Ok(()) - } } impl Reflect for &'static str { @@ -489,8 +501,19 @@ impl A impl PartialReflect for [T; N] { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + #[inline] + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn runtime_type(&self) -> Option { + Some(Type::of::()) } #[inline] @@ -518,6 +541,15 @@ impl P Some(self) } + fn apply(&mut self, value: &dyn PartialReflect) { + crate::array::array_apply(self, value); + } + + #[inline] + fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { + crate::array::array_try_apply(self, value) + } + #[inline] fn reflect_kind(&self) -> ReflectKind { ReflectKind::Array @@ -552,15 +584,6 @@ impl P fn reflect_partial_cmp(&self, value: &dyn PartialReflect) -> Option<::core::cmp::Ordering> { crate::array::array_partial_cmp(self, value) } - - fn apply(&mut self, value: &dyn PartialReflect) { - crate::array::array_apply(self, value); - } - - #[inline] - fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { - crate::array::array_try_apply(self, value) - } } impl Reflect for [T; N] { diff --git a/crates/bevy_reflect/src/impls/core/sync.rs b/crates/bevy_reflect/src/impls/core/sync.rs index 03b2f72a97e6f..6e37febc9ae49 100644 --- a/crates/bevy_reflect/src/impls/core/sync.rs +++ b/crates/bevy_reflect/src/impls/core/sync.rs @@ -50,8 +50,18 @@ macro_rules! impl_reflect_for_atomic { impl PartialReflect for $ty { #[inline] - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + fn runtime_type_info(&self) -> Option<&'static $crate::info::TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn comptime_type(&self) -> $crate::ty::Type { + $crate::ty::Type::of::() + } + + #[inline] + fn runtime_type(&self) -> Option<$crate::ty::Type> { + Some($crate::ty::Type::of::()) } #[inline] fn into_partial_reflect(self: Box) -> Box { diff --git a/crates/bevy_reflect/src/impls/indexmap.rs b/crates/bevy_reflect/src/impls/indexmap.rs index fa406f0642ff8..a6e254c0e16f4 100644 --- a/crates/bevy_reflect/src/impls/indexmap.rs +++ b/crates/bevy_reflect/src/impls/indexmap.rs @@ -2,7 +2,7 @@ use crate::{ set::{Set, SetInfo}, utility::GenericTypeInfoCell, FromReflect, Generics, GetTypeRegistration, PartialReflect, Reflect, ReflectCloneError, - ReflectFromPtr, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, TypeParamInfo, TypePath, + ReflectFromPtr, ReflectMut, ReflectOwned, ReflectRef, Type, TypeInfo, TypeParamInfo, TypePath, TypeRegistration, }; use bevy_platform::prelude::{Box, Vec}; @@ -60,7 +60,7 @@ where fn to_dynamic_map(&self) -> DynamicMap { let mut dynamic_map = DynamicMap::default(); - dynamic_map.set_represented_type(PartialReflect::get_represented_type_info(self)); + dynamic_map.set_represented_type(PartialReflect::runtime_type_info(self)); for (k, v) in self { let key = K::from_reflect(k).unwrap_or_else(|| { panic!( @@ -112,8 +112,19 @@ where V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration, S: TypePath + BuildHasher + Default + Send + Sync, { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + #[inline] + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn runtime_type(&self) -> Option { + Some(Type::of::()) } #[inline] @@ -348,8 +359,19 @@ where T: FromReflect + TypePath + GetTypeRegistration + Eq + Hash, S: TypePath + BuildHasher + Default + Send + Sync, { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + #[inline] + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn runtime_type(&self) -> Option { + Some(Type::of::()) } #[inline] diff --git a/crates/bevy_reflect/src/impls/macros/list.rs b/crates/bevy_reflect/src/impls/macros/list.rs index 923f0d324725b..2cc997d67449d 100644 --- a/crates/bevy_reflect/src/impls/macros/list.rs +++ b/crates/bevy_reflect/src/impls/macros/list.rs @@ -62,8 +62,18 @@ macro_rules! impl_reflect_for_veclike { impl $crate::reflect::PartialReflect for $ty { #[inline] - fn get_represented_type_info(&self) -> Option<&'static $crate::info::TypeInfo> { - Some(::type_info()) + fn runtime_type_info(&self) -> Option<&'static $crate::info::TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn comptime_type(&self) -> $crate::ty::Type { + $crate::ty::Type::of::() + } + + #[inline] + fn runtime_type(&self) -> Option<$crate::ty::Type> { + Some($crate::ty::Type::of::()) } fn into_partial_reflect(self: bevy_platform::prelude::Box) -> bevy_platform::prelude::Box { diff --git a/crates/bevy_reflect/src/impls/macros/map.rs b/crates/bevy_reflect/src/impls/macros/map.rs index bab819965d6bd..fd22d5b5658e4 100644 --- a/crates/bevy_reflect/src/impls/macros/map.rs +++ b/crates/bevy_reflect/src/impls/macros/map.rs @@ -44,7 +44,7 @@ macro_rules! impl_reflect_for_hashmap { fn to_dynamic_map(&self) -> $crate::map::DynamicMap { let mut dynamic_map = $crate::map::DynamicMap::default(); - dynamic_map.set_represented_type($crate::reflect::PartialReflect::get_represented_type_info(self)); + dynamic_map.set_represented_type($crate::reflect::PartialReflect::runtime_type_info(self)); for (k, v) in self { let key = K::from_reflect(k).unwrap_or_else(|| { panic!( @@ -96,8 +96,19 @@ macro_rules! impl_reflect_for_hashmap { V: $crate::from_reflect::FromReflect + $crate::info::MaybeTyped + $crate::type_path::TypePath + $crate::type_registry::GetTypeRegistration, S: $crate::type_path::TypePath + core::hash::BuildHasher + Default + Send + Sync, { - fn get_represented_type_info(&self) -> Option<&'static $crate::info::TypeInfo> { - Some(::type_info()) + #[inline] + fn runtime_type_info(&self) -> Option<&'static $crate::info::TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn comptime_type(&self) -> $crate::ty::Type { + $crate::ty::Type::of::() + } + + #[inline] + fn runtime_type(&self) -> Option<$crate::ty::Type> { + Some($crate::ty::Type::of::()) } #[inline] diff --git a/crates/bevy_reflect/src/impls/macros/set.rs b/crates/bevy_reflect/src/impls/macros/set.rs index da84d2f4ca5e5..4995308f7a41b 100644 --- a/crates/bevy_reflect/src/impls/macros/set.rs +++ b/crates/bevy_reflect/src/impls/macros/set.rs @@ -70,8 +70,19 @@ macro_rules! impl_reflect_for_hashset { V: $crate::from_reflect::FromReflect + $crate::type_path::TypePath + $crate::type_registry::GetTypeRegistration + Eq + core::hash::Hash, S: $crate::type_path::TypePath + core::hash::BuildHasher + Default + Send + Sync, { - fn get_represented_type_info(&self) -> Option<&'static $crate::info::TypeInfo> { - Some(::type_info()) + #[inline] + fn runtime_type_info(&self) -> Option<&'static $crate::info::TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn comptime_type(&self) -> $crate::ty::Type { + $crate::ty::Type::of::() + } + + #[inline] + fn runtime_type(&self) -> Option<$crate::ty::Type> { + Some($crate::ty::Type::of::()) } #[inline] diff --git a/crates/bevy_reflect/src/impls/smallvec.rs b/crates/bevy_reflect/src/impls/smallvec.rs index da430a381b3ea..e3256f130a3fc 100644 --- a/crates/bevy_reflect/src/impls/smallvec.rs +++ b/crates/bevy_reflect/src/impls/smallvec.rs @@ -6,7 +6,7 @@ use crate::{ TypePath, TypeRegistration, Typed, }; use alloc::{boxed::Box, vec::Vec}; -use bevy_reflect::ReflectCloneError; +use bevy_reflect::{ReflectCloneError, Type}; use bevy_reflect_derive::impl_type_path; use core::any::Any; use smallvec::{Array as SmallArray, SmallVec}; @@ -83,8 +83,19 @@ impl PartialReflect for SmallVec where T::Item: FromReflect + MaybeTyped + TypePath, { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + #[inline] + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn runtime_type(&self) -> Option { + Some(Type::of::()) } #[inline] diff --git a/crates/bevy_reflect/src/impls/std/path.rs b/crates/bevy_reflect/src/impls/std/path.rs index bbf25021e54e9..289b170d08b23 100644 --- a/crates/bevy_reflect/src/impls/std/path.rs +++ b/crates/bevy_reflect/src/impls/std/path.rs @@ -12,6 +12,7 @@ use crate::{ }; use alloc::borrow::Cow; use bevy_platform::prelude::*; +use bevy_reflect::{MaybeTyped, Type}; use bevy_reflect_derive::{impl_reflect_opaque, impl_type_path}; use core::any::Any; use core::fmt; @@ -30,8 +31,19 @@ impl_reflect_opaque!(::std::path::PathBuf( )); impl PartialReflect for &'static Path { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + #[inline] + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn runtime_type(&self) -> Option { + Some(Type::of::()) } #[inline] @@ -59,6 +71,18 @@ impl PartialReflect for &'static Path { Some(self) } + fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { + if let Some(value) = value.try_downcast_ref::() { + self.clone_from(value); + Ok(()) + } else { + Err(ApplyError::MismatchedTypes { + from_type: value.reflect_type_path().into(), + to_type: ::reflect_type_path(self).into(), + }) + } + } + fn reflect_kind(&self) -> ReflectKind { ReflectKind::Opaque } @@ -101,18 +125,6 @@ impl PartialReflect for &'static Path { None } } - - fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { - if let Some(value) = value.try_downcast_ref::() { - self.clone_from(value); - Ok(()) - } else { - Err(ApplyError::MismatchedTypes { - from_type: value.reflect_type_path().into(), - to_type: ::reflect_type_path(self).into(), - }) - } - } } impl Reflect for &'static Path { @@ -169,8 +181,19 @@ impl FromReflect for &'static Path { } impl PartialReflect for Cow<'static, Path> { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + #[inline] + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn runtime_type(&self) -> Option { + Some(Type::of::()) } #[inline] @@ -198,6 +221,18 @@ impl PartialReflect for Cow<'static, Path> { Some(self) } + fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { + if let Some(value) = value.try_downcast_ref::() { + self.clone_from(value); + Ok(()) + } else { + Err(ApplyError::MismatchedTypes { + from_type: value.reflect_type_path().into(), + to_type: ::reflect_type_path(self).into(), + }) + } + } + fn reflect_kind(&self) -> ReflectKind { ReflectKind::Opaque } @@ -244,18 +279,6 @@ impl PartialReflect for Cow<'static, Path> { fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self, f) } - - fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { - if let Some(value) = value.try_downcast_ref::() { - self.clone_from(value); - Ok(()) - } else { - Err(ApplyError::MismatchedTypes { - from_type: value.reflect_type_path().into(), - to_type: ::reflect_type_path(self).into(), - }) - } - } } impl Reflect for Cow<'static, Path> { diff --git a/crates/bevy_reflect/src/info/type_info.rs b/crates/bevy_reflect/src/info/type_info.rs index a81580eed0eff..6bba460bc30a3 100644 --- a/crates/bevy_reflect/src/info/type_info.rs +++ b/crates/bevy_reflect/src/info/type_info.rs @@ -10,14 +10,14 @@ use core::any::{Any, TypeId}; /// Generally, for any given type, this value can be retrieved in one of four ways: /// /// 1. [`Typed::type_info`] -/// 2. [`DynamicTyped::reflect_type_info`] -/// 3. [`PartialReflect::get_represented_type_info`] +/// 2. [`DynamicTyped::comptime_type_info`] +/// 3. [`PartialReflect::runtime_type_info`] /// 4. [`TypeRegistry::get_type_info`] /// /// Each returns a static reference to [`TypeInfo`], but they all have their own use cases. /// For example, if you know the type at compile time, [`Typed::type_info`] is probably -/// the simplest. If you have a `dyn Reflect` you can use [`DynamicTyped::reflect_type_info`]. -/// If all you have is a `dyn PartialReflect`, you'll probably want [`PartialReflect::get_represented_type_info`]. +/// the simplest. If you have a `dyn Reflect` you can use [`DynamicTyped::comptime_type_info`]. +/// If all you have is a `dyn PartialReflect`, you'll probably want [`PartialReflect::runtime_type_info`]. /// Lastly, if all you have is a [`TypeId`] or [type path], you will need to go through /// [`TypeRegistry::get_type_info`]. /// @@ -26,9 +26,9 @@ use core::any::{Any, TypeId}; /// the static [`TypeInfo`], while the registry simply checks a map. /// /// [`Typed::type_info`]: crate::info::Typed::type_info -/// [`DynamicTyped::reflect_type_info`]: crate::info::DynamicTyped::reflect_type_info +/// [`DynamicTyped::comptime_type_info`]: crate::info::DynamicTyped::comptime_type_info /// [`TypeRegistry::get_type_info`]: crate::TypeRegistry::get_type_info -/// [`PartialReflect::get_represented_type_info`]: crate::PartialReflect::get_represented_type_info +/// [`PartialReflect::runtime_type_info`]: crate::PartialReflect::runtime_type_info /// [type path]: crate::type_path::TypePath::type_path #[derive(Debug, Clone)] pub enum TypeInfo { diff --git a/crates/bevy_reflect/src/info/typed.rs b/crates/bevy_reflect/src/info/typed.rs index 84e25aad3b369..9bd659822f962 100644 --- a/crates/bevy_reflect/src/info/typed.rs +++ b/crates/bevy_reflect/src/info/typed.rs @@ -27,7 +27,7 @@ use crate::{ /// /// ``` /// # use core::any::Any; -/// # use bevy_reflect::{DynamicTypePath, NamedField, PartialReflect, Reflect, ReflectMut, ReflectOwned, ReflectRef, structs::StructInfo, TypeInfo, TypePath, OpaqueInfo, ApplyError}; +/// # use bevy_reflect::{DynamicTypePath, NamedField, PartialReflect, Reflect, ReflectMut, ReflectOwned, ReflectRef, structs::StructInfo, TypeInfo, TypePath, OpaqueInfo, ApplyError, Type}; /// # use bevy_reflect::utility::NonGenericTypeInfoCell; /// use bevy_reflect::Typed; /// @@ -55,7 +55,9 @@ use crate::{ /// # fn short_type_path() -> &'static str { todo!() } /// # } /// # impl PartialReflect for MyStruct { -/// # fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { todo!() } +/// # fn comptime_type(&self) -> Type { todo!() } +/// # fn runtime_type(&self) -> Option { todo!() } +/// # fn runtime_type_info(&self) -> Option<&'static TypeInfo> { todo!() } /// # fn into_partial_reflect(self: Box) -> Box { todo!() } /// # fn as_partial_reflect(&self) -> &dyn PartialReflect { todo!() } /// # fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect { todo!() } @@ -102,12 +104,21 @@ pub trait Typed: Reflect + TypePath { )] pub trait DynamicTyped { /// See [`Typed::type_info`]. - fn reflect_type_info(&self) -> &'static TypeInfo; + #[deprecated( + since = "0.20.0", + note = "Use `DynamicTyped::comptime_type_info` instead" + )] + fn reflect_type_info(&self) -> &'static TypeInfo { + self.comptime_type_info() + } + + /// Returns the compile-time [`TypeInfo`] as given by [`Typed::type_info`]. + fn comptime_type_info(&self) -> &'static TypeInfo; } impl DynamicTyped for T { #[inline] - fn reflect_type_info(&self) -> &'static TypeInfo { + fn comptime_type_info(&self) -> &'static TypeInfo { Self::type_info() } } diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index 9c2076458ea6e..dd11d6b3d4761 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -2631,7 +2631,7 @@ mod tests { // TypeInfo (instance) let value: &dyn Reflect = &123_i32; - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::()); // Struct @@ -2652,7 +2652,7 @@ mod tests { assert_eq!(usize::type_path(), info.field_at(1).unwrap().type_path()); let value: &dyn Reflect = &MyStruct { foo: 123, bar: 321 }; - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::()); // Struct (generic) @@ -2674,7 +2674,7 @@ mod tests { foo: String::from("Hello!"), bar: 321, }; - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::>()); // Struct (dynamic field) @@ -2704,7 +2704,7 @@ mod tests { foo: DynamicStruct::default(), bar: 321, }; - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::()); // Tuple Struct @@ -2730,7 +2730,7 @@ mod tests { assert!(info.field_at(1).unwrap().type_info().unwrap().is::()); let value: &dyn Reflect = &(123_u32, 1.23_f32, String::from("Hello!")); - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::()); // List @@ -2745,7 +2745,7 @@ mod tests { assert_eq!(usize::type_path(), info.item_ty().path()); let value: &dyn Reflect = &vec![123_usize]; - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::()); // List (SmallVec) @@ -2762,7 +2762,7 @@ mod tests { let value: MySmallVec = smallvec::smallvec![String::default(); 2]; let value: &dyn Reflect = &value; - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::()); } @@ -2778,7 +2778,7 @@ mod tests { assert_eq!(3, info.capacity()); let value: &dyn Reflect = &[1usize, 2usize, 3usize]; - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::()); // Cow<'static, str> @@ -2790,7 +2790,7 @@ mod tests { assert_eq!("alloc::borrow::Cow", info.type_path()); let value: &dyn Reflect = &Cow::<'static, str>::Owned("Hello!".to_string()); - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::()); // Cow<'static, [u8]> @@ -2805,7 +2805,7 @@ mod tests { assert_eq!("u8", info.item_ty().path()); let value: &dyn Reflect = &Cow::<'static, [u8]>::Owned(vec![0, 1, 2, 3]); - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::()); // Map @@ -2823,7 +2823,7 @@ mod tests { assert_eq!(f32::type_path(), info.value_ty().path()); let value: &dyn Reflect = &MyMap::default(); - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::()); // Map (IndexMap) @@ -2847,7 +2847,7 @@ mod tests { let value: MyIndexMap = MyIndexMap::with_capacity_and_hasher(10, RandomState::new()); let value: &dyn Reflect = &value; - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::()); } @@ -2860,7 +2860,7 @@ mod tests { assert_eq!(MyValue::type_path(), info.type_path()); let value: &dyn Reflect = &String::from("Hello!"); - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); assert!(info.is::()); } @@ -4045,6 +4045,49 @@ bevy_reflect::tests::Test { assert_eq!(point, Point(external_crate::Vector2([1, 2]))); } + #[test] + fn should_return_remote_type_info() { + mod external_crate { + use alloc::string::String; + + #[derive(Debug, Default)] + pub struct TheirType { + pub value: String, + } + } + + // === Remote Wrapper === // + #[reflect_remote(external_crate::TheirType)] + #[derive(Debug, Default)] + #[reflect(Debug, Default)] + struct MyType { + pub value: String, + } + + let info = MyType::type_info(); + assert!(info.is::()); + + let data: Box = Box::new(MyType(external_crate::TheirType { + value: "Hello".to_string(), + })); + assert_eq!(data.comptime_type(), Type::of::()); + assert_eq!(data.runtime_type(), Some(Type::of::())); + + // === Struct Container === // + #[derive(Reflect, Debug)] + #[reflect(from_reflect = false)] + struct ContainerStruct { + #[reflect(remote = MyType)] + their_type: external_crate::TheirType, + } + + let info = ContainerStruct::type_info(); + assert!(info.is::()); + + let field = info.as_struct().unwrap().field("their_type").unwrap(); + assert!(field.type_info().unwrap().is::()); + } + #[test] fn should_register_fully_qualified_type_data() { mod foo { diff --git a/crates/bevy_reflect/src/list.rs b/crates/bevy_reflect/src/list.rs index c7823fccbfb0e..bca73573229eb 100644 --- a/crates/bevy_reflect/src/list.rs +++ b/crates/bevy_reflect/src/list.rs @@ -109,14 +109,14 @@ pub trait List: PartialReflect { /// Creates a new [`DynamicList`] from this list. fn to_dynamic_list(&self) -> DynamicList { DynamicList { - represented_type: self.get_represented_type_info(), + represented_type: self.runtime_type_info(), values: self.iter().map(PartialReflect::to_dynamic).collect(), } } /// Will return `None` if [`TypeInfo`] is not available. fn get_represented_list_info(&self) -> Option<&'static ListInfo> { - self.get_represented_type_info()?.as_list().ok() + self.runtime_type_info()?.as_list().ok() } } @@ -252,10 +252,20 @@ impl List for DynamicList { impl PartialReflect for DynamicList { #[inline] - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { self.represented_type } + #[inline] + fn runtime_type(&self) -> Option { + self.represented_type.map(TypeInfo::ty).copied() + } + #[inline] fn into_partial_reflect(self: Box) -> Box { self diff --git a/crates/bevy_reflect/src/map.rs b/crates/bevy_reflect/src/map.rs index b9c4d1cea7546..9da65cc4c9317 100644 --- a/crates/bevy_reflect/src/map.rs +++ b/crates/bevy_reflect/src/map.rs @@ -83,7 +83,7 @@ pub trait Map: PartialReflect { /// Creates a new [`DynamicMap`] from this map. fn to_dynamic_map(&self) -> DynamicMap { let mut map = DynamicMap::default(); - map.set_represented_type(self.get_represented_type_info()); + map.set_represented_type(self.runtime_type_info()); for (key, value) in self.iter() { map.insert_boxed(key.to_dynamic(), value.to_dynamic()); } @@ -108,7 +108,7 @@ pub trait Map: PartialReflect { /// Will return `None` if [`TypeInfo`] is not available. fn get_represented_map_info(&self) -> Option<&'static MapInfo> { - self.get_represented_type_info()?.as_map().ok() + self.runtime_type_info()?.as_map().ok() } } @@ -203,7 +203,7 @@ macro_rules! hash_error { type_path ) } else { - match (*$key).get_represented_type_info() { + match (*$key).runtime_type_info() { // Handle dynamic types that do not represent a type (i.e a plain `DynamicStruct`): ::core::option::Option::None => $crate::__macro_exports::alloc_utils::format!("the dynamic type `{}` does not support hashing", type_path), // Handle dynamic types that do represent a type (i.e. a `DynamicStruct` proxying `Foo`): @@ -335,10 +335,20 @@ impl Map for DynamicMap { impl PartialReflect for DynamicMap { #[inline] - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { self.represented_type } + #[inline] + fn runtime_type(&self) -> Option { + self.represented_type.map(TypeInfo::ty).copied() + } + #[inline] fn into_partial_reflect(self: Box) -> Box { self diff --git a/crates/bevy_reflect/src/reflect.rs b/crates/bevy_reflect/src/reflect.rs index d0d4b87cc8f4e..253ed025ffbcb 100644 --- a/crates/bevy_reflect/src/reflect.rs +++ b/crates/bevy_reflect/src/reflect.rs @@ -13,9 +13,9 @@ use core::{ fmt::Debug, }; -use thiserror::Error; - use crate::utility::NonGenericTypeInfoCell; +use bevy_reflect::Type; +use thiserror::Error; /// A enumeration of all error outcomes that might happen when running [`try_apply`](PartialReflect::try_apply). #[derive(Error, Debug)] @@ -103,6 +103,32 @@ where // NB: we don't use `Self: Any` since for downcasting, `Reflect` should be used. Self: 'static, { + /// Returns the [`Type`] of this reflected value as known at compile-time. + /// + /// Unlike [`Self::runtime_type`], this method should behave the same for concrete and dynamic types, + /// with no special-casing for proxies. + /// This means that a [`DynamicStruct`] will always return the [`Type`] for [`DynamicStruct`], + /// regardless of proxy status. + /// + /// [`DynamicStruct`]: crate::structs::DynamicStruct + fn comptime_type(&self) -> Type; + + /// Returns the [`TypeInfo`] of this reflected value as known at runtime. + /// + /// For dynamic types, such as [`DynamicStruct`] or [`DynamicList`], + /// this will return the [`TypeInfo`] of the type that they are proxying, + /// or `None` if they are not proxying any type. + /// + /// This method is great if you have an instance of a type or a `dyn Reflect`, + /// and want to access its [`TypeInfo`]. + /// However, if this method is to be called frequently, + /// consider using [`TypeRegistry::get_type_info`] as it can often be more performant for such use cases. + /// + /// [`DynamicStruct`]: crate::structs::DynamicStruct + /// [`DynamicList`]: crate::list::DynamicList + /// [`TypeRegistry::get_type_info`]: crate::TypeRegistry::get_type_info + fn runtime_type_info(&self) -> Option<&'static TypeInfo>; + /// Returns the [`TypeInfo`] of the type _represented_ by this value. /// /// For most types, this will simply return their own `TypeInfo`. @@ -118,7 +144,26 @@ where /// [`DynamicStruct`]: crate::structs::DynamicStruct /// [`DynamicList`]: crate::list::DynamicList /// [`TypeRegistry::get_type_info`]: crate::TypeRegistry::get_type_info - fn get_represented_type_info(&self) -> Option<&'static TypeInfo>; + #[deprecated( + since = "0.20.0", + note = "Use `PartialReflect::runtime_type_info` instead" + )] + fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { + self.runtime_type_info() + } + + /// Returns the [`Type`] of this reflected value as known at runtime. + /// + /// For dynamic types, such as [`DynamicStruct`] or [`DynamicList`], + /// this will return the [`Type`] of the type that they are proxying, + /// or `None` if they are not proxying any type. + /// + /// In all other cases, this method should return the same [`Type`] + /// as returned by [`Self::comptime_type`]. + /// + /// [`DynamicStruct`]: crate::structs::DynamicStruct + /// [`DynamicList`]: crate::list::DynamicList + fn runtime_type(&self) -> Option; /// Casts this type to a boxed, reflected value. /// @@ -457,7 +502,7 @@ impl dyn PartialReflect { /// Read `is` for more information on underlying values and represented types. #[inline] pub fn represents(&self) -> bool { - self.get_represented_type_info() + self.runtime_type_info() .is_some_and(|t| t.type_path() == T::type_path()) } diff --git a/crates/bevy_reflect/src/serde/de/deserializer.rs b/crates/bevy_reflect/src/serde/de/deserializer.rs index 229a49f270f23..81b8b51bd7fca 100644 --- a/crates/bevy_reflect/src/serde/de/deserializer.rs +++ b/crates/bevy_reflect/src/serde/de/deserializer.rs @@ -86,7 +86,7 @@ use super::ReflectDeserializerProcessor; /// assert_eq!(value, MyStruct { value: 123 }); /// /// // We can also do this dynamically with `ReflectFromReflect`. -/// let type_id = output.get_represented_type_info().unwrap().type_id(); +/// let type_id = output.runtime_type_info().unwrap().type_id(); /// let reflect_from_reflect = registry.get_type_data::(type_id).unwrap(); /// let value: Box = reflect_from_reflect.from_reflect(output.as_partial_reflect()).unwrap(); /// assert!(value.is::()); @@ -254,7 +254,7 @@ impl<'de, P: ReflectDeserializerProcessor> DeserializeSeed<'de> for ReflectDeser /// assert_eq!(value, MyStruct { value: 123 }); /// /// // We can also do this dynamically with `ReflectFromReflect`. -/// let type_id = output.get_represented_type_info().unwrap().type_id(); +/// let type_id = output.runtime_type_info().unwrap().type_id(); /// let reflect_from_reflect = registry.get_type_data::(type_id).unwrap(); /// let value: Box = reflect_from_reflect.from_reflect(output.as_partial_reflect()).unwrap(); /// assert!(value.is::()); diff --git a/crates/bevy_reflect/src/serde/mod.rs b/crates/bevy_reflect/src/serde/mod.rs index 5dec549160bdc..aaea99ea3b5ad 100644 --- a/crates/bevy_reflect/src/serde/mod.rs +++ b/crates/bevy_reflect/src/serde/mod.rs @@ -300,9 +300,7 @@ mod tests { { let registration = self .registry - .get_with_type_path( - enemy.get_represented_type_info().unwrap().type_path(), - ) + .get_with_type_path(enemy.runtime_type_info().unwrap().type_path()) .unwrap(); // 1. Convert any possible dynamic values to concrete ones diff --git a/crates/bevy_reflect/src/serde/ser/custom_serialization.rs b/crates/bevy_reflect/src/serde/ser/custom_serialization.rs index 7b2dc7ce26bcf..38d49bc416c54 100644 --- a/crates/bevy_reflect/src/serde/ser/custom_serialization.rs +++ b/crates/bevy_reflect/src/serde/ser/custom_serialization.rs @@ -25,7 +25,7 @@ pub(super) fn try_custom_serialize( )); }; - let info = value.reflect_type_info(); + let info = value.comptime_type_info(); let Some(registration) = type_registry.get(info.type_id()) else { return Err(( diff --git a/crates/bevy_reflect/src/serde/ser/enums.rs b/crates/bevy_reflect/src/serde/ser/enums.rs index 6eba01e44ede1..cc777946cbe54 100644 --- a/crates/bevy_reflect/src/serde/ser/enums.rs +++ b/crates/bevy_reflect/src/serde/ser/enums.rs @@ -22,7 +22,7 @@ impl Serialize for EnumSerializer<'_, P> { where S: serde::Serializer, { - let type_info = self.enum_value.get_represented_type_info().ok_or_else(|| { + let type_info = self.enum_value.runtime_type_info().ok_or_else(|| { make_custom_error(format_args!( "cannot get type info for `{}`", self.enum_value.reflect_type_path() diff --git a/crates/bevy_reflect/src/serde/ser/mod.rs b/crates/bevy_reflect/src/serde/ser/mod.rs index a2a3712921f37..4b82122ab03ce 100644 --- a/crates/bevy_reflect/src/serde/ser/mod.rs +++ b/crates/bevy_reflect/src/serde/ser/mod.rs @@ -508,7 +508,7 @@ mod tests { return Ok(Err(serializer)); }; - let type_id = value.reflect_type_info().type_id(); + let type_id = value.comptime_type_info().type_id(); if type_id == TypeId::of::() { Ok(Ok(serializer.serialize_str("custom!")?)) } else { @@ -567,7 +567,7 @@ mod tests { return Ok(Err(serializer)); }; - let type_id = value.reflect_type_info().type_id(); + let type_id = value.comptime_type_info().type_id(); if type_id == TypeId::of::() { Ok(Ok(serializer.serialize_str("an i32")?)) } else if type_id == TypeId::of::() { @@ -621,7 +621,7 @@ mod tests { return Ok(Err(serializer)); }; - let type_id = value.reflect_type_info().type_id(); + let type_id = value.comptime_type_info().type_id(); if type_id == TypeId::of::() { Err(serde::ser::Error::custom("my custom serialize error")) } else { diff --git a/crates/bevy_reflect/src/serde/ser/processor.rs b/crates/bevy_reflect/src/serde/ser/processor.rs index fc35ff883ac74..d769814b392c1 100644 --- a/crates/bevy_reflect/src/serde/ser/processor.rs +++ b/crates/bevy_reflect/src/serde/ser/processor.rs @@ -95,7 +95,7 @@ use crate::{PartialReflect, TypeRegistry}; /// // we don't have any info on this type; do the default serialization logic /// return Ok(Err(serializer)); /// }; -/// let type_id = value.reflect_type_info().type_id(); +/// let type_id = value.comptime_type_info().type_id(); /// let Some(reflect_handle) = registry.get_type_data::(type_id) else { /// // this isn't a `Handle` /// return Ok(Err(serializer)); diff --git a/crates/bevy_reflect/src/serde/ser/serializer.rs b/crates/bevy_reflect/src/serde/ser/serializer.rs index afe6b56b1dfaf..1d5228b48005c 100644 --- a/crates/bevy_reflect/src/serde/ser/serializer.rs +++ b/crates/bevy_reflect/src/serde/ser/serializer.rs @@ -104,7 +104,7 @@ impl Serialize for ReflectSerializer<'_, P> { let mut state = serializer.serialize_map(Some(1))?; state.serialize_entry( self.value - .get_represented_type_info() + .runtime_type_info() .ok_or_else(|| { if self.value.is_dynamic() { make_custom_error(format_args!( @@ -236,7 +236,7 @@ impl Serialize for TypedReflectSerializer<'_, P> { #[cfg(feature = "debug_stack")] { - if let Some(info) = self.value.get_represented_type_info() { + if let Some(info) = self.value.runtime_type_info() { TYPE_INFO_STACK.with_borrow_mut(|stack| stack.push(info)); } } diff --git a/crates/bevy_reflect/src/serde/ser/structs.rs b/crates/bevy_reflect/src/serde/ser/structs.rs index 304b0ba274e82..8020309eaa4f9 100644 --- a/crates/bevy_reflect/src/serde/ser/structs.rs +++ b/crates/bevy_reflect/src/serde/ser/structs.rs @@ -19,15 +19,12 @@ impl Serialize for StructSerializer<'_, P> { where S: serde::Serializer, { - let type_info = self - .struct_value - .get_represented_type_info() - .ok_or_else(|| { - make_custom_error(format_args!( - "cannot get type info for `{}`", - self.struct_value.reflect_type_path() - )) - })?; + let type_info = self.struct_value.runtime_type_info().ok_or_else(|| { + make_custom_error(format_args!( + "cannot get type info for `{}`", + self.struct_value.reflect_type_path() + )) + })?; let struct_info = match type_info { TypeInfo::Struct(struct_info) => struct_info, diff --git a/crates/bevy_reflect/src/serde/ser/tuple_structs.rs b/crates/bevy_reflect/src/serde/ser/tuple_structs.rs index 850b80e253c02..1f395b2ebf257 100644 --- a/crates/bevy_reflect/src/serde/ser/tuple_structs.rs +++ b/crates/bevy_reflect/src/serde/ser/tuple_structs.rs @@ -19,15 +19,12 @@ impl Serialize for TupleStructSerializer<'_, P> { where S: serde::Serializer, { - let type_info = self - .tuple_struct - .get_represented_type_info() - .ok_or_else(|| { - make_custom_error(format_args!( - "cannot get type info for `{}`", - self.tuple_struct.reflect_type_path() - )) - })?; + let type_info = self.tuple_struct.runtime_type_info().ok_or_else(|| { + make_custom_error(format_args!( + "cannot get type info for `{}`", + self.tuple_struct.reflect_type_path() + )) + })?; let tuple_struct_info = match type_info { TypeInfo::TupleStruct(tuple_struct_info) => tuple_struct_info, diff --git a/crates/bevy_reflect/src/set.rs b/crates/bevy_reflect/src/set.rs index ee1de19d29e88..59e5819885206 100644 --- a/crates/bevy_reflect/src/set.rs +++ b/crates/bevy_reflect/src/set.rs @@ -78,7 +78,7 @@ pub trait Set: PartialReflect { /// Creates a new [`DynamicSet`] from this set. fn to_dynamic_set(&self) -> DynamicSet { let mut set = DynamicSet::default(); - set.set_represented_type(self.get_represented_type_info()); + set.set_represented_type(self.runtime_type_info()); for value in self.iter() { set.insert_boxed(value.to_dynamic()); } @@ -258,10 +258,20 @@ impl Set for DynamicSet { impl PartialReflect for DynamicSet { #[inline] - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { self.represented_type } + #[inline] + fn runtime_type(&self) -> Option { + self.represented_type.map(TypeInfo::ty).copied() + } + #[inline] fn into_partial_reflect(self: Box) -> Box { self diff --git a/crates/bevy_reflect/src/structs.rs b/crates/bevy_reflect/src/structs.rs index 738ab98817d67..72a9753e756ff 100644 --- a/crates/bevy_reflect/src/structs.rs +++ b/crates/bevy_reflect/src/structs.rs @@ -79,7 +79,7 @@ pub trait Struct: PartialReflect { /// Creates a new [`DynamicStruct`] from this struct. fn to_dynamic_struct(&self) -> DynamicStruct { let mut dynamic_struct = DynamicStruct::default(); - dynamic_struct.set_represented_type(self.get_represented_type_info()); + dynamic_struct.set_represented_type(self.runtime_type_info()); for (name, value) in self.iter_fields() { dynamic_struct.insert_boxed(name, value.to_dynamic()); } @@ -88,7 +88,7 @@ pub trait Struct: PartialReflect { /// Will return `None` if [`TypeInfo`] is not available. fn get_represented_struct_info(&self) -> Option<&'static StructInfo> { - self.get_represented_type_info()?.as_struct().ok() + self.runtime_type_info()?.as_struct().ok() } } @@ -465,10 +465,20 @@ impl Struct for DynamicStruct { impl PartialReflect for DynamicStruct { #[inline] - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { self.represented_type } + #[inline] + fn runtime_type(&self) -> Option { + self.represented_type.map(TypeInfo::ty).copied() + } + #[inline] fn into_partial_reflect(self: Box) -> Box { self @@ -751,7 +761,7 @@ where pub fn struct_debug(dyn_struct: &dyn Struct, f: &mut Formatter<'_>) -> core::fmt::Result { let mut debug = f.debug_struct( dyn_struct - .get_represented_type_info() + .runtime_type_info() .map(TypeInfo::type_path) .unwrap_or("_"), ); diff --git a/crates/bevy_reflect/src/tuple.rs b/crates/bevy_reflect/src/tuple.rs index 49fdaffb34d75..148d5cabb011c 100644 --- a/crates/bevy_reflect/src/tuple.rs +++ b/crates/bevy_reflect/src/tuple.rs @@ -61,14 +61,14 @@ pub trait Tuple: PartialReflect { /// Creates a new [`DynamicTuple`] from this tuple. fn to_dynamic_tuple(&self) -> DynamicTuple { DynamicTuple { - represented_type: self.get_represented_type_info(), + represented_type: self.runtime_type_info(), fields: self.iter_fields().map(PartialReflect::to_dynamic).collect(), } } /// Will return `None` if [`TypeInfo`] is not available. fn get_represented_tuple_info(&self) -> Option<&'static TupleInfo> { - self.get_represented_type_info()?.as_tuple().ok() + self.runtime_type_info()?.as_tuple().ok() } } @@ -289,10 +289,20 @@ impl Tuple for DynamicTuple { impl PartialReflect for DynamicTuple { #[inline] - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { self.represented_type } + #[inline] + fn runtime_type(&self) -> Option { + self.represented_type.map(TypeInfo::ty).copied() + } + #[inline] fn into_partial_reflect(self: Box) -> Box { self @@ -322,6 +332,10 @@ impl PartialReflect for DynamicTuple { tuple_apply(self, value); } + fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { + tuple_try_apply(self, value) + } + #[inline] fn reflect_kind(&self) -> ReflectKind { ReflectKind::Tuple @@ -342,10 +356,6 @@ impl PartialReflect for DynamicTuple { ReflectOwned::Tuple(self) } - fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> { - tuple_try_apply(self, value) - } - fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option { tuple_partial_eq(self, value) } @@ -550,8 +560,19 @@ macro_rules! impl_reflect_tuple { } impl<$($name: Reflect + MaybeTyped + TypePath + GetTypeRegistration),*> PartialReflect for ($($name,)*) { - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { - Some(::type_info()) + #[inline] + fn runtime_type_info(&self) -> Option<&'static $crate::info::TypeInfo> { + ::maybe_type_info() + } + + #[inline] + fn comptime_type(&self) -> $crate::ty::Type { + $crate::ty::Type::of::() + } + + #[inline] + fn runtime_type(&self) -> Option<$crate::ty::Type> { + Some($crate::ty::Type::of::()) } #[inline] diff --git a/crates/bevy_reflect/src/tuple_struct.rs b/crates/bevy_reflect/src/tuple_struct.rs index 76281012906d4..6244bdb6fd9c5 100644 --- a/crates/bevy_reflect/src/tuple_struct.rs +++ b/crates/bevy_reflect/src/tuple_struct.rs @@ -61,14 +61,14 @@ pub trait TupleStruct: PartialReflect { /// Creates a new [`DynamicTupleStruct`] from this tuple struct. fn to_dynamic_tuple_struct(&self) -> DynamicTupleStruct { DynamicTupleStruct { - represented_type: self.get_represented_type_info(), + represented_type: self.runtime_type_info(), fields: self.iter_fields().map(PartialReflect::to_dynamic).collect(), } } /// Will return `None` if [`TypeInfo`] is not available. fn get_represented_tuple_struct_info(&self) -> Option<&'static TupleStructInfo> { - self.get_represented_type_info()?.as_tuple_struct().ok() + self.runtime_type_info()?.as_tuple_struct().ok() } } @@ -298,10 +298,20 @@ impl TupleStruct for DynamicTupleStruct { impl PartialReflect for DynamicTupleStruct { #[inline] - fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { + fn comptime_type(&self) -> Type { + Type::of::() + } + + #[inline] + fn runtime_type_info(&self) -> Option<&'static TypeInfo> { self.represented_type } + #[inline] + fn runtime_type(&self) -> Option { + self.represented_type.map(TypeInfo::ty).copied() + } + #[inline] fn into_partial_reflect(self: Box) -> Box { self @@ -512,7 +522,7 @@ pub fn tuple_struct_debug( ) -> core::fmt::Result { let mut debug = f.debug_tuple( dyn_tuple_struct - .get_represented_type_info() + .runtime_type_info() .map(TypeInfo::type_path) .unwrap_or("_"), ); diff --git a/crates/bevy_reflect/src/utility.rs b/crates/bevy_reflect/src/utility.rs index 580432e5cdda4..47cc5a69ce40e 100644 --- a/crates/bevy_reflect/src/utility.rs +++ b/crates/bevy_reflect/src/utility.rs @@ -56,7 +56,7 @@ mod sealed { /// /// ``` /// # use core::any::Any; -/// # use bevy_reflect::{DynamicTypePath, NamedField, PartialReflect, Reflect, ReflectMut, ReflectOwned, ReflectRef, structs::StructInfo, Typed, TypeInfo, TypePath, ApplyError}; +/// # use bevy_reflect::{DynamicTypePath, NamedField, PartialReflect, Reflect, ReflectMut, ReflectOwned, ReflectRef, structs::StructInfo, Typed, TypeInfo, TypePath, ApplyError, Type}; /// use bevy_reflect::utility::NonGenericTypeInfoCell; /// /// struct Foo { @@ -78,7 +78,9 @@ mod sealed { /// # fn short_type_path() -> &'static str { todo!() } /// # } /// # impl PartialReflect for Foo { -/// # fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { todo!() } +/// # fn comptime_type(&self) -> Type { todo!() } +/// # fn runtime_type(&self) -> Option { todo!() } +/// # fn runtime_type_info(&self) -> Option<&'static TypeInfo> { todo!() } /// # fn into_partial_reflect(self: Box) -> Box { todo!() } /// # fn as_partial_reflect(&self) -> &dyn PartialReflect { todo!() } /// # fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect { todo!() } @@ -144,7 +146,7 @@ impl Default for NonGenericTypeCell { /// /// ``` /// # use core::any::Any; -/// # use bevy_reflect::{DynamicTypePath, PartialReflect, Reflect, ReflectMut, ReflectOwned, ReflectRef, tuple_struct::TupleStructInfo, Typed, TypeInfo, TypePath, UnnamedField, ApplyError, Generics, TypeParamInfo}; +/// # use bevy_reflect::{DynamicTypePath, PartialReflect, Reflect, ReflectMut, ReflectOwned, ReflectRef, tuple_struct::TupleStructInfo, Typed, TypeInfo, TypePath, UnnamedField, ApplyError, Generics, TypeParamInfo, Type}; /// use bevy_reflect::utility::GenericTypeInfoCell; /// /// struct Foo(T); @@ -165,7 +167,9 @@ impl Default for NonGenericTypeCell { /// # fn short_type_path() -> &'static str { todo!() } /// # } /// # impl PartialReflect for Foo { -/// # fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { todo!() } +/// # fn comptime_type(&self) -> Type { todo!() } +/// # fn runtime_type(&self) -> Option { todo!() } +/// # fn runtime_type_info(&self) -> Option<&'static TypeInfo> { todo!() } /// # fn into_partial_reflect(self: Box) -> Box { todo!() } /// # fn as_partial_reflect(&self) -> &dyn PartialReflect { todo!() } /// # fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect { todo!() } diff --git a/crates/bevy_settings/src/lib.rs b/crates/bevy_settings/src/lib.rs index 2bd75d75086a7..df6777de84366 100644 --- a/crates/bevy_settings/src/lib.rs +++ b/crates/bevy_settings/src/lib.rs @@ -521,7 +521,7 @@ fn apply_settings_to_world( } fn load_properties(value: &toml::Value, resource: &mut dyn PartialReflect, types: &TypeRegistry) { - let Some(tinfo) = resource.get_represented_type_info() else { + let Some(tinfo) = resource.runtime_type_info() else { return; }; diff --git a/crates/bevy_world_serialization/src/dynamic_world.rs b/crates/bevy_world_serialization/src/dynamic_world.rs index 526357c64e1df..dcdcc5ad1ea07 100644 --- a/crates/bevy_world_serialization/src/dynamic_world.rs +++ b/crates/bevy_world_serialization/src/dynamic_world.rs @@ -114,7 +114,7 @@ impl DynamicWorld { // Apply/ add each component to the given entity. for component in &dynamic_entity.components { - let type_info = component.get_represented_type_info().ok_or_else(|| { + let type_info = component.runtime_type_info().ok_or_else(|| { WorldInstanceSpawnError::NoRepresentedType { type_path: component.reflect_type_path().to_string(), } @@ -160,7 +160,7 @@ impl DynamicWorld { // Insert resources after all entities have been added to the world. // This ensures the entities are available for the resources to reference during mapping. for resource in &self.resources { - let type_info = resource.get_represented_type_info().ok_or_else(|| { + let type_info = resource.runtime_type_info().ok_or_else(|| { WorldInstanceSpawnError::NoRepresentedType { type_path: resource.reflect_type_path().to_string(), } diff --git a/crates/bevy_world_serialization/src/serde.rs b/crates/bevy_world_serialization/src/serde.rs index 64505eda4b8ee..a213a98374b86 100644 --- a/crates/bevy_world_serialization/src/serde.rs +++ b/crates/bevy_world_serialization/src/serde.rs @@ -178,7 +178,7 @@ impl<'a> Serialize for WorldMapSerializer<'a> { .iter() .map(|entry| { ( - entry.get_represented_type_info().unwrap().type_path(), + entry.runtime_type().unwrap().path(), entry.as_partial_reflect(), ) }) @@ -1009,13 +1009,13 @@ mod tests { .components .iter() .find(|component| { - component.get_represented_type_info().unwrap().type_path() - == expected.get_represented_type_info().unwrap().type_path() + component.runtime_type().unwrap().path() + == expected.runtime_type().unwrap().path() }) .unwrap_or_else(|| { panic!( "missing component (expected: `{}`)", - expected.get_represented_type_info().unwrap().type_path() + expected.runtime_type().unwrap().path() ) }); diff --git a/examples/reflection/type_data.rs b/examples/reflection/type_data.rs index 425f71f36b140..baa5cf98c9580 100644 --- a/examples/reflection/type_data.rs +++ b/examples/reflection/type_data.rs @@ -84,7 +84,7 @@ fn main() { registry.register_type_data::(); // Then at any point we can retrieve the type data from the registry: - let type_id = value.reflect_type_info().type_id(); + let type_id = value.comptime_type_info().type_id(); let reflect_damageable = registry .get_type_data::(type_id) .unwrap(); @@ -190,7 +190,7 @@ fn main() { // Now we can use `ReflectHealth` to convert `dyn Reflect` into `dyn Health`: let value: Box = Box::new(Skeleton { health: 50 }); - let type_id = value.reflect_type_info().type_id(); + let type_id = value.comptime_type_info().type_id(); let reflect_health = registry.get_type_data::(type_id).unwrap(); // Type data generated by `#[reflect_trait]` comes with a `get`, `get_mut`, and `get_boxed` method,