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.
This commit is contained in:
parent
71f8b4a92f
commit
306c1ac617
@ -160,9 +160,15 @@ impl<T: Asset> Handle<T> {
|
||||
}
|
||||
|
||||
/// Recasts this handle as a weak handle of an Asset `U`.
|
||||
pub fn as_weak<U: Asset>(&self) -> Handle<U> {
|
||||
pub fn cast_weak<U: Asset>(&self) -> Handle<U> {
|
||||
let id = if let HandleId::Id(_, id) = self.id {
|
||||
HandleId::Id(U::TYPE_UUID, id)
|
||||
} else {
|
||||
self.id
|
||||
};
|
||||
|
||||
Handle {
|
||||
id: self.id,
|
||||
id,
|
||||
handle_type: HandleType::Weak,
|
||||
marker: PhantomData,
|
||||
}
|
||||
|
@ -106,7 +106,7 @@ impl GlyphBrush {
|
||||
let section_data = sections_data[sg.section_index];
|
||||
if let Some(outlined_glyph) = section_data.1.font.outline_glyph(glyph) {
|
||||
let bounds = outlined_glyph.px_bounds();
|
||||
let handle_font_atlas: Handle<FontAtlasSet> = section_data.0.as_weak();
|
||||
let handle_font_atlas: Handle<FontAtlasSet> = section_data.0.cast_weak();
|
||||
let font_atlas_set = font_atlas_set_storage
|
||||
.get_or_insert_with(handle_font_atlas, FontAtlasSet::default);
|
||||
|
||||
|
@ -37,7 +37,7 @@ fn atlas_render_system(
|
||||
font_atlas_sets: Res<Assets<FontAtlasSet>>,
|
||||
texture_atlases: Res<Assets<TextureAtlas>>,
|
||||
) {
|
||||
if let Some(set) = font_atlas_sets.get(&state.handle.as_weak::<FontAtlasSet>()) {
|
||||
if let Some(set) = font_atlas_sets.get(&state.handle.cast_weak::<FontAtlasSet>()) {
|
||||
if let Some((_size, font_atlas)) = set.iter().next() {
|
||||
let x_offset = state.atlas_count as f32;
|
||||
if state.atlas_count == font_atlas.len() as u32 {
|
||||
|
Loading…
Reference in New Issue
Block a user