Skip to content

Commit

Permalink
ash: Remove unnecessary trivial_casts and trivial_numeric_casts
Browse files Browse the repository at this point in the history
While making the code only marginally harder to read such casts can also
introduce subtle bugs when used incorrectly, and are best omitted
whenever unnecessary: Rust already coerces borrows into raw pointers
when the types on both ends are clear, and even then there remain many
casts that are identical to the source type.

In addition these errors show up when using a local crate reference to
`ash` in a workspace that uses "the `.cargo/config.toml` setup" from
[EmbarkStudios/rust-ecosystem#68] to configure linter warnings
project-wide instead of for all crates in that workspace individually.
In our case aforementioned linter warnings are enabled on top of
Embark's configuration, leading to a lot of these warnings in our build
process.

[EmbarkStudios/rust-ecosystem#68]: EmbarkStudios/rust-ecosystem#68
  • Loading branch information
MarijnS95 committed Jan 25, 2022
1 parent 0fd7327 commit 9bae276
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 68 deletions.
6 changes: 4 additions & 2 deletions ash-window/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(trivial_casts, trivial_numeric_casts)]

use ash::{extensions::khr, prelude::*, vk, Entry, Instance};
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use std::ffi::CStr;
Expand Down Expand Up @@ -69,7 +71,7 @@ pub unsafe fn create_surface(
))]
RawWindowHandle::Xcb(handle) => {
let surface_desc = vk::XcbSurfaceCreateInfoKHR::builder()
.connection(handle.connection as *mut _)
.connection(handle.connection)
.window(handle.window);
let surface_fn = khr::XcbSurface::new(entry, instance);
surface_fn.create_xcb_surface(&surface_desc, allocation_callbacks)
Expand All @@ -78,7 +80,7 @@ pub unsafe fn create_surface(
#[cfg(any(target_os = "android"))]
RawWindowHandle::Android(handle) => {
let surface_desc =
vk::AndroidSurfaceCreateInfoKHR::builder().window(handle.a_native_window as _);
vk::AndroidSurfaceCreateInfoKHR::builder().window(handle.a_native_window);
let surface_fn = khr::AndroidSurface::new(entry, instance);
surface_fn.create_android_surface(&surface_desc, allocation_callbacks)
}
Expand Down
26 changes: 9 additions & 17 deletions ash/src/extensions/khr/acceleration_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl AccelerationStructure {
info: &vk::CopyAccelerationStructureInfoKHR,
) -> VkResult<()> {
self.fp
.copy_acceleration_structure_khr(self.handle, deferred_operation, info as *const _)
.copy_acceleration_structure_khr(self.handle, deferred_operation, info)
.result()
}

Expand All @@ -167,11 +167,7 @@ impl AccelerationStructure {
info: &vk::CopyAccelerationStructureToMemoryInfoKHR,
) -> VkResult<()> {
self.fp
.copy_acceleration_structure_to_memory_khr(
self.handle,
deferred_operation,
info as *const _,
)
.copy_acceleration_structure_to_memory_khr(self.handle, deferred_operation, info)
.result()
}

Expand All @@ -182,11 +178,7 @@ impl AccelerationStructure {
info: &vk::CopyMemoryToAccelerationStructureInfoKHR,
) -> VkResult<()> {
self.fp
.copy_memory_to_acceleration_structure_khr(
self.handle,
deferred_operation,
info as *const _,
)
.copy_memory_to_acceleration_structure_khr(self.handle, deferred_operation, info)
.result()
}

Expand Down Expand Up @@ -228,7 +220,7 @@ impl AccelerationStructure {
info: &vk::CopyAccelerationStructureToMemoryInfoKHR,
) {
self.fp
.cmd_copy_acceleration_structure_to_memory_khr(command_buffer, info as *const _);
.cmd_copy_acceleration_structure_to_memory_khr(command_buffer, info);
}

/// <https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCmdCopyMemoryToAccelerationStructureKHR.html>
Expand All @@ -238,7 +230,7 @@ impl AccelerationStructure {
info: &vk::CopyMemoryToAccelerationStructureInfoKHR,
) {
self.fp
.cmd_copy_memory_to_acceleration_structure_khr(command_buffer, info as *const _);
.cmd_copy_memory_to_acceleration_structure_khr(command_buffer, info);
}

/// <https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetAccelerationStructureHandleKHR.html>
Expand All @@ -247,7 +239,7 @@ impl AccelerationStructure {
info: &vk::AccelerationStructureDeviceAddressInfoKHR,
) -> vk::DeviceAddress {
self.fp
.get_acceleration_structure_device_address_khr(self.handle, info as *const _)
.get_acceleration_structure_device_address_khr(self.handle, info)
}

/// <https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCmdWriteAccelerationStructuresPropertiesKHR.html>
Expand Down Expand Up @@ -279,7 +271,7 @@ impl AccelerationStructure {
self.fp.get_device_acceleration_structure_compatibility_khr(
self.handle,
version,
&mut compatibility as *mut _,
&mut compatibility,
);

compatibility
Expand All @@ -299,9 +291,9 @@ impl AccelerationStructure {
self.fp.get_acceleration_structure_build_sizes_khr(
self.handle,
build_type,
build_info as *const _,
build_info,
max_primitive_counts.as_ptr(),
&mut size_info as *mut _,
&mut size_info,
);

size_info
Expand Down
8 changes: 4 additions & 4 deletions ash/src/extensions/khr/ray_tracing_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ impl RayTracingPipeline {
) {
self.fp.cmd_trace_rays_khr(
command_buffer,
raygen_shader_binding_tables as *const _,
miss_shader_binding_tables as *const _,
hit_shader_binding_tables as *const _,
callable_shader_binding_tables as *const _,
raygen_shader_binding_tables,
miss_shader_binding_tables,
hit_shader_binding_tables,
callable_shader_binding_tables,
width,
height,
depth,
Expand Down
4 changes: 2 additions & 2 deletions ash/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![deny(clippy::use_self)]
#![warn(/* trivial_casts, */ trivial_numeric_casts)]
#![allow(
clippy::too_many_arguments,
clippy::missing_safety_doc,
Expand Down Expand Up @@ -59,8 +60,7 @@ pub trait RawPtr<T> {
impl<'r, T> RawPtr<T> for Option<&'r T> {
fn as_raw_ptr(&self) -> *const T {
match *self {
Some(inner) => inner as *const T,

Some(inner) => inner,
_ => ::std::ptr::null(),
}
}
Expand Down
3 changes: 2 additions & 1 deletion ash/src/vk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub use prelude::*;
/// Native bindings from Vulkan headers, generated by bindgen
#[allow(nonstandard_style)]
#[allow(deref_nullptr)]
#[allow(trivial_casts, trivial_numeric_casts)]
pub mod native;
mod platform_types;
pub use platform_types::*;
Expand All @@ -40,7 +41,7 @@ pub(crate) unsafe fn ptr_chain_iter<T>(ptr: &mut T) -> impl Iterator<Item = *mut
if p_ptr.is_null() {
return None;
}
let n_ptr = (**p_ptr).p_next as *mut BaseOutStructure;
let n_ptr = (**p_ptr).p_next;
let old = *p_ptr;
*p_ptr = n_ptr;
Some(old)
Expand Down
66 changes: 33 additions & 33 deletions ash/src/vk/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ impl fmt::Debug for PhysicalDeviceProperties {
.field("device_id", &self.device_id)
.field("device_type", &self.device_type)
.field("device_name", &unsafe {
::std::ffi::CStr::from_ptr(self.device_name.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.device_name.as_ptr())
})
.field("pipeline_cache_uuid", &self.pipeline_cache_uuid)
.field("limits", &self.limits)
Expand Down Expand Up @@ -1108,7 +1108,7 @@ impl fmt::Debug for ExtensionProperties {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("ExtensionProperties")
.field("extension_name", &unsafe {
::std::ffi::CStr::from_ptr(self.extension_name.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.extension_name.as_ptr())
})
.field("spec_version", &self.spec_version)
.finish()
Expand Down Expand Up @@ -1176,12 +1176,12 @@ impl fmt::Debug for LayerProperties {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("LayerProperties")
.field("layer_name", &unsafe {
::std::ffi::CStr::from_ptr(self.layer_name.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.layer_name.as_ptr())
})
.field("spec_version", &self.spec_version)
.field("implementation_version", &self.implementation_version)
.field("description", &unsafe {
::std::ffi::CStr::from_ptr(self.description.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.description.as_ptr())
})
.finish()
}
Expand Down Expand Up @@ -4362,7 +4362,7 @@ impl<'a> ShaderModuleCreateInfoBuilder<'a> {
}
pub fn code(mut self, code: &'a [u32]) -> Self {
self.inner.code_size = code.len() * 4;
self.inner.p_code = code.as_ptr() as *const u32;
self.inner.p_code = code.as_ptr();
self
}
#[doc = r" Prepends the given extension struct between the root and the first pointer. This"]
Expand Down Expand Up @@ -4853,7 +4853,7 @@ impl<'a> SpecializationInfoBuilder<'a> {
self
}
pub fn data(mut self, data: &'a [u8]) -> Self {
self.inner.data_size = data.len() as _;
self.inner.data_size = data.len();
self.inner.p_data = data.as_ptr() as *const c_void;
self
}
Expand Down Expand Up @@ -5709,7 +5709,7 @@ impl<'a> PipelineMultisampleStateCreateInfoBuilder<'a> {
self.inner.p_sample_mask = if sample_mask.is_empty() {
std::ptr::null()
} else {
sample_mask.as_ptr() as *const SampleMask
sample_mask.as_ptr()
};
self
}
Expand Down Expand Up @@ -6401,7 +6401,7 @@ impl<'a> PipelineCacheCreateInfoBuilder<'a> {
self
}
pub fn initial_data(mut self, initial_data: &'a [u8]) -> Self {
self.inner.initial_data_size = initial_data.len() as _;
self.inner.initial_data_size = initial_data.len();
self.inner.p_initial_data = initial_data.as_ptr() as *const c_void;
self
}
Expand Down Expand Up @@ -11675,7 +11675,7 @@ impl<'a> DebugMarkerObjectTagInfoEXTBuilder<'a> {
self
}
pub fn tag(mut self, tag: &'a [u8]) -> Self {
self.inner.tag_size = tag.len() as _;
self.inner.tag_size = tag.len();
self.inner.p_tag = tag.as_ptr() as *const c_void;
self
}
Expand Down Expand Up @@ -14311,10 +14311,10 @@ impl fmt::Debug for PhysicalDeviceDriverProperties {
.field("p_next", &self.p_next)
.field("driver_id", &self.driver_id)
.field("driver_name", &unsafe {
::std::ffi::CStr::from_ptr(self.driver_name.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.driver_name.as_ptr())
})
.field("driver_info", &unsafe {
::std::ffi::CStr::from_ptr(self.driver_info.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.driver_info.as_ptr())
})
.field("conformance_version", &self.conformance_version)
.finish()
Expand Down Expand Up @@ -24097,7 +24097,7 @@ impl<'a> ValidationCacheCreateInfoEXTBuilder<'a> {
self
}
pub fn initial_data(mut self, initial_data: &'a [u8]) -> Self {
self.inner.initial_data_size = initial_data.len() as _;
self.inner.initial_data_size = initial_data.len();
self.inner.p_initial_data = initial_data.as_ptr() as *const c_void;
self
}
Expand Down Expand Up @@ -25487,7 +25487,7 @@ impl<'a> DebugUtilsObjectTagInfoEXTBuilder<'a> {
self
}
pub fn tag(mut self, tag: &'a [u8]) -> Self {
self.inner.tag_size = tag.len() as _;
self.inner.tag_size = tag.len();
self.inner.p_tag = tag.as_ptr() as *const c_void;
self
}
Expand Down Expand Up @@ -37008,13 +37008,13 @@ impl fmt::Debug for PerformanceCounterDescriptionKHR {
.field("p_next", &self.p_next)
.field("flags", &self.flags)
.field("name", &unsafe {
::std::ffi::CStr::from_ptr(self.name.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.name.as_ptr())
})
.field("category", &unsafe {
::std::ffi::CStr::from_ptr(self.category.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.category.as_ptr())
})
.field("description", &unsafe {
::std::ffi::CStr::from_ptr(self.description.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.description.as_ptr())
})
.finish()
}
Expand Down Expand Up @@ -38748,10 +38748,10 @@ impl fmt::Debug for PipelineExecutablePropertiesKHR {
.field("p_next", &self.p_next)
.field("stages", &self.stages)
.field("name", &unsafe {
::std::ffi::CStr::from_ptr(self.name.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.name.as_ptr())
})
.field("description", &unsafe {
::std::ffi::CStr::from_ptr(self.description.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.description.as_ptr())
})
.field("subgroup_size", &self.subgroup_size)
.finish()
Expand Down Expand Up @@ -38909,10 +38909,10 @@ impl fmt::Debug for PipelineExecutableStatisticKHR {
.field("s_type", &self.s_type)
.field("p_next", &self.p_next)
.field("name", &unsafe {
::std::ffi::CStr::from_ptr(self.name.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.name.as_ptr())
})
.field("description", &unsafe {
::std::ffi::CStr::from_ptr(self.description.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.description.as_ptr())
})
.field("format", &self.format)
.field("value", &"union")
Expand Down Expand Up @@ -38998,10 +38998,10 @@ impl fmt::Debug for PipelineExecutableInternalRepresentationKHR {
.field("s_type", &self.s_type)
.field("p_next", &self.p_next)
.field("name", &unsafe {
::std::ffi::CStr::from_ptr(self.name.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.name.as_ptr())
})
.field("description", &unsafe {
::std::ffi::CStr::from_ptr(self.description.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.description.as_ptr())
})
.field("is_text", &self.is_text)
.field("data_size", &self.data_size)
Expand Down Expand Up @@ -39060,7 +39060,7 @@ impl<'a> PipelineExecutableInternalRepresentationKHRBuilder<'a> {
self
}
pub fn data(mut self, data: &'a mut [u8]) -> Self {
self.inner.data_size = data.len() as _;
self.inner.data_size = data.len();
self.inner.p_data = data.as_mut_ptr() as *mut c_void;
self
}
Expand Down Expand Up @@ -40836,10 +40836,10 @@ impl fmt::Debug for PhysicalDeviceVulkan12Properties {
.field("p_next", &self.p_next)
.field("driver_id", &self.driver_id)
.field("driver_name", &unsafe {
::std::ffi::CStr::from_ptr(self.driver_name.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.driver_name.as_ptr())
})
.field("driver_info", &unsafe {
::std::ffi::CStr::from_ptr(self.driver_info.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.driver_info.as_ptr())
})
.field("conformance_version", &self.conformance_version)
.field(
Expand Down Expand Up @@ -41644,17 +41644,17 @@ impl fmt::Debug for PhysicalDeviceToolPropertiesEXT {
.field("s_type", &self.s_type)
.field("p_next", &self.p_next)
.field("name", &unsafe {
::std::ffi::CStr::from_ptr(self.name.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.name.as_ptr())
})
.field("version", &unsafe {
::std::ffi::CStr::from_ptr(self.version.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.version.as_ptr())
})
.field("purposes", &self.purposes)
.field("description", &unsafe {
::std::ffi::CStr::from_ptr(self.description.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.description.as_ptr())
})
.field("layer", &unsafe {
::std::ffi::CStr::from_ptr(self.layer.as_ptr() as *const c_char)
::std::ffi::CStr::from_ptr(self.layer.as_ptr())
})
.finish()
}
Expand Down Expand Up @@ -42923,7 +42923,7 @@ impl<'a> ::std::ops::DerefMut for AccelerationStructureVersionInfoKHRBuilder<'a>
}
impl<'a> AccelerationStructureVersionInfoKHRBuilder<'a> {
pub fn version_data(mut self, version_data: &'a [u8; 2 * UUID_SIZE]) -> Self {
self.inner.p_version_data = version_data as *const [u8; 2 * UUID_SIZE];
self.inner.p_version_data = version_data;
self
}
#[doc = r" Calling build will **discard** all the lifetime information. Only call this if"]
Expand Down Expand Up @@ -53094,7 +53094,7 @@ impl<'a> ::std::ops::DerefMut for CuModuleCreateInfoNVXBuilder<'a> {
}
impl<'a> CuModuleCreateInfoNVXBuilder<'a> {
pub fn data(mut self, data: &'a [u8]) -> Self {
self.inner.data_size = data.len() as _;
self.inner.data_size = data.len();
self.inner.p_data = data.as_ptr() as *const c_void;
self
}
Expand Down Expand Up @@ -53263,12 +53263,12 @@ impl<'a> CuLaunchInfoNVXBuilder<'a> {
self
}
pub fn params(mut self, params: &'a [*const c_void]) -> Self {
self.inner.param_count = params.len() as _;
self.inner.param_count = params.len();
self.inner.p_params = params.as_ptr();
self
}
pub fn extras(mut self, extras: &'a [*const c_void]) -> Self {
self.inner.extra_count = extras.len() as _;
self.inner.extra_count = extras.len();
self.inner.p_extras = extras.as_ptr();
self
}
Expand Down
Loading

0 comments on commit 9bae276

Please sign in to comment.