From ec44db1eb58d7785f78d3d69e0f9123ad6f42fb3 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Sat, 27 Aug 2022 09:51:28 -0700 Subject: [PATCH] Don't use a callback to return backend instance handles. Change `Instance::as_hal` to simply return an `Option<&A::Instance>`, rather than passing it to a callback. We're just borrowing a reference to some `wgpu_hal::Api::Instance` owned by the `wgpu_core::instance::Instance`, so there's no special scoping or locking magic required here. --- wgpu-core/src/hub.rs | 10 +++------- wgpu/src/backend/direct.rs | 10 +++++----- wgpu/src/lib.rs | 18 +++++++++--------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index 091e7856650..043cb077362 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -947,13 +947,9 @@ impl Global { /// # Safety /// - /// - The raw handle obtained from the hal Instance must not be manually destroyed - pub unsafe fn instance_as_hal) -> R, R>( - &self, - hal_instance_callback: F, - ) -> R { - let hal_instance = A::instance_as_hal(&self.instance); - hal_instance_callback(hal_instance) + /// - The raw instance handle returned must not be manually destroyed. + pub unsafe fn instance_as_hal(&self) -> Option<&A::Instance> { + A::instance_as_hal(&self.instance) } /// # Safety diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 870d03d106a..d358499f91c 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -48,11 +48,11 @@ impl Context { )) } - pub unsafe fn instance_as_hal) -> R, R>( - &self, - hal_instance_callback: F, - ) -> R { - self.0.instance_as_hal::(hal_instance_callback) + /// # Safety + /// + /// - The raw instance handle returned must not be manually destroyed. + pub unsafe fn instance_as_hal(&self) -> Option<&A::Instance> { + self.0.instance_as_hal::() } pub unsafe fn from_core_instance(core_instance: wgc::instance::Instance) -> Self { diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index ad94bd5b1cb..38dc4e8c8b5 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1671,19 +1671,19 @@ impl Instance { } } - /// Returns the inner hal Instance using a callback. The hal instance will be `None` if the - /// backend type argument does not match with this wgpu Instance + /// Return a reference to a specific backend instance, if available. + /// + /// If this `Instance` has a wgpu-hal [`Instance`] for backend + /// `A`, return a reference to it. Otherwise, return `None`. /// /// # Safety /// - /// - The raw handle obtained from the hal Instance must not be manually destroyed + /// - The raw instance handle returned must not be manually destroyed. + /// + /// [`Instance`]: hal::Api::Instance #[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))] - pub unsafe fn as_hal) -> R, R>( - &self, - hal_instance_callback: F, - ) -> R { - self.context - .instance_as_hal::(hal_instance_callback) + pub unsafe fn as_hal(&self) -> Option<&A::Instance> { + self.context.instance_as_hal::() } /// Create an new instance of wgpu from a wgpu-core instance.