@@ -18,16 +18,16 @@ pub struct ReflectAsset {
18
18
handle_type_id : TypeId ,
19
19
assets_resource_type_id : TypeId ,
20
20
21
- get : fn ( & World , UntypedHandle ) -> Option < & dyn Reflect > ,
21
+ get : fn ( & World , UntypedAssetId ) -> Option < & dyn Reflect > ,
22
22
// SAFETY:
23
23
// - may only be called with an [`UnsafeWorldCell`] which can be used to access the corresponding `Assets<T>` resource mutably
24
24
// - may only be used to access **at most one** access at once
25
- get_unchecked_mut : unsafe fn ( UnsafeWorldCell < ' _ > , UntypedHandle ) -> Option < & mut dyn Reflect > ,
25
+ get_unchecked_mut : unsafe fn ( UnsafeWorldCell < ' _ > , UntypedAssetId ) -> Option < & mut dyn Reflect > ,
26
26
add : fn ( & mut World , & dyn PartialReflect ) -> UntypedHandle ,
27
- insert : fn ( & mut World , UntypedHandle , & dyn PartialReflect ) ,
27
+ insert : fn ( & mut World , UntypedAssetId , & dyn PartialReflect ) ,
28
28
len : fn ( & World ) -> usize ,
29
29
ids : for <' w > fn ( & ' w World ) -> Box < dyn Iterator < Item = UntypedAssetId > + ' w > ,
30
- remove : fn ( & mut World , UntypedHandle ) -> Option < Box < dyn Reflect > > ,
30
+ remove : fn ( & mut World , UntypedAssetId ) -> Option < Box < dyn Reflect > > ,
31
31
}
32
32
33
33
impl ReflectAsset {
@@ -42,23 +42,27 @@ impl ReflectAsset {
42
42
}
43
43
44
44
/// Equivalent of [`Assets::get`]
45
- pub fn get < ' w > ( & self , world : & ' w World , handle : UntypedHandle ) -> Option < & ' w dyn Reflect > {
46
- ( self . get ) ( world, handle)
45
+ pub fn get < ' w > (
46
+ & self ,
47
+ world : & ' w World ,
48
+ asset_id : impl Into < UntypedAssetId > ,
49
+ ) -> Option < & ' w dyn Reflect > {
50
+ ( self . get ) ( world, asset_id. into ( ) )
47
51
}
48
52
49
53
/// Equivalent of [`Assets::get_mut`]
50
54
pub fn get_mut < ' w > (
51
55
& self ,
52
56
world : & ' w mut World ,
53
- handle : UntypedHandle ,
57
+ asset_id : impl Into < UntypedAssetId > ,
54
58
) -> Option < & ' w mut dyn Reflect > {
55
59
// SAFETY: unique world access
56
60
#[ expect(
57
61
unsafe_code,
58
62
reason = "Use of unsafe `Self::get_unchecked_mut()` function."
59
63
) ]
60
64
unsafe {
61
- ( self . get_unchecked_mut ) ( world. as_unsafe_world_cell ( ) , handle )
65
+ ( self . get_unchecked_mut ) ( world. as_unsafe_world_cell ( ) , asset_id . into ( ) )
62
66
}
63
67
}
64
68
@@ -96,24 +100,33 @@ impl ReflectAsset {
96
100
pub unsafe fn get_unchecked_mut < ' w > (
97
101
& self ,
98
102
world : UnsafeWorldCell < ' w > ,
99
- handle : UntypedHandle ,
103
+ asset_id : impl Into < UntypedAssetId > ,
100
104
) -> Option < & ' w mut dyn Reflect > {
101
105
// SAFETY: requirements are deferred to the caller
102
- unsafe { ( self . get_unchecked_mut ) ( world, handle ) }
106
+ unsafe { ( self . get_unchecked_mut ) ( world, asset_id . into ( ) ) }
103
107
}
104
108
105
109
/// Equivalent of [`Assets::add`]
106
110
pub fn add ( & self , world : & mut World , value : & dyn PartialReflect ) -> UntypedHandle {
107
111
( self . add ) ( world, value)
108
112
}
109
113
/// Equivalent of [`Assets::insert`]
110
- pub fn insert ( & self , world : & mut World , handle : UntypedHandle , value : & dyn PartialReflect ) {
111
- ( self . insert ) ( world, handle, value) ;
114
+ pub fn insert (
115
+ & self ,
116
+ world : & mut World ,
117
+ asset_id : impl Into < UntypedAssetId > ,
118
+ value : & dyn PartialReflect ,
119
+ ) {
120
+ ( self . insert ) ( world, asset_id. into ( ) , value) ;
112
121
}
113
122
114
123
/// Equivalent of [`Assets::remove`]
115
- pub fn remove ( & self , world : & mut World , handle : UntypedHandle ) -> Option < Box < dyn Reflect > > {
116
- ( self . remove ) ( world, handle)
124
+ pub fn remove (
125
+ & self ,
126
+ world : & mut World ,
127
+ asset_id : impl Into < UntypedAssetId > ,
128
+ ) -> Option < Box < dyn Reflect > > {
129
+ ( self . remove ) ( world, asset_id. into ( ) )
117
130
}
118
131
119
132
/// Equivalent of [`Assets::len`]
@@ -137,17 +150,17 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
137
150
ReflectAsset {
138
151
handle_type_id : TypeId :: of :: < Handle < A > > ( ) ,
139
152
assets_resource_type_id : TypeId :: of :: < Assets < A > > ( ) ,
140
- get : |world, handle | {
153
+ get : |world, asset_id | {
141
154
let assets = world. resource :: < Assets < A > > ( ) ;
142
- let asset = assets. get ( & handle . typed_debug_checked ( ) ) ;
155
+ let asset = assets. get ( asset_id . typed_debug_checked ( ) ) ;
143
156
asset. map ( |asset| asset as & dyn Reflect )
144
157
} ,
145
- get_unchecked_mut : |world, handle | {
158
+ get_unchecked_mut : |world, asset_id | {
146
159
// SAFETY: `get_unchecked_mut` must be called with `UnsafeWorldCell` having access to `Assets<A>`,
147
160
// and must ensure to only have at most one reference to it live at all times.
148
161
#[ expect( unsafe_code, reason = "Uses `UnsafeWorldCell::get_resource_mut()`." ) ]
149
162
let assets = unsafe { world. get_resource_mut :: < Assets < A > > ( ) . unwrap ( ) . into_inner ( ) } ;
150
- let asset = assets. get_mut ( & handle . typed_debug_checked ( ) ) ;
163
+ let asset = assets. get_mut ( asset_id . typed_debug_checked ( ) ) ;
151
164
asset. map ( |asset| asset as & mut dyn Reflect )
152
165
} ,
153
166
add : |world, value| {
@@ -156,11 +169,11 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
156
169
. expect ( "could not call `FromReflect::from_reflect` in `ReflectAsset::add`" ) ;
157
170
assets. add ( value) . untyped ( )
158
171
} ,
159
- insert : |world, handle , value| {
172
+ insert : |world, asset_id , value| {
160
173
let mut assets = world. resource_mut :: < Assets < A > > ( ) ;
161
174
let value: A = FromReflect :: from_reflect ( value)
162
175
. expect ( "could not call `FromReflect::from_reflect` in `ReflectAsset::set`" ) ;
163
- assets. insert ( & handle . typed_debug_checked ( ) , value) ;
176
+ assets. insert ( asset_id . typed_debug_checked ( ) , value) ;
164
177
} ,
165
178
len : |world| {
166
179
let assets = world. resource :: < Assets < A > > ( ) ;
@@ -170,9 +183,9 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
170
183
let assets = world. resource :: < Assets < A > > ( ) ;
171
184
Box :: new ( assets. ids ( ) . map ( AssetId :: untyped) )
172
185
} ,
173
- remove : |world, handle | {
186
+ remove : |world, asset_id | {
174
187
let mut assets = world. resource_mut :: < Assets < A > > ( ) ;
175
- let value = assets. remove ( & handle . typed_debug_checked ( ) ) ;
188
+ let value = assets. remove ( asset_id . typed_debug_checked ( ) ) ;
176
189
value. map ( |value| Box :: new ( value) as Box < dyn Reflect > )
177
190
} ,
178
191
}
@@ -247,7 +260,7 @@ mod tests {
247
260
use alloc:: { string:: String , vec:: Vec } ;
248
261
use core:: any:: TypeId ;
249
262
250
- use crate :: { Asset , AssetApp , AssetPlugin , ReflectAsset , UntypedHandle } ;
263
+ use crate :: { Asset , AssetApp , AssetPlugin , ReflectAsset } ;
251
264
use bevy_app:: App ;
252
265
use bevy_ecs:: reflect:: AppTypeRegistry ;
253
266
use bevy_reflect:: Reflect ;
@@ -281,7 +294,7 @@ mod tests {
281
294
let handle = reflect_asset. add ( app. world_mut ( ) , & value) ;
282
295
// struct is a reserved keyword, so we can't use it here
283
296
let strukt = reflect_asset
284
- . get_mut ( app. world_mut ( ) , handle)
297
+ . get_mut ( app. world_mut ( ) , & handle)
285
298
. unwrap ( )
286
299
. reflect_mut ( )
287
300
. as_struct ( )
@@ -294,16 +307,12 @@ mod tests {
294
307
assert_eq ! ( reflect_asset. len( app. world( ) ) , 1 ) ;
295
308
let ids: Vec < _ > = reflect_asset. ids ( app. world ( ) ) . collect ( ) ;
296
309
assert_eq ! ( ids. len( ) , 1 ) ;
310
+ let id = ids[ 0 ] ;
297
311
298
- let fetched_handle = UntypedHandle :: Weak ( ids[ 0 ] ) ;
299
- let asset = reflect_asset
300
- . get ( app. world ( ) , fetched_handle. clone_weak ( ) )
301
- . unwrap ( ) ;
312
+ let asset = reflect_asset. get ( app. world ( ) , id) . unwrap ( ) ;
302
313
assert_eq ! ( asset. downcast_ref:: <AssetType >( ) . unwrap( ) . field, "edited" ) ;
303
314
304
- reflect_asset
305
- . remove ( app. world_mut ( ) , fetched_handle)
306
- . unwrap ( ) ;
315
+ reflect_asset. remove ( app. world_mut ( ) , id) . unwrap ( ) ;
307
316
assert_eq ! ( reflect_asset. len( app. world( ) ) , 0 ) ;
308
317
}
309
318
}
0 commit comments