Skip to content

Commit 306c1ac

Browse files
committed
Rename Handle::as_weak() to cast_weak() (#5321)
# Objective Following discussion on #3536 and #3522, `Handle::as_weak()` takes a type `U`, reinterpreting the handle as of another asset type while keeping the same ID. This is mainly used today in font atlas code. This PR does two things: - Rename the method to `cast_weak()` to make its intent more clear - Actually change the type uuid in the handle if it's not an asset path variant. ## Migration Guide - Rename `Handle::as_weak` uses to `Handle::cast_weak` The method now properly sets the associated type uuid if the handle is a direct reference (e.g. not a reference to an `AssetPath`), so adjust you code accordingly if you relied on the previous behavior.
1 parent 71f8b4a commit 306c1ac

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

crates/bevy_asset/src/handle.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,15 @@ impl<T: Asset> Handle<T> {
160160
}
161161

162162
/// Recasts this handle as a weak handle of an Asset `U`.
163-
pub fn as_weak<U: Asset>(&self) -> Handle<U> {
163+
pub fn cast_weak<U: Asset>(&self) -> Handle<U> {
164+
let id = if let HandleId::Id(_, id) = self.id {
165+
HandleId::Id(U::TYPE_UUID, id)
166+
} else {
167+
self.id
168+
};
169+
164170
Handle {
165-
id: self.id,
171+
id,
166172
handle_type: HandleType::Weak,
167173
marker: PhantomData,
168174
}

crates/bevy_text/src/glyph_brush.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl GlyphBrush {
106106
let section_data = sections_data[sg.section_index];
107107
if let Some(outlined_glyph) = section_data.1.font.outline_glyph(glyph) {
108108
let bounds = outlined_glyph.px_bounds();
109-
let handle_font_atlas: Handle<FontAtlasSet> = section_data.0.as_weak();
109+
let handle_font_atlas: Handle<FontAtlasSet> = section_data.0.cast_weak();
110110
let font_atlas_set = font_atlas_set_storage
111111
.get_or_insert_with(handle_font_atlas, FontAtlasSet::default);
112112

examples/ui/font_atlas_debug.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn atlas_render_system(
3737
font_atlas_sets: Res<Assets<FontAtlasSet>>,
3838
texture_atlases: Res<Assets<TextureAtlas>>,
3939
) {
40-
if let Some(set) = font_atlas_sets.get(&state.handle.as_weak::<FontAtlasSet>()) {
40+
if let Some(set) = font_atlas_sets.get(&state.handle.cast_weak::<FontAtlasSet>()) {
4141
if let Some((_size, font_atlas)) = set.iter().next() {
4242
let x_offset = state.atlas_count as f32;
4343
if state.atlas_count == font_atlas.len() as u32 {

0 commit comments

Comments
 (0)