11use std:: any:: { Any , TypeId } ;
22
33use 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
66use 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
3332impl 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}
223229impl 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)
0 commit comments