Skip to content

Commit

Permalink
make Handle::<T> field id private, and replace with a getter (bevyeng…
Browse files Browse the repository at this point in the history
…ine#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 bevyengine#6171. Pinging @zicklag 

---

## Migration Guide

- If you were accessing the value `handle.id`, you can now do so with `handle.id()`
  • Loading branch information
mockersf authored and ItsDoot committed Feb 1, 2023
1 parent 8105434 commit 53009d4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
6 changes: 3 additions & 3 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
9 changes: 7 additions & 2 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
}
Expand All @@ -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(),
});
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_text/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 53009d4

Please sign in to comment.