bevy_reflect/utility.rs
1//! Helpers for working with Bevy reflection.
2
3use crate::TypeInfo;
4use bevy_utils::{FixedState, NoOpHash, TypeIdMap};
5use std::{
6 any::{Any, TypeId},
7 hash::BuildHasher,
8 sync::{OnceLock, PoisonError, RwLock},
9};
10
11/// A type that can be stored in a ([`Non`])[`GenericTypeCell`].
12///
13/// [`Non`]: NonGenericTypeCell
14pub trait TypedProperty: sealed::Sealed {
15 type Stored: 'static;
16}
17
18/// Used to store a [`String`] in a [`GenericTypePathCell`] as part of a [`TypePath`] implementation.
19///
20/// [`TypePath`]: crate::TypePath
21pub struct TypePathComponent;
22
23mod sealed {
24 use super::{TypeInfo, TypePathComponent, TypedProperty};
25
26 pub trait Sealed {}
27
28 impl Sealed for TypeInfo {}
29 impl Sealed for TypePathComponent {}
30
31 impl TypedProperty for TypeInfo {
32 type Stored = Self;
33 }
34
35 impl TypedProperty for TypePathComponent {
36 type Stored = String;
37 }
38}
39
40/// A container for [`TypeInfo`] over non-generic types, allowing instances to be stored statically.
41///
42/// This is specifically meant for use with _non_-generic types. If your type _is_ generic,
43/// then use [`GenericTypeCell`] instead. Otherwise, it will not take into account all
44/// monomorphizations of your type.
45///
46/// Non-generic [`TypePath`]s should be trivially generated with string literals and [`concat!`].
47///
48/// ## Example
49///
50/// ```
51/// # use std::any::Any;
52/// # use bevy_reflect::{DynamicTypePath, NamedField, Reflect, ReflectMut, ReflectOwned, ReflectRef, StructInfo, Typed, TypeInfo, TypePath, ApplyError};
53/// use bevy_reflect::utility::NonGenericTypeInfoCell;
54///
55/// struct Foo {
56/// bar: i32
57/// }
58///
59/// impl Typed for Foo {
60/// fn type_info() -> &'static TypeInfo {
61/// static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
62/// CELL.get_or_set(|| {
63/// let fields = [NamedField::new::<i32>("bar")];
64/// let info = StructInfo::new::<Self>(&fields);
65/// TypeInfo::Struct(info)
66/// })
67/// }
68/// }
69/// # impl TypePath for Foo {
70/// # fn type_path() -> &'static str { todo!() }
71/// # fn short_type_path() -> &'static str { todo!() }
72/// # }
73/// # impl Reflect for Foo {
74/// # fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { todo!() }
75/// # fn into_any(self: Box<Self>) -> Box<dyn Any> { todo!() }
76/// # fn as_any(&self) -> &dyn Any { todo!() }
77/// # fn as_any_mut(&mut self) -> &mut dyn Any { todo!() }
78/// # fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> { todo!() }
79/// # fn as_reflect(&self) -> &dyn Reflect { todo!() }
80/// # fn as_reflect_mut(&mut self) -> &mut dyn Reflect { todo!() }
81/// # fn try_apply(&mut self, value: &dyn Reflect) -> Result<(), ApplyError> { todo!() }
82/// # fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> { todo!() }
83/// # fn reflect_ref(&self) -> ReflectRef { todo!() }
84/// # fn reflect_mut(&mut self) -> ReflectMut { todo!() }
85/// # fn reflect_owned(self: Box<Self>) -> ReflectOwned { todo!() }
86/// # fn clone_value(&self) -> Box<dyn Reflect> { todo!() }
87/// # }
88/// ```
89///
90/// [`TypePath`]: crate::TypePath
91pub struct NonGenericTypeCell<T: TypedProperty>(OnceLock<T::Stored>);
92
93/// See [`NonGenericTypeCell`].
94pub type NonGenericTypeInfoCell = NonGenericTypeCell<TypeInfo>;
95
96impl<T: TypedProperty> NonGenericTypeCell<T> {
97 /// Initialize a [`NonGenericTypeCell`] for non-generic types.
98 pub const fn new() -> Self {
99 Self(OnceLock::new())
100 }
101
102 /// Returns a reference to the [`TypedProperty`] stored in the cell.
103 ///
104 /// If there is no entry found, a new one will be generated from the given function.
105 pub fn get_or_set<F>(&self, f: F) -> &T::Stored
106 where
107 F: FnOnce() -> T::Stored,
108 {
109 self.0.get_or_init(f)
110 }
111}
112
113impl<T: TypedProperty> Default for NonGenericTypeCell<T> {
114 fn default() -> Self {
115 Self::new()
116 }
117}
118
119/// A container for [`TypedProperty`] over generic types, allowing instances to be stored statically.
120///
121/// This is specifically meant for use with generic types. If your type isn't generic,
122/// then use [`NonGenericTypeCell`] instead as it should be much more performant.
123///
124/// `#[derive(TypePath)]` and [`impl_type_path`] should always be used over [`GenericTypePathCell`]
125/// where possible.
126///
127/// ## Examples
128///
129/// Implementing [`TypeInfo`] with generics.
130///
131/// ```
132/// # use std::any::Any;
133/// # use bevy_reflect::{DynamicTypePath, Reflect, ReflectMut, ReflectOwned, ReflectRef, TupleStructInfo, Typed, TypeInfo, TypePath, UnnamedField, ApplyError};
134/// use bevy_reflect::utility::GenericTypeInfoCell;
135///
136/// struct Foo<T>(T);
137///
138/// impl<T: Reflect + TypePath> Typed for Foo<T> {
139/// fn type_info() -> &'static TypeInfo {
140/// static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
141/// CELL.get_or_insert::<Self, _>(|| {
142/// let fields = [UnnamedField::new::<T>(0)];
143/// let info = TupleStructInfo::new::<Self>(&fields);
144/// TypeInfo::TupleStruct(info)
145/// })
146/// }
147/// }
148/// # impl<T: TypePath> TypePath for Foo<T> {
149/// # fn type_path() -> &'static str { todo!() }
150/// # fn short_type_path() -> &'static str { todo!() }
151/// # }
152/// # impl<T: Reflect + TypePath> Reflect for Foo<T> {
153/// # fn get_represented_type_info(&self) -> Option<&'static TypeInfo> { todo!() }
154/// # fn into_any(self: Box<Self>) -> Box<dyn Any> { todo!() }
155/// # fn as_any(&self) -> &dyn Any { todo!() }
156/// # fn as_any_mut(&mut self) -> &mut dyn Any { todo!() }
157/// # fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> { todo!() }
158/// # fn as_reflect(&self) -> &dyn Reflect { todo!() }
159/// # fn as_reflect_mut(&mut self) -> &mut dyn Reflect { todo!() }
160/// # fn try_apply(&mut self, value: &dyn Reflect) -> Result<(), ApplyError> { todo!() }
161/// # fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> { todo!() }
162/// # fn reflect_ref(&self) -> ReflectRef { todo!() }
163/// # fn reflect_mut(&mut self) -> ReflectMut { todo!() }
164/// # fn reflect_owned(self: Box<Self>) -> ReflectOwned { todo!() }
165/// # fn clone_value(&self) -> Box<dyn Reflect> { todo!() }
166/// # }
167/// ```
168///
169/// Implementing [`TypePath`] with generics.
170///
171/// ```
172/// # use std::any::Any;
173/// # use bevy_reflect::TypePath;
174/// use bevy_reflect::utility::GenericTypePathCell;
175///
176/// struct Foo<T>(T);
177///
178/// impl<T: TypePath> TypePath for Foo<T> {
179/// fn type_path() -> &'static str {
180/// static CELL: GenericTypePathCell = GenericTypePathCell::new();
181/// CELL.get_or_insert::<Self, _>(|| format!("my_crate::foo::Foo<{}>", T::type_path()))
182/// }
183///
184/// fn short_type_path() -> &'static str {
185/// static CELL: GenericTypePathCell = GenericTypePathCell::new();
186/// CELL.get_or_insert::<Self, _>(|| format!("Foo<{}>", T::short_type_path()))
187/// }
188///
189/// fn type_ident() -> Option<&'static str> {
190/// Some("Foo")
191/// }
192///
193/// fn module_path() -> Option<&'static str> {
194/// Some("my_crate::foo")
195/// }
196///
197/// fn crate_name() -> Option<&'static str> {
198/// Some("my_crate")
199/// }
200/// }
201/// ```
202/// [`impl_type_path`]: crate::impl_type_path
203/// [`TypePath`]: crate::TypePath
204pub struct GenericTypeCell<T: TypedProperty>(RwLock<TypeIdMap<&'static T::Stored>>);
205
206/// See [`GenericTypeCell`].
207pub type GenericTypeInfoCell = GenericTypeCell<TypeInfo>;
208/// See [`GenericTypeCell`].
209pub type GenericTypePathCell = GenericTypeCell<TypePathComponent>;
210
211impl<T: TypedProperty> GenericTypeCell<T> {
212 /// Initialize a [`GenericTypeCell`] for generic types.
213 pub const fn new() -> Self {
214 Self(RwLock::new(TypeIdMap::with_hasher(NoOpHash)))
215 }
216
217 /// Returns a reference to the [`TypedProperty`] stored in the cell.
218 ///
219 /// This method will then return the correct [`TypedProperty`] reference for the given type `T`.
220 /// If there is no entry found, a new one will be generated from the given function.
221 pub fn get_or_insert<G, F>(&self, f: F) -> &T::Stored
222 where
223 G: Any + ?Sized,
224 F: FnOnce() -> T::Stored,
225 {
226 let type_id = TypeId::of::<G>();
227
228 // Put in a separate scope, so `mapping` is dropped before `f`,
229 // since `f` might want to call `get_or_insert` recursively
230 // and we don't want a deadlock!
231 {
232 let mapping = self.0.read().unwrap_or_else(PoisonError::into_inner);
233 if let Some(info) = mapping.get(&type_id) {
234 return info;
235 }
236 }
237
238 let value = f();
239
240 let mut mapping = self.0.write().unwrap_or_else(PoisonError::into_inner);
241 mapping
242 .entry(type_id)
243 .insert({
244 // We leak here in order to obtain a `&'static` reference.
245 // Otherwise, we won't be able to return a reference due to the `RwLock`.
246 // This should be okay, though, since we expect it to remain statically
247 // available over the course of the application.
248 Box::leak(Box::new(value))
249 })
250 .get()
251 }
252}
253
254impl<T: TypedProperty> Default for GenericTypeCell<T> {
255 fn default() -> Self {
256 Self::new()
257 }
258}
259
260/// Deterministic fixed state hasher to be used by implementors of [`Reflect::reflect_hash`].
261///
262/// Hashes should be deterministic across processes so hashes can be used as
263/// checksums for saved scenes, rollback snapshots etc. This function returns
264/// such a hasher.
265///
266/// [`Reflect::reflect_hash`]: crate::Reflect
267#[inline]
268pub fn reflect_hasher() -> bevy_utils::AHasher {
269 FixedState.build_hasher()
270}