Skip to content

Commit

Permalink
refactor(hal): satisfy trivial_casts
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Aug 15, 2024
1 parent 89a64e9 commit 23e7846
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 29 deletions.
8 changes: 4 additions & 4 deletions naga/src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::{
proc::{self, NameKey, TypeResolution},
valid, FastHashMap, FastHashSet,
};
#[cfg(test)]
use std::ptr;
use std::{
fmt::{Display, Error as FmtError, Formatter, Write},
iter,
Expand Down Expand Up @@ -1411,9 +1413,8 @@ impl<W: Write> Writer<W> {
) -> BackendResult {
// Add to the set in order to track the stack size.
#[cfg(test)]
#[allow(trivial_casts)]
self.put_expression_stack_pointers
.insert(&expr_handle as *const _ as *const ());
.insert(ptr::from_ref(&expr_handle).cast());

if let Some(name) = self.named_expressions.get(&expr_handle) {
write!(self.out, "{name}")?;
Expand Down Expand Up @@ -2792,9 +2793,8 @@ impl<W: Write> Writer<W> {
) -> BackendResult {
// Add to the set in order to track the stack size.
#[cfg(test)]
#[allow(trivial_casts)]
self.put_block_stack_pointers
.insert(&level as *const _ as *const ());
.insert(ptr::from_ref(&level).cast());

for statement in statements {
log::trace!("statement[{}] {:?}", level.0, statement);
Expand Down
26 changes: 12 additions & 14 deletions wgpu-hal/src/dynamic/device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Box casts are needed, alternative would be a temporaries which are more verbose and not more expressive.
#![allow(trivial_casts)]

use crate::{
AccelerationStructureBuildSizes, AccelerationStructureDescriptor, Api, BindGroupDescriptor,
BindGroupLayoutDescriptor, BufferDescriptor, BufferMapping, CommandEncoderDescriptor,
Expand Down Expand Up @@ -261,7 +258,7 @@ impl<D: Device + DynResource> DynDevice for D {
queue: desc.queue.expect_downcast_ref(),
};
unsafe { D::create_command_encoder(self, &desc) }
.map(|b| Box::new(b) as Box<dyn DynCommandEncoder>)
.map(|b| -> Box<dyn DynCommandEncoder> { Box::new(b) })
}

unsafe fn destroy_command_encoder(&self, encoder: Box<dyn DynCommandEncoder>) {
Expand All @@ -273,7 +270,7 @@ impl<D: Device + DynResource> DynDevice for D {
desc: &BindGroupLayoutDescriptor,
) -> Result<Box<dyn DynBindGroupLayout>, DeviceError> {
unsafe { D::create_bind_group_layout(self, desc) }
.map(|b| Box::new(b) as Box<dyn DynBindGroupLayout>)
.map(|b| -> Box<dyn DynBindGroupLayout> { Box::new(b) })
}

unsafe fn destroy_bind_group_layout(&self, bg_layout: Box<dyn DynBindGroupLayout>) {
Expand All @@ -297,7 +294,7 @@ impl<D: Device + DynResource> DynDevice for D {
};

unsafe { D::create_pipeline_layout(self, &desc) }
.map(|b| Box::new(b) as Box<dyn DynPipelineLayout>)
.map(|b| -> Box<dyn DynPipelineLayout> { Box::new(b) })
}

unsafe fn destroy_pipeline_layout(&self, pipeline_layout: Box<dyn DynPipelineLayout>) {
Expand Down Expand Up @@ -345,7 +342,8 @@ impl<D: Device + DynResource> DynDevice for D {
acceleration_structures: &acceleration_structures,
};

unsafe { D::create_bind_group(self, &desc) }.map(|b| Box::new(b) as Box<dyn DynBindGroup>)
unsafe { D::create_bind_group(self, &desc) }
.map(|b| -> Box<dyn DynBindGroup> { Box::new(b) })
}

unsafe fn destroy_bind_group(&self, group: Box<dyn DynBindGroup>) {
Expand All @@ -358,7 +356,7 @@ impl<D: Device + DynResource> DynDevice for D {
shader: ShaderInput,
) -> Result<Box<dyn DynShaderModule>, ShaderError> {
unsafe { D::create_shader_module(self, desc, shader) }
.map(|b| Box::new(b) as Box<dyn DynShaderModule>)
.map(|b| -> Box<dyn DynShaderModule> { Box::new(b) })
}

unsafe fn destroy_shader_module(&self, module: Box<dyn DynShaderModule>) {
Expand Down Expand Up @@ -388,7 +386,7 @@ impl<D: Device + DynResource> DynDevice for D {
};

unsafe { D::create_render_pipeline(self, &desc) }
.map(|b| Box::new(b) as Box<dyn DynRenderPipeline>)
.map(|b| -> Box<dyn DynRenderPipeline> { Box::new(b) })
}

unsafe fn destroy_render_pipeline(&self, pipeline: Box<dyn DynRenderPipeline>) {
Expand All @@ -411,7 +409,7 @@ impl<D: Device + DynResource> DynDevice for D {
};

unsafe { D::create_compute_pipeline(self, &desc) }
.map(|b| Box::new(b) as Box<dyn DynComputePipeline>)
.map(|b| -> Box<dyn DynComputePipeline> { Box::new(b) })
}

unsafe fn destroy_compute_pipeline(&self, pipeline: Box<dyn DynComputePipeline>) {
Expand All @@ -423,7 +421,7 @@ impl<D: Device + DynResource> DynDevice for D {
desc: &PipelineCacheDescriptor<'_>,
) -> Result<Box<dyn DynPipelineCache>, PipelineCacheError> {
unsafe { D::create_pipeline_cache(self, desc) }
.map(|b| Box::new(b) as Box<dyn DynPipelineCache>)
.map(|b| -> Box<dyn DynPipelineCache> { Box::new(b) })
}

fn pipeline_cache_validation_key(&self) -> Option<[u8; 16]> {
Expand All @@ -438,15 +436,15 @@ impl<D: Device + DynResource> DynDevice for D {
&self,
desc: &wgt::QuerySetDescriptor<Label>,
) -> Result<Box<dyn DynQuerySet>, DeviceError> {
unsafe { D::create_query_set(self, desc) }.map(|b| Box::new(b) as Box<dyn DynQuerySet>)
unsafe { D::create_query_set(self, desc) }.map(|b| -> Box<dyn DynQuerySet> { Box::new(b) })
}

unsafe fn destroy_query_set(&self, query_set: Box<dyn DynQuerySet>) {
unsafe { D::destroy_query_set(self, query_set.unbox()) };
}

unsafe fn create_fence(&self) -> Result<Box<dyn DynFence>, DeviceError> {
unsafe { D::create_fence(self) }.map(|f| Box::new(f) as Box<dyn DynFence>)
unsafe { D::create_fence(self) }.map(|b| -> Box<dyn DynFence> { Box::new(b) })
}

unsafe fn destroy_fence(&self, fence: Box<dyn DynFence>) {
Expand Down Expand Up @@ -486,7 +484,7 @@ impl<D: Device + DynResource> DynDevice for D {
desc: &AccelerationStructureDescriptor,
) -> Result<Box<dyn DynAccelerationStructure>, DeviceError> {
unsafe { D::create_acceleration_structure(self, desc) }
.map(|b| Box::new(b) as Box<dyn DynAccelerationStructure>)
.map(|b| -> Box<dyn DynAccelerationStructure> { Box::new(b) })
}

unsafe fn get_acceleration_structure_build_sizes(
Expand Down
5 changes: 1 addition & 4 deletions wgpu-hal/src/dynamic/instance.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Box casts are needed, alternative would be a temporaries which are more verbose and not more expressive.
#![allow(trivial_casts)]

use crate::{Api, Capabilities, ExposedAdapter, Instance, InstanceError};

use super::{DynAdapter, DynResource, DynResourceExt as _, DynSurface};
Expand Down Expand Up @@ -50,7 +47,7 @@ impl<I: Instance + DynResource> DynInstance for I {
window_handle: raw_window_handle::RawWindowHandle,
) -> Result<Box<dyn DynSurface>, InstanceError> {
unsafe { I::create_surface(self, display_handle, window_handle) }
.map(|surface| Box::new(surface) as Box<dyn DynSurface>)
.map(|surface| -> Box<dyn DynSurface> { Box::new(surface) })
}

unsafe fn enumerate_adapters(
Expand Down
5 changes: 2 additions & 3 deletions wgpu-hal/src/metal/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ impl HalManagedMetalLayerDelegate {
CAML_DELEGATE_REGISTER.call_once(|| {
type Fun = extern "C" fn(&Class, Sel, *mut Object, CGFloat, *mut Object) -> BOOL;
let mut decl = ClassDecl::new(&class_name, class!(NSObject)).unwrap();
#[allow(trivial_casts)] // false positive
unsafe {
decl.add_class_method(
decl.add_class_method::<Fun>(
sel!(layer:shouldInheritContentsScale:fromWindow:),
layer_should_inherit_contents_scale_from_window as Fun,
layer_should_inherit_contents_scale_from_window,
);
}
decl.register();
Expand Down
5 changes: 1 addition & 4 deletions wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1560,16 +1560,13 @@ impl crate::Device for super::Device {
// we can't use the safe (yet unstable) MaybeUninit::write_slice() here because of having an iterator to write

let init = {
#[allow(trivial_casts)]
// SAFETY: The loop above has initialized exactly as many items as to_init is
// long, so it is safe to cast away the MaybeUninit<T> wrapper into T.

// Additional safety docs from unstable slice_assume_init_mut
// SAFETY: similar to safety notes for `slice_get_ref`, but we have a
// mutable reference which is also guaranteed to be valid for writes.
unsafe {
&mut *(ptr::from_mut::<[MaybeUninit<T>]>(to_init) as *mut [T])
}
unsafe { std::mem::transmute::<&mut [MaybeUninit<T>], &mut [T]>(to_init) }
};
(Self { remainder }, init)
}
Expand Down

0 comments on commit 23e7846

Please sign in to comment.