Skip to content

Commit

Permalink
[core] Use expect instead of "if let else panic". (gfx-rs#5513)
Browse files Browse the repository at this point in the history
Use `Option::expect` to check the result from `Arc::into_inner`,
rather than `if let Some ... else panic!`.
  • Loading branch information
jimblandy authored Apr 10, 2024
1 parent 82dead0 commit f8deb03
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
8 changes: 3 additions & 5 deletions wgpu-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,9 @@ impl<A: HalApi> CommandBuffer<A> {
}

pub(crate) fn from_arc_into_baked(self: Arc<Self>) -> BakedCommands<A> {
if let Some(mut command_buffer) = Arc::into_inner(self) {
command_buffer.extract_baked_commands()
} else {
panic!("CommandBuffer cannot be destroyed because is still in use");
}
let mut command_buffer = Arc::into_inner(self)
.expect("CommandBuffer cannot be destroyed because is still in use");
command_buffer.extract_baked_commands()
}
}

Expand Down
11 changes: 4 additions & 7 deletions wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,13 +1210,10 @@ impl Global {
));
}
if !cmdbuf.is_finished() {
if let Some(cmdbuf) = Arc::into_inner(cmdbuf) {
device.destroy_command_buffer(cmdbuf);
} else {
panic!(
"Command buffer cannot be destroyed because is still in use"
);
}
let cmdbuf = Arc::into_inner(cmdbuf).expect(
"Command buffer cannot be destroyed because is still in use",
);
device.destroy_command_buffer(cmdbuf);
continue;
}

Expand Down
8 changes: 3 additions & 5 deletions wgpu-core/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,9 @@ impl Drop for Global {
// destroy surfaces
for element in surfaces_locked.map.drain(..) {
if let Element::Occupied(arc_surface, _) = element {
if let Some(surface) = Arc::into_inner(arc_surface) {
self.instance.destroy_surface(surface);
} else {
panic!("Surface cannot be destroyed because is still in use");
}
let surface = Arc::into_inner(arc_surface)
.expect("Surface cannot be destroyed because is still in use");
self.instance.destroy_surface(surface);
}
}
}
Expand Down
27 changes: 12 additions & 15 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,22 +667,19 @@ impl Global {
}

let surface = self.surfaces.unregister(id);
if let Some(surface) = Arc::into_inner(surface.unwrap()) {
if let Some(present) = surface.presentation.lock().take() {
#[cfg(vulkan)]
unconfigure::<hal::api::Vulkan>(self, &surface.raw, &present);
#[cfg(metal)]
unconfigure::<hal::api::Metal>(self, &surface.raw, &present);
#[cfg(dx12)]
unconfigure::<hal::api::Dx12>(self, &surface.raw, &present);
#[cfg(gles)]
unconfigure::<hal::api::Gles>(self, &surface.raw, &present);
}

self.instance.destroy_surface(surface);
} else {
panic!("Surface cannot be destroyed because is still in use");
let surface = Arc::into_inner(surface.unwrap())
.expect("Surface cannot be destroyed because is still in use");
if let Some(present) = surface.presentation.lock().take() {
#[cfg(vulkan)]
unconfigure::<hal::api::Vulkan>(self, &surface.raw, &present);
#[cfg(metal)]
unconfigure::<hal::api::Metal>(self, &surface.raw, &present);
#[cfg(dx12)]
unconfigure::<hal::api::Dx12>(self, &surface.raw, &present);
#[cfg(gles)]
unconfigure::<hal::api::Gles>(self, &surface.raw, &present);
}
self.instance.destroy_surface(surface);
}

fn enumerate<A: HalApi>(
Expand Down

0 comments on commit f8deb03

Please sign in to comment.