Skip to content

Commit 9f10455

Browse files
committed
use Reflect in more places
1 parent ecf4c52 commit 9f10455

File tree

8 files changed

+82
-69
lines changed

8 files changed

+82
-69
lines changed

crates/bevy_app/src/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ impl App {
10821082
/// See [`bevy_reflect::TypeRegistry::register_type_data`].
10831083
#[cfg(feature = "bevy_reflect")]
10841084
pub fn register_type_data<
1085-
T: bevy_reflect::PartialReflect + 'static,
1085+
T: bevy_reflect::Reflect + 'static,
10861086
D: bevy_reflect::TypeData + bevy_reflect::FromType<T>,
10871087
>(
10881088
&mut self,

crates/bevy_asset/src/reflect.rs

+43-40
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::any::{Any, TypeId};
22

33
use bevy_ecs::world::{unsafe_world_cell::UnsafeWorldCell, World};
4-
use bevy_reflect::{FromReflect, FromType, PartialReflect, Uuid};
4+
use bevy_reflect::{FromReflect, FromType, Reflect, Uuid};
55

66
use crate::{Asset, Assets, Handle, HandleId, HandleUntyped};
77

@@ -17,17 +17,16 @@ pub struct ReflectAsset {
1717
handle_type_id: TypeId,
1818
assets_resource_type_id: TypeId,
1919

20-
get: fn(&World, HandleUntyped) -> Option<&dyn PartialReflect>,
20+
get: fn(&World, HandleUntyped) -> Option<&dyn Reflect>,
2121
// SAFETY:
2222
// - may only be called with a [`IteriorMutableWorld`] which can be used to access the corresponding `Assets<T>` resource mutably
2323
// - may only be used to access **at most one** access at once
24-
get_unchecked_mut:
25-
unsafe fn(UnsafeWorldCell<'_>, HandleUntyped) -> Option<&mut dyn PartialReflect>,
26-
add: fn(&mut World, &dyn PartialReflect) -> HandleUntyped,
27-
set: fn(&mut World, HandleUntyped, &dyn PartialReflect) -> HandleUntyped,
24+
get_unchecked_mut: unsafe fn(UnsafeWorldCell<'_>, HandleUntyped) -> Option<&mut dyn Reflect>,
25+
add: fn(&mut World, Box<dyn Reflect>) -> HandleUntyped,
26+
set: fn(&mut World, HandleUntyped, Box<dyn Reflect>) -> HandleUntyped,
2827
len: fn(&World) -> usize,
2928
ids: for<'w> fn(&'w World) -> Box<dyn Iterator<Item = HandleId> + 'w>,
30-
remove: fn(&mut World, HandleUntyped) -> Option<Box<dyn PartialReflect>>,
29+
remove: fn(&mut World, HandleUntyped) -> Option<Box<dyn Reflect>>,
3130
}
3231

3332
impl ReflectAsset {
@@ -47,11 +46,7 @@ impl ReflectAsset {
4746
}
4847

4948
/// Equivalent of [`Assets::get`]
50-
pub fn get<'w>(
51-
&self,
52-
world: &'w World,
53-
handle: HandleUntyped,
54-
) -> Option<&'w dyn PartialReflect> {
49+
pub fn get<'w>(&self, world: &'w World, handle: HandleUntyped) -> Option<&'w dyn Reflect> {
5550
(self.get)(world, handle)
5651
}
5752

@@ -60,7 +55,7 @@ impl ReflectAsset {
6055
&self,
6156
world: &'w mut World,
6257
handle: HandleUntyped,
63-
) -> Option<&'w mut dyn PartialReflect> {
58+
) -> Option<&'w mut dyn Reflect> {
6459
// SAFETY: unique world access
6560
unsafe { (self.get_unchecked_mut)(world.as_unsafe_world_cell(), handle) }
6661
}
@@ -96,31 +91,27 @@ impl ReflectAsset {
9691
&self,
9792
world: UnsafeWorldCell<'w>,
9893
handle: HandleUntyped,
99-
) -> Option<&'w mut dyn PartialReflect> {
94+
) -> Option<&'w mut dyn Reflect> {
10095
// SAFETY: requirements are deferred to the caller
10196
(self.get_unchecked_mut)(world, handle)
10297
}
10398

