1
1
use std:: any:: { Any , TypeId } ;
2
2
3
3
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 } ;
5
5
6
6
use crate :: { Asset , Assets , Handle , HandleId , HandleUntyped } ;
7
7
@@ -17,17 +17,16 @@ pub struct ReflectAsset {
17
17
handle_type_id : TypeId ,
18
18
assets_resource_type_id : TypeId ,
19
19
20
- get : fn ( & World , HandleUntyped ) -> Option < & dyn PartialReflect > ,
20
+ get : fn ( & World , HandleUntyped ) -> Option < & dyn Reflect > ,
21
21
// SAFETY:
22
22
// - may only be called with a [`IteriorMutableWorld`] which can be used to access the corresponding `Assets<T>` resource mutably
23
23
// - 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 ,
28
27
len : fn ( & World ) -> usize ,
29
28
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 > > ,
31
30
}
32
31
33
32
impl ReflectAsset {
@@ -47,11 +46,7 @@ impl ReflectAsset {
47
46
}
48
47
49
48
/// 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 > {
55
50
( self . get ) ( world, handle)
56
51
}
57
52
@@ -60,7 +55,7 @@ impl ReflectAsset {
60
55
& self ,
61
56
world : & ' w mut World ,
62
57
handle : HandleUntyped ,
63
- ) -> Option < & ' w mut dyn PartialReflect > {
58
+ ) -> Option < & ' w mut dyn Reflect > {
64
59
// SAFETY: unique world access
65
60
unsafe { ( self . get_unchecked_mut ) ( world. as_unsafe_world_cell ( ) , handle) }
66
61
}
@@ -96,31 +91,27 @@ impl ReflectAsset {
96
91
& self ,
97
92
world : UnsafeWorldCell < ' w > ,
98
93
handle : HandleUntyped ,
99
- ) -> Option < & ' w mut dyn PartialReflect > {
94
+ ) -> Option < & ' w mut dyn Reflect > {
100
95
// SAFETY: requirements are deferred to the caller
101
96
( self . get_unchecked_mut ) ( world, handle)
102
97
}
103
98
104
99
/// 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 {
106
101
( self . add ) ( world, value)
107
102
}
108
103
/// Equivalent of [`Assets::set`]
109
104
pub fn set (
110
105
& self ,
111
106
world : & mut World ,
112
107
handle : HandleUntyped ,
113
- value : & dyn PartialReflect ,
108
+ value : Box < dyn Reflect > ,
114
109
) -> HandleUntyped {
115
110
( self . set ) ( world, handle, value)
116
111
}
117
112
118
113
/// 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 > > {
124
115
( self . remove ) ( world, handle)
125
116
}
126
117
@@ -150,26 +141,41 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
150
141
get : |world, handle| {
151
142
let assets = world. resource :: < Assets < A > > ( ) ;
152
143
let asset = assets. get ( & handle. typed ( ) ) ;
153
- asset. map ( |asset| asset as & dyn PartialReflect )
144
+ asset. map ( |asset| asset as & dyn Reflect )
154
145
} ,
155
146
get_unchecked_mut : |world, handle| {
156
147
// SAFETY: `get_unchecked_mut` must be callied with `UnsafeWorldCell` having access to `Assets<A>`,
157
148
// and must ensure to only have at most one reference to it live at all times.
158
149
let assets = unsafe { world. get_resource_mut :: < Assets < A > > ( ) . unwrap ( ) . into_inner ( ) } ;
159
150
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 )
161
152
} ,
162
153
add : |world, value| {
163
154
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 ( )
167
164
} ,
168
165
set : |world, handle, value| {
169
166
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 ( )
173
179
} ,
174
180
len : |world| {
175
181
let assets = world. resource :: < Assets < A > > ( ) ;
@@ -182,15 +188,15 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
182
188
remove : |world, handle| {
183
189
let mut assets = world. resource_mut :: < Assets < A > > ( ) ;
184
190
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 > )
186
192
} ,
187
193
}
188
194
}
189
195
}
190
196
191
197
/// Reflect type data struct relating a [`Handle<T>`] back to the `T` asset type.
192
198
///
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.
194
200
/// Assets can be looked up in the world using [`ReflectAsset`], but how do you determine which [`ReflectAsset`] to use when
195
201
/// 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
196
202
/// the [`ReflectAsset`] type data on the corresponding `T` asset type:
@@ -203,7 +209,7 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
203
209
///
204
210
/// # let world: &World = unimplemented!();
205
211
/// # let type_registry: TypeRegistry = unimplemented!();
206
- /// let handles: Vec<&dyn PartialReflect > = unimplemented!();
212
+ /// let handles: Vec<&dyn Reflect > = unimplemented!();
207
213
/// for handle in handles {
208
214
/// let reflect_handle = type_registry.get_type_data::<ReflectHandle>(handle.type_id()).unwrap();
209
215
/// let reflect_asset = type_registry.get_type_data::<ReflectAsset>(reflect_handle.asset_type_id()).unwrap();
@@ -218,7 +224,7 @@ pub struct ReflectHandle {
218
224
type_uuid : Uuid ,
219
225
asset_type_id : TypeId ,
220
226
downcast_handle_untyped : fn ( & dyn Any ) -> Option < HandleUntyped > ,
221
- typed : fn ( HandleUntyped ) -> Box < dyn PartialReflect > ,
227
+ typed : fn ( HandleUntyped ) -> Box < dyn Reflect > ,
222
228
}
223
229
impl ReflectHandle {
224
230
/// The [`bevy_reflect::TypeUuid`] of the asset
@@ -235,9 +241,9 @@ impl ReflectHandle {
235
241
( self . downcast_handle_untyped ) ( handle)
236
242
}
237
243
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 >`.
239
245
/// Equivalent of [`HandleUntyped::typed`].
240
- pub fn typed ( & self , handle : HandleUntyped ) -> Box < dyn PartialReflect > {
246
+ pub fn typed ( & self , handle : HandleUntyped ) -> Box < dyn Reflect > {
241
247
( self . typed ) ( handle)
242
248
}
243
249
}
@@ -293,7 +299,7 @@ mod tests {
293
299
field : "test" . into ( ) ,
294
300
} ;
295
301
296
- let handle = reflect_asset. add ( & mut app. world , & value) ;
302
+ let handle = reflect_asset. add ( & mut app. world , Box :: new ( value) ) ;
297
303
let strukt = match reflect_asset
298
304
. get_mut ( & mut app. world , handle)
299
305
. unwrap ( )
@@ -315,10 +321,7 @@ mod tests {
315
321
let asset = reflect_asset
316
322
. get ( & app. world , fetched_handle. clone_weak ( ) )
317
323
. 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" ) ;
322
325
323
326
reflect_asset
324
327
. remove ( & mut app. world , fetched_handle)
0 commit comments