I'm curious if this is 'safe' in the sense its not UB.
Basically I want to provide some Arrays of C structures, which rust code can modify the data.
struct data {
uintptr_t pointer;
size_t stride;
size_t count;
};
struct tform {
float position, rotation;
}
struct data api_get_data() {
static tform t[3] = {
{3.1f, 4.1f},
{3.2f, 4.2f},
{3.3f, 4.3f},
};
return {
(uintptr_t)t,
sizeof(tform),
3,
};
};
I have the equivlent structure in Rust and the function prototype.
#[repr(C)]
struct Transform {
position : f32,
rotation : f32,
}
And I'm accessing the array like this in rust.
let d = api_get_data();
let bytes = d.stride * d.count;
let ptr = d.pointer as *mut Transform;
let mut t = std::slice::from_raw_parts_mut(ptr, bytes);
do_thing(&mut t);
I have two questions, is this a performant solution and is this an valid solution. I saw this in the documentation for from_raw_parts
which makes me think this isn't correct.
The entire memory range of this slice must be contained within a single allocated object! Slices can never span across multiple allocated objects.