10499
/// Equivalent of [`Assets::add`]
105-
pub fn add(&self, world: &mut World, value: &dyn PartialReflect) -> HandleUntyped {
100+
pub fn add(&self, world: &mut World, value: Box<dyn Reflect>) -> HandleUntyped {
106101
(self.add)(world, value)
107102
}
108103
/// Equivalent of [`Assets::set`]
109104
pub fn set(
110105
&self,
111106
world: &mut World,
112107
handle: HandleUntyped,
113-
value: &dyn PartialReflect,
108+
value: Box<dyn Reflect>,
114109
) -> HandleUntyped {
115110
(self.set)(world, handle, value)
116111
}
117112

118113
/// Equivalent of [`Assets::remove`]
119-
pub fn remove(
120-
&self,
121-
world: &mut World,
122-
handle: HandleUntyped,
123-
) -> Option<Box<dyn PartialReflect>> {
114+
pub fn remove(&self, world: &mut World, handle: HandleUntyped) -> Option<Box<dyn Reflect>> {
124115
(self.remove)(world, handle)
125116
}
126117

@@ -150,26 +141,41 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
150141
get: |world, handle| {
151142
let assets = world.resource::<Assets<A>>();
152143
let asset = assets.get(&handle.typed());
153-
asset.map(|asset| asset as &dyn PartialReflect)
144+
asset.map(|asset| asset as &dyn Reflect)
154145
},
155146
get_unchecked_mut: |world, handle| {
156147
// SAFETY: `get_unchecked_mut` must be callied with `UnsafeWorldCell` having access to `Assets<A>`,
157148
// and must ensure to only have at most one reference to it live at all times.
158149
let assets = unsafe { world.get_resource_mut::<Assets<A>>().unwrap().into_inner() };
159150
let asset = assets.get_mut(&handle.typed());
160-
asset.map(|asset| asset as &mut dyn PartialReflect)
151+
asset.map(|asset| asset as &mut dyn Reflect)
161152
},
162153
add: |world, value| {
163154
let mut assets = world.resource_mut::<Assets<A>>();
164-
let value: A = FromReflect::from_reflect(value)
165-
.expect("could not call `FromReflect::from_reflect` in `ReflectAsset::add`");
166-
assets.add(value).into()
155+
assets
156+
.add(value.take::<A>().unwrap_or_else(|value| {
157+
panic!(
158+
"ReflectAsset::add called with type `{}`, even though it was created for `{}`",
159+
value.type_name(),
160+
std::any::type_name::<A>()
161+
)
162+
}))
163+
.into()
167164
},
168165
set: |world, handle, value| {
169166
let mut assets = world.resource_mut::<Assets<A>>();
170-
let value: A = FromReflect::from_reflect(value)
171-
.expect("could not call `FromReflect::from_reflect` in `ReflectAsset::set`");
172-
assets.set(handle, value).into()
167+
assets
168+
.set(
169+
handle,
170+
value.take::<A>().unwrap_or_else(|value| {
171+
panic!(
172+
"ReflectAsset::set called with type `{}`, even though it was created for `{}`",
173+
value.type_name(),
174+
std::any::type_name::<A>()
175+
)
176+
}),
177+
)
178+
.into()
173179
},
174180
len: |world| {
175181
let assets = world.resource::<Assets<A>>();
@@ -182,15 +188,15 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
182188
remove: |world, handle| {
183189
let mut assets = world.resource_mut::<Assets<A>>();
184190
let value = assets.remove(handle);
185-
value.map(|value| Box::new(value) as Box<dyn PartialReflect>)
191+
value.map(|value| Box::new(value) as Box<dyn Reflect>)
186192
},
187193
}
188194
}
189195
}
190196

