From 85743ce49eda885854252bab59d57b0ddc9fe8a7 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 4 Jan 2023 23:40:43 +0000 Subject: [PATCH] asset: make HandleUntyped::id private (#7076) # Objective It is currently possible to break reference counting for assets by creating a strong `HandleUntyped` and then modifying the `id` field before dropping the handle. This should not be allowed. ## Solution Change the `id` field visibility to private and add a getter instead. The same change was previously done for `Handle` in #6176, but `HandleUntyped` was forgotten. --- ## Migration Guide - Instead of directly accessing the ID of a `HandleUntyped` as `handle.id`, use the new getter `handle.id()`. --- crates/bevy_asset/src/handle.rs | 9 +++++++-- examples/2d/texture_atlas.rs | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/bevy_asset/src/handle.rs b/crates/bevy_asset/src/handle.rs index 677f1e053e..dfa4bd2d35 100644 --- a/crates/bevy_asset/src/handle.rs +++ b/crates/bevy_asset/src/handle.rs @@ -322,8 +322,7 @@ impl Clone for Handle { /// To convert back to a typed handle, use the [typed](HandleUntyped::typed) method. #[derive(Debug)] pub struct HandleUntyped { - /// An unique identifier to an Asset. - pub id: HandleId, + id: HandleId, handle_type: HandleType, } @@ -352,6 +351,12 @@ impl HandleUntyped { } } + /// The ID of the asset. + #[inline] + pub fn id(&self) -> HandleId { + self.id + } + /// Creates a weak copy of this handle. #[must_use] pub fn clone_weak(&self) -> Self { diff --git a/examples/2d/texture_atlas.rs b/examples/2d/texture_atlas.rs index be41577cc5..14d9119ebe 100644 --- a/examples/2d/texture_atlas.rs +++ b/examples/2d/texture_atlas.rs @@ -34,8 +34,8 @@ fn check_textures( rpg_sprite_handles: ResMut, asset_server: Res, ) { - if let LoadState::Loaded = - asset_server.get_group_load_state(rpg_sprite_handles.handles.iter().map(|handle| handle.id)) + if let LoadState::Loaded = asset_server + .get_group_load_state(rpg_sprite_handles.handles.iter().map(|handle| handle.id())) { state.set(AppState::Finished).unwrap(); }