From f8deb0317fb36864d4f4ddab02aa062993537f79 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Wed, 10 Apr 2024 00:53:10 -0700 Subject: [PATCH] [core] Use `expect` instead of "if let else panic". (#5513) Use `Option::expect` to check the result from `Arc::into_inner`, rather than `if let Some ... else panic!`. --- wgpu-core/src/command/mod.rs | 8 +++----- wgpu-core/src/device/queue.rs | 11 ++++------- wgpu-core/src/global.rs | 8 +++----- wgpu-core/src/instance.rs | 27 ++++++++++++--------------- 4 files changed, 22 insertions(+), 32 deletions(-) diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index e2e8f74113..8d5d8b64d2 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -286,11 +286,9 @@ impl CommandBuffer { } pub(crate) fn from_arc_into_baked(self: Arc) -> BakedCommands { - 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() } } diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 0e91408b65..4e412f685f 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -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; } diff --git a/wgpu-core/src/global.rs b/wgpu-core/src/global.rs index 9a8c43bf33..fbfb257285 100644 --- a/wgpu-core/src/global.rs +++ b/wgpu-core/src/global.rs @@ -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); } } } diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 55c7b581e9..20e67d5f71 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -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::(self, &surface.raw, &present); - #[cfg(metal)] - unconfigure::(self, &surface.raw, &present); - #[cfg(dx12)] - unconfigure::(self, &surface.raw, &present); - #[cfg(gles)] - unconfigure::(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::(self, &surface.raw, &present); + #[cfg(metal)] + unconfigure::(self, &surface.raw, &present); + #[cfg(dx12)] + unconfigure::(self, &surface.raw, &present); + #[cfg(gles)] + unconfigure::(self, &surface.raw, &present); } + self.instance.destroy_surface(surface); } fn enumerate(