make Handle::<T> field id private, and replace with a getter (#6176)

# Objective

- Field `id` of `Handle<T>` is public: https://docs.rs/bevy/latest/bevy/asset/struct.Handle.html#structfield.id
- Changing the value of this field doesn't make sense as it could mean changing the previous handle without dropping it, breaking asset cleanup detection for the old handle and the new one

## Solution

- Make the field private, and add a public getter


Opened after discussion in #6171. Pinging @zicklag 

---

## Migration Guide

- If you were accessing the value `handle.id`, you can now do so with `handle.id()`
This commit is contained in:
François 2022-10-06 13:33:30 +00:00
parent f2106bb3ce
commit f00212fd48
5 changed files with 14 additions and 9 deletions

View file

@ -33,21 +33,21 @@ impl<T: Asset> Debug for AssetEvent<T> {
"AssetEvent<{}>::Created",
std::any::type_name::<T>()
))
.field("handle", &handle.id)
.field("handle", &handle.id())
.finish(),
AssetEvent::Modified { handle } => f
.debug_struct(&format!(
"AssetEvent<{}>::Modified",
std::any::type_name::<T>()
))
.field("handle", &handle.id)
.field("handle", &handle.id())
.finish(),
AssetEvent::Removed { handle } => f
.debug_struct(&format!(
"AssetEvent<{}>::Removed",
std::any::type_name::<T>()
))
.field("handle", &handle.id)
.field("handle", &handle.id())
.finish(),
}
}

View file

@ -105,8 +105,7 @@ pub struct Handle<T>
where
T: Asset,
{
/// The ID of the asset as contained within its respective [`Assets`] collection
pub id: HandleId,
id: HandleId,
#[reflect(ignore)]
handle_type: HandleType,
#[reflect(ignore)]
@ -151,6 +150,12 @@ impl<T: Asset> Handle<T> {
}
}
/// The ID of the asset as contained within its respective [`Assets`] collection.
#[inline]
pub fn id(&self) -> HandleId {
self.id
}
/// Recasts this handle as a weak handle of an Asset `U`.
pub fn as_weak<U: Asset>(&self) -> Handle<U> {
Handle {

View file

@ -261,7 +261,7 @@ pub fn extract_sprites(
custom_size: sprite.custom_size,
flip_x: sprite.flip_x,
flip_y: sprite.flip_y,
image_handle_id: handle.id,
image_handle_id: handle.id(),
anchor: sprite.anchor.as_vec(),
});
}
@ -281,7 +281,7 @@ pub fn extract_sprites(
custom_size: atlas_sprite.custom_size,
flip_x: atlas_sprite.flip_x,
flip_y: atlas_sprite.flip_y,
image_handle_id: texture_atlas.texture.id,
image_handle_id: texture_atlas.texture.id(),
anchor: atlas_sprite.anchor.as_vec(),
});
}

View file

@ -34,7 +34,7 @@ impl TextPipeline {
let brush = &mut self.brush;
*self
.map_font_id
.entry(handle.id)
.entry(handle.id())
.or_insert_with(|| brush.add_font(handle.clone(), font.font.clone()))
}

View file

@ -134,7 +134,7 @@ pub fn extract_text2d_sprite(
color,
rect,
custom_size: None,
image_handle_id: handle.id,
image_handle_id: handle.id(),
flip_x: false,
flip_y: false,
anchor: Anchor::Center.as_vec(),