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: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/30414>
This commit is contained in:
Karol Herbst 2024-07-29 18:06:57 +02:00 committed by Marge Bot
parent 11e4793b41
commit 19c66754df
4 changed files with 7 additions and 6 deletions

View file

@ -409,7 +409,7 @@ macro_rules! impl_cl_type_trait_base {
impl std::hash::Hash for $t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
(self as *const Self).hash(state);
std::ptr::from_ref(self).hash(state);
}
}
};

View file

@ -97,7 +97,7 @@ macro_rules! cl_prop_for_type {
($ty: ty) => {
impl CLProp for $ty {
fn cl_vec(&self) -> Vec<MaybeUninit<u8>> {
unsafe { slice::from_raw_parts((self as *const Self).cast(), size_of::<Self>()) }
unsafe { slice::from_raw_parts(std::ptr::from_ref(self).cast(), size_of::<Self>()) }
.to_vec()
}
}

View file

@ -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 {

View file

@ -79,7 +79,7 @@ unsafe extern "C" fn spirv_to_nir_msg_callback(
fn create_clc_logger(msgs: &mut Vec<String>) -> clc_logger {
clc_logger {
priv_: msgs as *mut Vec<String> 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<Self>, 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<String>).cast(),
private_data: ptr::from_mut(log).cast(),
});
spirv_to_nir_options {