Skip to content

Commit a7ea451

Browse files
committed
Replace UntypedHandle from ReflectAsset with impl Into<UntypedAssetId>.
1 parent 2961f44 commit a7ea451

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

crates/bevy_asset/src/reflect.rs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ pub struct ReflectAsset {
1818
handle_type_id: TypeId,
1919
assets_resource_type_id: TypeId,
2020

21-
get: fn(&World, UntypedHandle) -> Option<&dyn Reflect>,
21+
get: fn(&World, UntypedAssetId) -> Option<&dyn Reflect>,
2222
// SAFETY:
2323
// - may only be called with an [`UnsafeWorldCell`] which can be used to access the corresponding `Assets<T>` resource mutably
2424
// - 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>,
2626
add: fn(&mut World, &dyn PartialReflect) -> UntypedHandle,
27-
insert: fn(&mut World, UntypedHandle, &dyn PartialReflect),
27+
insert: fn(&mut World, UntypedAssetId, &dyn PartialReflect),
2828
len: fn(&World) -> usize,
2929
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>>,
3131
}
3232

3333
impl ReflectAsset {
@@ -42,23 +42,27 @@ impl ReflectAsset {
4242
}
4343

4444
/// 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())
4751
}
4852

4953
/// Equivalent of [`Assets::get_mut`]
5054
pub fn get_mut<'w>(
5155
&self,
5256
world: &'w mut World,
53-
handle: UntypedHandle,
57+
asset_id: impl Into<UntypedAssetId>,
5458
) -> Option<&'w mut dyn Reflect> {
5559
// SAFETY: unique world access
5660
#[expect(
5761
unsafe_code,
5862
reason = "Use of unsafe `Self::get_unchecked_mut()` function."
5963
)]
6064
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())
6266
}
6367
}
6468

@@ -96,24 +100,33 @@ impl ReflectAsset {
96100
pub unsafe fn get_unchecked_mut<'w>(
97101
&self,
98102
world: UnsafeWorldCell<'w>,
99-
handle: UntypedHandle,
103+
asset_id: impl Into<UntypedAssetId>,
100104
) -> Option<&'w mut dyn Reflect> {
101105
// 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()) }
103107
}
104108

105109
/// Equivalent of [`Assets::add`]
106110
pub fn add(&self, world: &mut World, value: &dyn PartialReflect) -> UntypedHandle {
107111
(self.add)(world, value)
108112
}
109113
/// 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);
112121
}
113122

114123
/// 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())
117130
}
118131

119132
/// Equivalent of [`Assets::len`]
@@ -137,17 +150,17 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
137150
ReflectAsset {
138151
handle_type_id: TypeId::of::<Handle<A>>(),
139152
assets_resource_type_id: TypeId::of::<Assets<A>>(),
140-
get: |world, handle| {
153+
get: |world, asset_id| {
141154
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());
143156
asset.map(|asset| asset as &dyn Reflect)
144157
},
145-
get_unchecked_mut: |world, handle| {
158+
get_unchecked_mut: |world, asset_id| {
146159
// SAFETY: `get_unchecked_mut` must be called with `UnsafeWorldCell` having access to `Assets<A>`,
147160
// and must ensure to only have at most one reference to it live at all times.
148161
#[expect(unsafe_code, reason = "Uses `UnsafeWorldCell::get_resource_mut()`.")]
149162
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());
151164
asset.map(|asset| asset as &mut dyn Reflect)
152165
},
153166
add: |world, value| {
@@ -156,11 +169,11 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
156169
.expect("could not call `FromReflect::from_reflect` in `ReflectAsset::add`");
157170
assets.add(value).untyped()
158171
},
159-
insert: |world, handle, value| {
172+
insert: |world, asset_id, value| {
160173
let mut assets = world.resource_mut::<Assets<A>>();
161174
let value: A = FromReflect::from_reflect(value)
162175
.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);
164177
},
165178
len: |world| {
166179
let assets = world.resource::<Assets<A>>();
@@ -170,9 +183,9 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
170183
let assets = world.resource::<Assets<A>>();
171184
Box::new(assets.ids().map(AssetId::untyped))
172185
},
173-
remove: |world, handle| {
186+
remove: |world, asset_id| {
174187
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());
176189
value.map(|value| Box::new(value) as Box<dyn Reflect>)
177190
},
178191
}
@@ -247,7 +260,7 @@ mod tests {
247260
use alloc::{string::String, vec::Vec};
248261
use core::any::TypeId;
249262

250-
use crate::{Asset, AssetApp, AssetPlugin, ReflectAsset, UntypedHandle};
263+
use crate::{Asset, AssetApp, AssetPlugin, ReflectAsset};
251264
use bevy_app::App;
252265
use bevy_ecs::reflect::AppTypeRegistry;
253266
use bevy_reflect::Reflect;
@@ -281,7 +294,7 @@ mod tests {
281294
let handle = reflect_asset.add(app.world_mut(), &value);
282295
// struct is a reserved keyword, so we can't use it here
283296
let strukt = reflect_asset
284-
.get_mut(app.world_mut(), handle)
297+
.get_mut(app.world_mut(), &handle)
285298
.unwrap()
286299
.reflect_mut()
287300
.as_struct()
@@ -294,16 +307,12 @@ mod tests {
294307
assert_eq!(reflect_asset.len(app.world()), 1);
295308
let ids: Vec<_> = reflect_asset.ids(app.world()).collect();
296309
assert_eq!(ids.len(), 1);
310+
let id = ids[0];
297311

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();
302313
assert_eq!(asset.downcast_ref::<AssetType>().unwrap().field, "edited");
303314

304-
reflect_asset
305-
.remove(app.world_mut(), fetched_handle)
306-
.unwrap();
315+
reflect_asset.remove(app.world_mut(), id).unwrap();
307316
assert_eq!(reflect_asset.len(app.world()), 0);
308317
}
309318
}

0 commit comments

Comments
 (0)