bevy_ecs/reflect/resource.rs
1//! Definitions for [`Resource`] reflection.
2//!
3//! # Architecture
4//!
5//! See the module doc for [`crate::reflect::component`].
6
7use crate::{
8 change_detection::Mut,
9 system::Resource,
10 world::{unsafe_world_cell::UnsafeWorldCell, World},
11};
12use bevy_reflect::{FromReflect, FromType, Reflect, TypeRegistry};
13
14use super::from_reflect_with_fallback;
15
16/// A struct used to operate on reflected [`Resource`] of a type.
17///
18/// A [`ReflectResource`] for type `T` can be obtained via
19/// [`bevy_reflect::TypeRegistration::data`].
20#[derive(Clone)]
21pub struct ReflectResource(ReflectResourceFns);
22
23/// The raw function pointers needed to make up a [`ReflectResource`].
24///
25/// This is used when creating custom implementations of [`ReflectResource`] with
26/// [`ReflectResource::new()`].
27///
28/// > **Note:**
29/// > Creating custom implementations of [`ReflectResource`] is an advanced feature that most users
30/// > will not need.
31/// > Usually a [`ReflectResource`] is created for a type by deriving [`Reflect`]
32/// > and adding the `#[reflect(Resource)]` attribute.
33/// > After adding the component to the [`TypeRegistry`],
34/// > its [`ReflectResource`] can then be retrieved when needed.
35///
36/// Creating a custom [`ReflectResource`] may be useful if you need to create new resource types at
37/// runtime, for example, for scripting implementations.
38///
39/// By creating a custom [`ReflectResource`] and inserting it into a type's
40/// [`TypeRegistration`][bevy_reflect::TypeRegistration],
41/// you can modify the way that reflected resources of that type will be inserted into the bevy
42/// world.
43#[derive(Clone)]
44pub struct ReflectResourceFns {
45 /// Function pointer implementing [`ReflectResource::insert()`].
46 pub insert: fn(&mut World, &dyn Reflect, &TypeRegistry),
47 /// Function pointer implementing [`ReflectResource::apply()`].
48 pub apply: fn(&mut World, &dyn Reflect),
49 /// Function pointer implementing [`ReflectResource::apply_or_insert()`].
50 pub apply_or_insert: fn(&mut World, &dyn Reflect, &TypeRegistry),
51 /// Function pointer implementing [`ReflectResource::remove()`].
52 pub remove: fn(&mut World),
53 /// Function pointer implementing [`ReflectResource::reflect()`].
54 pub reflect: fn(&World) -> Option<&dyn Reflect>,
55 /// Function pointer implementing [`ReflectResource::reflect_unchecked_mut()`].
56 ///
57 /// # Safety
58 /// The function may only be called with an [`UnsafeWorldCell`] that can be used to mutably access the relevant resource.
59 pub reflect_unchecked_mut: unsafe fn(UnsafeWorldCell<'_>) -> Option<Mut<'_, dyn Reflect>>,
60 /// Function pointer implementing [`ReflectResource::copy()`].
61 pub copy: fn(&World, &mut World, &TypeRegistry),
62}
63
64impl ReflectResourceFns {
65 /// Get the default set of [`ReflectResourceFns`] for a specific resource type using its
66 /// [`FromType`] implementation.
67 ///
68 /// This is useful if you want to start with the default implementation before overriding some
69 /// of the functions to create a custom implementation.
70 pub fn new<T: Resource + FromReflect>() -> Self {
71 <ReflectResource as FromType<T>>::from_type().0
72 }
73}
74
75impl ReflectResource {
76 /// Insert a reflected [`Resource`] into the world like [`insert()`](World::insert_resource).
77 pub fn insert(&self, world: &mut World, resource: &dyn Reflect, registry: &TypeRegistry) {
78 (self.0.insert)(world, resource, registry);
79 }
80
81 /// Uses reflection to set the value of this [`Resource`] type in the world to the given value.
82 ///
83 /// # Panics
84 ///
85 /// Panics if there is no [`Resource`] of the given type.
86 pub fn apply(&self, world: &mut World, resource: &dyn Reflect) {
87 (self.0.apply)(world, resource);
88 }
89
90 /// Uses reflection to set the value of this [`Resource`] type in the world to the given value or insert a new one if it does not exist.
91 pub fn apply_or_insert(
92 &self,
93 world: &mut World,
94 resource: &dyn Reflect,
95 registry: &TypeRegistry,
96 ) {
97 (self.0.apply_or_insert)(world, resource, registry);
98 }
99
100 /// Removes this [`Resource`] type from the world. Does nothing if it doesn't exist.
101 pub fn remove(&self, world: &mut World) {
102 (self.0.remove)(world);
103 }
104
105 /// Gets the value of this [`Resource`] type from the world as a reflected reference.
106 pub fn reflect<'a>(&self, world: &'a World) -> Option<&'a dyn Reflect> {
107 (self.0.reflect)(world)
108 }
109
110 /// Gets the value of this [`Resource`] type from the world as a mutable reflected reference.
111 pub fn reflect_mut<'a>(&self, world: &'a mut World) -> Option<Mut<'a, dyn Reflect>> {
112 // SAFETY: unique world access
113 unsafe { (self.0.reflect_unchecked_mut)(world.as_unsafe_world_cell()) }
114 }
115
116 /// # Safety
117 /// This method does not prevent you from having two mutable pointers to the same data,
118 /// violating Rust's aliasing rules. To avoid this:
119 /// * Only call this method with an [`UnsafeWorldCell`] which can be used to mutably access the resource.
120 /// * Don't call this method more than once in the same scope for a given [`Resource`].
121 pub unsafe fn reflect_unchecked_mut<'w>(
122 &self,
123 world: UnsafeWorldCell<'w>,
124 ) -> Option<Mut<'w, dyn Reflect>> {
125 // SAFETY: caller promises to uphold uniqueness guarantees
126 unsafe { (self.0.reflect_unchecked_mut)(world) }
127 }
128
129 /// Gets the value of this [`Resource`] type from `source_world` and [applies](Self::apply()) it to the value of this [`Resource`] type in `destination_world`.
130 ///
131 /// # Panics
132 ///
133 /// Panics if there is no [`Resource`] of the given type.
134 pub fn copy(
135 &self,
136 source_world: &World,
137 destination_world: &mut World,
138 registry: &TypeRegistry,
139 ) {
140 (self.0.copy)(source_world, destination_world, registry);
141 }
142
143 /// Create a custom implementation of [`ReflectResource`].
144 ///
145 /// This is an advanced feature,
146 /// useful for scripting implementations,
147 /// that should not be used by most users
148 /// unless you know what you are doing.
149 ///
150 /// Usually you should derive [`Reflect`] and add the `#[reflect(Resource)]` component
151 /// to generate a [`ReflectResource`] implementation automatically.
152 ///
153 /// See [`ReflectResourceFns`] for more information.
154 pub fn new(&self, fns: ReflectResourceFns) -> Self {
155 Self(fns)
156 }
157
158 /// The underlying function pointers implementing methods on `ReflectResource`.
159 ///
160 /// This is useful when you want to keep track locally of an individual
161 /// function pointer.
162 ///
163 /// Calling [`TypeRegistry::get`] followed by
164 /// [`TypeRegistration::data::<ReflectResource>`] can be costly if done several
165 /// times per frame. Consider cloning [`ReflectResource`] and keeping it
166 /// between frames, cloning a `ReflectResource` is very cheap.
167 ///
168 /// If you only need a subset of the methods on `ReflectResource`,
169 /// use `fn_pointers` to get the underlying [`ReflectResourceFns`]
170 /// and copy the subset of function pointers you care about.
171 ///
172 /// [`TypeRegistration::data::<ReflectResource>`]: bevy_reflect::TypeRegistration::data
173 /// [`TypeRegistry::get`]: bevy_reflect::TypeRegistry::get
174 pub fn fn_pointers(&self) -> &ReflectResourceFns {
175 &self.0
176 }
177}
178
179impl<R: Resource + FromReflect> FromType<R> for ReflectResource {
180 fn from_type() -> Self {
181 ReflectResource(ReflectResourceFns {
182 insert: |world, reflected_resource, registry| {
183 let resource = from_reflect_with_fallback::<R>(reflected_resource, world, registry);
184 world.insert_resource(resource);
185 },
186 apply: |world, reflected_resource| {
187 let mut resource = world.resource_mut::<R>();
188 resource.apply(reflected_resource);
189 },
190 apply_or_insert: |world, reflected_resource, registry| {
191 if let Some(mut resource) = world.get_resource_mut::<R>() {
192 resource.apply(reflected_resource);
193 } else {
194 let resource =
195 from_reflect_with_fallback::<R>(reflected_resource, world, registry);
196 world.insert_resource(resource);
197 }
198 },
199 remove: |world| {
200 world.remove_resource::<R>();
201 },
202 reflect: |world| world.get_resource::<R>().map(|res| res as &dyn Reflect),
203 reflect_unchecked_mut: |world| {
204 // SAFETY: all usages of `reflect_unchecked_mut` guarantee that there is either a single mutable
205 // reference or multiple immutable ones alive at any given point
206 unsafe {
207 world.get_resource_mut::<R>().map(|res| Mut {
208 value: res.value as &mut dyn Reflect,
209 ticks: res.ticks,
210 })
211 }
212 },
213 copy: |source_world, destination_world, registry| {
214 let source_resource = source_world.resource::<R>();
215 let destination_resource =
216 from_reflect_with_fallback::<R>(source_resource, destination_world, registry);
217 destination_world.insert_resource(destination_resource);
218 },
219 })
220 }
221}