Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change HandleUntyped::typed to not panic #2536

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ impl AssetServer {
/// `"assets"`.
#[must_use = "not using the returned strong handle may result in the unexpected release of the asset"]
pub fn load<'a, T: Asset, P: Into<AssetPath<'a>>>(&self, path: P) -> Handle<T> {
self.load_untyped(path).typed()
self.load_untyped(path).typed().expect(
"Failed to convert untyped handle from asset to typed, this should never happen. Report a bug!",
)
}

async fn load_async(
Expand Down
11 changes: 7 additions & 4 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,23 +275,26 @@ impl HandleUntyped {
matches!(self.handle_type, HandleType::Strong(_))
}

pub fn typed<T: Asset>(mut self) -> Handle<T> {
pub fn typed<T: Asset>(mut self) -> Result<Handle<T>, Self> {
if let HandleId::Id(type_uuid, _) = self.id {
if T::TYPE_UUID != type_uuid {
panic!("Attempted to convert handle to invalid type.");
return Err(self);
}
}

let handle_type = match &self.handle_type {
HandleType::Strong(sender) => HandleType::Strong(sender.clone()),
HandleType::Weak => HandleType::Weak,
};

// ensure we don't send the RefChange event when "self" is dropped
self.handle_type = HandleType::Weak;
Handle {

Ok(Handle {
handle_type,
id: self.id,
marker: PhantomData::default(),
}
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Default for PbrBundle {
fn default() -> Self {
Self {
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new(
PBR_PIPELINE_HANDLE.typed(),
PBR_PIPELINE_HANDLE.typed().unwrap(),
)]),
mesh: Default::default(),
visible: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_pbr/src/render_graph/pbr_pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bevy_render::{
texture::TextureFormat,
};

// FIXME: Handle is always converted to typed, see discussion in PR #2536
pub const PBR_PIPELINE_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(PipelineDescriptor::TYPE_UUID, 13148362314012771389);

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/wireframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use bevy_utils::HashSet;

mod pipeline;

// FIXME: Handle is always converted to typed, see discussion in PR #2536
pub const WIREFRAME_PIPELINE_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(PipelineDescriptor::TYPE_UUID, 0x137c75ab7e9ad7f5);

Expand Down Expand Up @@ -84,7 +85,7 @@ pub fn draw_wireframes_system(
};

let mut render_pipeline = RenderPipeline::specialized(
WIREFRAME_PIPELINE_HANDLE.typed(),
WIREFRAME_PIPELINE_HANDLE.typed().unwrap(),
PipelineSpecialization {
sample_count: msaa.samples,
strip_index_format: None,
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_sprite/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub struct SpriteBundle {
impl Default for SpriteBundle {
fn default() -> Self {
Self {
mesh: QUAD_HANDLE.typed(),
mesh: QUAD_HANDLE.typed().unwrap(),
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new(
SPRITE_PIPELINE_HANDLE.typed(),
SPRITE_PIPELINE_HANDLE.typed().unwrap(),
)]),
visible: Visible {
is_transparent: true,
Expand Down Expand Up @@ -68,14 +68,14 @@ impl Default for SpriteSheetBundle {
fn default() -> Self {
Self {
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new(
SPRITE_SHEET_PIPELINE_HANDLE.typed(),
SPRITE_SHEET_PIPELINE_HANDLE.typed().unwrap(),
)]),
visible: Visible {
is_transparent: true,
..Default::default()
},
main_pass: MainPass,
mesh: QUAD_HANDLE.typed(),
mesh: QUAD_HANDLE.typed().unwrap(),
draw: Default::default(),
sprite: Default::default(),
texture_atlas: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_sprite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl Default for SpriteSettings {
#[derive(Default)]
pub struct SpritePlugin;

// FIXME: Handle is always converted to typed, see discussion in PR #2536
pub const QUAD_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(Mesh::TYPE_UUID, 14240461981130137526);

Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ use bevy_render::{
texture::TextureFormat,
};

// FIXME: Handle is always converted to typed, see discussion in PR #2536
pub const SPRITE_PIPELINE_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(PipelineDescriptor::TYPE_UUID, 2785347840338765446);

// FIXME: Handle is always converted to typed, see discussion in PR #2536
pub const SPRITE_SHEET_PIPELINE_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(PipelineDescriptor::TYPE_UUID, 9016885805180281612);

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_text/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'a> Drawable for DrawableText<'a> {
fn draw(&mut self, draw: &mut Draw, context: &mut DrawContext) -> Result<(), DrawError> {
context.set_pipeline(
draw,
&bevy_sprite::SPRITE_SHEET_PIPELINE_HANDLE.typed(),
&bevy_sprite::SPRITE_SHEET_PIPELINE_HANDLE.typed().unwrap(),
&PipelineSpecialization {
sample_count: self.msaa.samples,
vertex_buffer_layout: self.font_quad_vertex_layout.clone(),
Expand All @@ -40,7 +40,7 @@ impl<'a> Drawable for DrawableText<'a> {

if let Some(RenderResourceId::Buffer(vertex_attribute_buffer_id)) = render_resource_context
.get_asset_resource(
&bevy_sprite::QUAD_HANDLE.typed::<Mesh>(),
&bevy_sprite::QUAD_HANDLE.typed::<Mesh>().unwrap(),
mesh::VERTEX_ATTRIBUTE_BUFFER_ID,
)
{
Expand All @@ -52,7 +52,7 @@ impl<'a> Drawable for DrawableText<'a> {
let mut indices = 0..0;
if let Some(RenderResourceId::Buffer(quad_index_buffer)) = render_resource_context
.get_asset_resource(
&bevy_sprite::QUAD_HANDLE.typed::<Mesh>(),
&bevy_sprite::QUAD_HANDLE.typed::<Mesh>().unwrap(),
mesh::INDEX_BUFFER_ASSET_INDEX,
)
{
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_ui/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ pub struct NodeBundle {
impl Default for NodeBundle {
fn default() -> Self {
NodeBundle {
mesh: QUAD_HANDLE.typed(),
mesh: QUAD_HANDLE.typed().unwrap(),
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new(
UI_PIPELINE_HANDLE.typed(),
UI_PIPELINE_HANDLE.typed().unwrap(),
)]),
visible: Visible {
is_transparent: true,
Expand Down Expand Up @@ -69,9 +69,9 @@ pub struct ImageBundle {
impl Default for ImageBundle {
fn default() -> Self {
ImageBundle {
mesh: QUAD_HANDLE.typed(),
mesh: QUAD_HANDLE.typed().unwrap(),
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new(
UI_PIPELINE_HANDLE.typed(),
UI_PIPELINE_HANDLE.typed().unwrap(),
)]),
node: Default::default(),
image: Default::default(),
Expand Down Expand Up @@ -143,9 +143,9 @@ impl Default for ButtonBundle {
fn default() -> Self {
ButtonBundle {
button: Button,
mesh: QUAD_HANDLE.typed(),
mesh: QUAD_HANDLE.typed().unwrap(),
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new(
UI_PIPELINE_HANDLE.typed(),
UI_PIPELINE_HANDLE.typed().unwrap(),
)]),
interaction: Default::default(),
focus_policy: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use bevy_render::{
texture::TextureFormat,
};

// FIXME: Handle is always converted to typed, see discussion in PR #2536
pub const UI_PIPELINE_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(PipelineDescriptor::TYPE_UUID, 3234320022263993878);

Expand Down
11 changes: 8 additions & 3 deletions examples/2d/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ enum AppState {

#[derive(Default)]
struct RpgSpriteHandles {
handles: Vec<HandleUntyped>,
handles: Vec<Handle<Texture>>,
}

fn load_textures(mut rpg_sprite_handles: ResMut<RpgSpriteHandles>, asset_server: Res<AssetServer>) {
rpg_sprite_handles.handles = asset_server.load_folder("textures/rpg").unwrap();
if let Ok(handles) = asset_server.load_folder("textures/rpg") {
rpg_sprite_handles.handles = handles
.into_iter()
.flat_map(|handle| handle.typed::<Texture>())
.collect();
}
}

fn check_textures(
Expand All @@ -51,7 +56,7 @@ fn setup(
let mut texture_atlas_builder = TextureAtlasBuilder::default();
for handle in rpg_sprite_handles.handles.iter() {
let texture = textures.get(handle).unwrap();
texture_atlas_builder.add_texture(handle.clone_weak().typed::<Texture>(), texture);
texture_atlas_builder.add_texture(handle.clone_weak(), texture);
}

let texture_atlas = texture_atlas_builder.finish(&mut textures).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion examples/3d/render_to_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use bevy::{

pub struct FirstPass;

// FIXME: Handle is always converted to typed, see discussion in PR #2536
pub const RENDER_TEXTURE_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(Texture::TYPE_UUID, 13378939762009864029);

Expand Down Expand Up @@ -186,7 +187,7 @@ fn setup(

commands.spawn_bundle(first_pass_camera);

let texture_handle = RENDER_TEXTURE_HANDLE.typed();
let texture_handle = RENDER_TEXTURE_HANDLE.typed().unwrap();

let cube_size = 4.0;
let cube_handle = meshes.add(Mesh::from(shape::Box::new(cube_size, cube_size, cube_size)));
Expand Down