diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index ee50bd949..cd3894218 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -577,9 +577,20 @@ impl Global { metal: Some(self.instance.metal.as_ref().map_or( Err(CreateSurfaceError::BackendNotEnabled(Backend::Metal)), |inst| { - // we don't want to link to metal-rs for this - #[allow(clippy::transmute_ptr_to_ref)] - Ok(inst.create_surface_from_layer(unsafe { std::mem::transmute(layer) })) + let layer = layer.cast(); + // SAFETY: We do this cast and deref. (rather than using `metal` to get the + // object we want) to avoid direct coupling on the `metal` crate. + // + // To wit, this pointer… + // + // - …is properly aligned. + // - …is dereferenceable to a `MetalLayerRef` as an invariant of the `metal` + // field. + // - …points to an _initialized_ `MetalLayerRef`. + // - …is only ever aliased via an immutable reference that lives within this + // lexical scope. + let layer = unsafe { &*layer }; + Ok(inst.create_surface_from_layer(layer)) }, )?), #[cfg(dx12)] diff --git a/wgpu-hal/src/gles/device.rs b/wgpu-hal/src/gles/device.rs index 67d0a2971..4f187709a 100644 --- a/wgpu-hal/src/gles/device.rs +++ b/wgpu-hal/src/gles/device.rs @@ -9,8 +9,6 @@ use std::{ }; use arrayvec::ArrayVec; -#[cfg(native)] -use std::mem; use std::sync::atomic::Ordering; type ShaderStage<'a> = ( @@ -178,9 +176,7 @@ impl super::Device { let raw = unsafe { gl.create_shader(target) }.unwrap(); #[cfg(native)] if gl.supports_debug() { - //TODO: remove all transmutes from `object_label` - // https://github.com/grovesNL/glow/issues/186 - let name = unsafe { mem::transmute(raw) }; + let name = raw.0.get(); unsafe { gl.object_label(glow::SHADER, name, label) }; } @@ -366,7 +362,7 @@ impl super::Device { #[cfg(native)] if let Some(label) = label { if private_caps.contains(PrivateCapabilities::DEBUG_FNS) { - let name = unsafe { mem::transmute(program) }; + let name = program.0.get(); unsafe { gl.object_label(glow::PROGRAM, name, Some(label)) }; } } @@ -621,7 +617,7 @@ impl crate::Device for super::Device { .private_caps .contains(PrivateCapabilities::DEBUG_FNS) { - let name = unsafe { mem::transmute(raw) }; + let name = raw.map_or(0, |buf| buf.0.get()); unsafe { gl.object_label(glow::BUFFER, name, Some(label)) }; } } @@ -768,7 +764,7 @@ impl crate::Device for super::Device { .private_caps .contains(PrivateCapabilities::DEBUG_FNS) { - let name = unsafe { mem::transmute(raw) }; + let name = raw.0.get(); unsafe { gl.object_label(glow::RENDERBUFFER, name, Some(label)) }; } } @@ -936,7 +932,7 @@ impl crate::Device for super::Device { .private_caps .contains(PrivateCapabilities::DEBUG_FNS) { - let name = unsafe { mem::transmute(raw) }; + let name = raw.0.get(); unsafe { gl.object_label(glow::TEXTURE, name, Some(label)) }; } } @@ -1088,7 +1084,7 @@ impl crate::Device for super::Device { .private_caps .contains(PrivateCapabilities::DEBUG_FNS) { - let name = unsafe { mem::transmute(raw) }; + let name = raw.0.get(); unsafe { gl.object_label(glow::SAMPLER, name, Some(label)) }; } } diff --git a/wgpu-hal/src/metal/surface.rs b/wgpu-hal/src/metal/surface.rs index 1a1105660..b0ea55e9f 100644 --- a/wgpu-hal/src/metal/surface.rs +++ b/wgpu-hal/src/metal/surface.rs @@ -1,6 +1,6 @@ #![allow(clippy::let_unit_value)] // `let () =` being used to constrain result type -use std::{mem, os::raw::c_void, ptr::NonNull, sync::Once, thread}; +use std::{os::raw::c_void, ptr::NonNull, sync::Once, thread}; use core_graphics_types::{ base::CGFloat, @@ -82,10 +82,19 @@ impl super::Surface { view: *mut c_void, delegate: Option<&HalManagedMetalLayerDelegate>, ) -> Self { - let view = view as *mut Object; + let view = view.cast::(); let render_layer = { let layer = unsafe { Self::get_metal_layer(view, delegate) }; - unsafe { mem::transmute::<_, &metal::MetalLayerRef>(layer) } + let layer = layer.cast::(); + // SAFETY: This pointer… + // + // - …is properly aligned. + // - …is dereferenceable to a `MetalLayerRef` as an invariant of the `metal` + // field. + // - …points to an _initialized_ `MetalLayerRef`. + // - …is only ever aliased via an immutable reference that lives within this + // lexical scope. + unsafe { &*layer } } .to_owned(); let _: *mut c_void = msg_send![view, retain];