From 19c66754df77c74210a9915563bc979f88b66040 Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Mon, 29 Jul 2024 18:06:57 +0200 Subject: [PATCH] rusticl: use ptr::from_ref and ptr::from_mut This is considered safer as it prevents changing the type the pointer points to. It got stabilized with 1.76 Part-of: --- src/gallium/frontends/rusticl/api/icd.rs | 2 +- src/gallium/frontends/rusticl/api/util.rs | 2 +- src/gallium/frontends/rusticl/core/platform.rs | 3 ++- src/gallium/frontends/rusticl/mesa/compiler/clc/spirv.rs | 6 +++--- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/gallium/frontends/rusticl/api/icd.rs b/src/gallium/frontends/rusticl/api/icd.rs index 0a69858a1cf..d686e8ba07a 100644 --- a/src/gallium/frontends/rusticl/api/icd.rs +++ b/src/gallium/frontends/rusticl/api/icd.rs @@ -409,7 +409,7 @@ macro_rules! impl_cl_type_trait_base { impl std::hash::Hash for $t { fn hash(&self, state: &mut H) { - (self as *const Self).hash(state); + std::ptr::from_ref(self).hash(state); } } }; diff --git a/src/gallium/frontends/rusticl/api/util.rs b/src/gallium/frontends/rusticl/api/util.rs index db2e6cf449b..876697258d8 100644 --- a/src/gallium/frontends/rusticl/api/util.rs +++ b/src/gallium/frontends/rusticl/api/util.rs @@ -97,7 +97,7 @@ macro_rules! cl_prop_for_type { ($ty: ty) => { impl CLProp for $ty { fn cl_vec(&self) -> Vec> { - unsafe { slice::from_raw_parts((self as *const Self).cast(), size_of::()) } + unsafe { slice::from_raw_parts(std::ptr::from_ref(self).cast(), size_of::()) } .to_vec() } } diff --git a/src/gallium/frontends/rusticl/core/platform.rs b/src/gallium/frontends/rusticl/core/platform.rs index 50fb82ef877..0c50fd18d29 100644 --- a/src/gallium/frontends/rusticl/core/platform.rs +++ b/src/gallium/frontends/rusticl/core/platform.rs @@ -7,6 +7,7 @@ use mesa_rust_gen::*; use rusticl_opencl_gen::*; use std::env; +use std::ptr; use std::ptr::addr_of; use std::ptr::addr_of_mut; use std::sync::Once; @@ -124,7 +125,7 @@ fn load_env() { impl Platform { pub fn as_ptr(&self) -> cl_platform_id { - (self as *const Self) as cl_platform_id + ptr::from_ref(self) as cl_platform_id } pub fn get() -> &'static Self { diff --git a/src/gallium/frontends/rusticl/mesa/compiler/clc/spirv.rs b/src/gallium/frontends/rusticl/mesa/compiler/clc/spirv.rs index 1f1e1b73694..a25c5189ded 100644 --- a/src/gallium/frontends/rusticl/mesa/compiler/clc/spirv.rs +++ b/src/gallium/frontends/rusticl/mesa/compiler/clc/spirv.rs @@ -79,7 +79,7 @@ unsafe extern "C" fn spirv_to_nir_msg_callback( fn create_clc_logger(msgs: &mut Vec) -> clc_logger { clc_logger { - priv_: msgs as *mut Vec as *mut c_void, + priv_: ptr::from_mut(msgs).cast(), error: Some(spirv_msg_callback), warning: Some(spirv_msg_callback), } @@ -180,7 +180,7 @@ impl SPIRVBin { // TODO cache linking, parsing is around 25% of link time pub fn link(spirvs: &[&SPIRVBin], library: bool) -> (Option, String) { - let bins: Vec<_> = spirvs.iter().map(|s| &s.spirv as *const _).collect(); + let bins: Vec<_> = spirvs.iter().map(|s| ptr::from_ref(&s.spirv)).collect(); let linker_args = clc_linker_args { in_objs: bins.as_ptr(), @@ -308,7 +308,7 @@ impl SPIRVBin { let debug = log.map(|log| spirv_to_nir_options__bindgen_ty_1 { func: Some(spirv_to_nir_msg_callback), - private_data: (log as *mut Vec).cast(), + private_data: ptr::from_mut(log).cast(), }); spirv_to_nir_options {