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<T>` 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()`.
This commit is contained in:
Matthias Schiffer 2023-01-04 23:40:43 +00:00
parent 4fff0ce837
commit 85743ce49e
2 changed files with 9 additions and 4 deletions

View file

@ -322,8 +322,7 @@ impl<T: Asset> Clone for Handle<T> {
/// 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 {

View file

@ -34,8 +34,8 @@ fn check_textures(
rpg_sprite_handles: ResMut<RpgSpriteHandles>,
asset_server: Res<AssetServer>,
) {
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();
}