191197
/// Reflect type data struct relating a [`Handle<T>`] back to the `T` asset type.
192198
///
193-
/// Say you want to look up the asset values of a list of handles when you have access to their `&dyn PartialReflect` form.
199+
/// Say you want to look up the asset values of a list of handles when you have access to their `&dyn Reflect` form.
194200
/// Assets can be looked up in the world using [`ReflectAsset`], but how do you determine which [`ReflectAsset`] to use when
195201
/// only looking at the handle? [`ReflectHandle`] is stored in the type registry on each `Handle<T>` type, so you can use [`ReflectHandle::asset_type_id`] to look up
196202
/// the [`ReflectAsset`] type data on the corresponding `T` asset type:
@@ -203,7 +209,7 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
203209
///
204210
/// # let world: &World = unimplemented!();
205211
/// # let type_registry: TypeRegistry = unimplemented!();
206-
/// let handles: Vec<&dyn PartialReflect> = unimplemented!();
212+
/// let handles: Vec<&dyn Reflect> = unimplemented!();
207213
/// for handle in handles {
208214
/// let reflect_handle = type_registry.get_type_data::<ReflectHandle>(handle.type_id()).unwrap();
209215
/// let reflect_asset = type_registry.get_type_data::<ReflectAsset>(reflect_handle.asset_type_id()).unwrap();
@@ -218,7 +224,7 @@ pub struct ReflectHandle {
218224
type_uuid: Uuid,
219225
asset_type_id: TypeId,
220226
downcast_handle_untyped: fn(&dyn Any) -> Option<HandleUntyped>,
221-
typed: fn(HandleUntyped) -> Box<dyn PartialReflect>,
227+
typed: fn(HandleUntyped) -> Box<dyn Reflect>,
222228
}
223229
impl ReflectHandle {
224230
/// The [`bevy_reflect::TypeUuid`] of the asset
@@ -235,9 +241,9 @@ impl ReflectHandle {
235241
(self.downcast_handle_untyped)(handle)
236242
}
237243

238-
/// A way to go from a [`HandleUntyped`] to a [`Handle<T>`] in a `Box<dyn PartialReflect>`.
244+
/// A way to go from a [`HandleUntyped`] to a [`Handle<T>`] in a `Box<dyn Reflect>`.
239245
/// Equivalent of [`HandleUntyped::typed`].
240-
pub fn typed(&self, handle: HandleUntyped) -> Box<dyn PartialReflect> {
246+
pub fn typed(&self, handle: HandleUntyped) -> Box<dyn Reflect> {
241247
(self.typed)(handle)
242248
}
243249
}
@@ -293,7 +299,7 @@ mod tests {
293299
field: "test".into(),
294300
};
295301

296-
let handle = reflect_asset.add(&mut app.world, &value);
302+
let handle = reflect_asset.add(&mut app.world, Box::new(value));
297303
let strukt = match reflect_asset
298304
.get_mut(&mut app.world, handle)
299305
.unwrap()
@@ -315,10 +321,7 @@ mod tests {
315321
let asset = reflect_asset
316322
.get(&app.world, fetched_handle.clone_weak())
317323
.unwrap();
318-
assert_eq!(
319-
asset.try_downcast_ref::<AssetType>().unwrap().field,
320-
"edited"
321-
);
324+
assert_eq!(asset.downcast_ref::<AssetType>().unwrap().field, "edited");
322325

323326
reflect_asset
324327
.remove(&mut app.world, fetched_handle)

crates/bevy_core_pipeline/src/bloom/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bevy_ecs::{
88
world::{FromWorld, World},
99
};
1010
use bevy_math::{UVec2, UVec4, Vec4};
11-
use bevy_reflect::{PartialReflect, Reflect, TypeUuid};
11+
use bevy_reflect::{Reflect, TypeUuid};
1212
use bevy_render::{
1313
camera::ExtractedCamera,
1414
extract_component::{

crates/bevy_pbr/src/bundle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
};
55
use bevy_asset::Handle;
66
use bevy_ecs::{bundle::Bundle, component::Component, prelude::Entity, reflect::ReflectComponent};
7-
use bevy_reflect::{PartialReflect, Reflect};
7+
use bevy_reflect::Reflect;
88
use bevy_render::{
99
mesh::Mesh,
1010
primitives::{CascadesFrusta, CubemapFrusta, Frustum},

crates/bevy_reflect/src/serde/de.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ impl<'a, 'de> DeserializeSeed<'de> for TypedReflectDeserializer<'a> {
375375
// Handle both Value case and types that have a custom `ReflectDeserialize`
376376
if let Some(deserialize_reflect) = self.registration.data::<ReflectDeserialize>() {
377377
let value = deserialize_reflect.deserialize(deserializer)?;
378-
return Ok(value);
378+
return Ok(value.into_partial());
379379
}
380380

381381
match self.registration.type_info() {

crates/bevy_reflect/src/serde/ser.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@ fn get_serializable<'a, E: serde::ser::Error>(
3232
reflect_value: &'a dyn PartialReflect,
3333
type_registry: &TypeRegistry,
3434
) -> Result<Serializable<'a>, E> {
35+
let full_reflect = reflect_value.as_full().ok_or_else(|| {
36+
serde::ser::Error::custom(format_args!(
37+
"Type '{}' does not implement Reflect",
38+
reflect_value.type_name(),
39+
))
40+
})?;
3541
let reflect_serialize = type_registry
36-
.get_type_data::<ReflectSerialize>(reflect_value.type_id())
42+
.get_type_data::<ReflectSerialize>(full_reflect.type_id())
3743
.ok_or_else(|| {
3844
serde::ser::Error::custom(format_args!(
3945
"Type '{}' did not register ReflectSerialize",
4046
reflect_value.type_name()
4147
))
4248
})?;
43-
Ok(reflect_serialize.get_serializable(reflect_value))
49+
Ok(reflect_serialize.get_serializable(full_reflect))
4450
}
4551

4652
/// Get the underlying [`TypeInfo`] of a given type.

0 commit comments

Comments
 (0)