Skip to content

Commit

Permalink
refactor: satisfy clippy::missing_transmute_annotations (#6024)
Browse files Browse the repository at this point in the history
* refactor(metal): satisfy `clippy::missing_transmute_annotations`

* refactor(gles): satisfy `clippy::missing_transmute_annotations`

* refactor(metal): `metal::Surface::view`: use `ptr::cast` instead of `as`
  • Loading branch information
ErichDonGubler authored Jul 24, 2024
1 parent 9b680e6 commit 7b2e08f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
17 changes: 14 additions & 3 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
16 changes: 6 additions & 10 deletions wgpu-hal/src/gles/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use std::{
};

use arrayvec::ArrayVec;
#[cfg(native)]
use std::mem;
use std::sync::atomic::Ordering;

type ShaderStage<'a> = (
Expand Down Expand Up @@ -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) };
}

Expand Down Expand Up @@ -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)) };
}
}
Expand Down Expand Up @@ -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)) };
}
}
Expand Down Expand Up @@ -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)) };
}
}
Expand Down Expand Up @@ -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)) };
}
}
Expand Down Expand Up @@ -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)) };
}
}
Expand Down
15 changes: 12 additions & 3 deletions wgpu-hal/src/metal/surface.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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::<Object>();
let render_layer = {
let layer = unsafe { Self::get_metal_layer(view, delegate) };
unsafe { mem::transmute::<_, &metal::MetalLayerRef>(layer) }
let layer = layer.cast::<metal::MetalLayerRef>();
// 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];
Expand Down

0 comments on commit 7b2e08f

Please sign in to comment.