Skip to main content

dobby_rs_framework/framework/
params.rs

1use core::ffi::c_void;
2
3/// Cast a raw pointer (address) to a function pointer type.
4///
5/// # Safety
6/// - `T` must be a function pointer type with the correct ABI/signature.
7/// - The address must be valid to call as `T`.
8pub unsafe fn cast_fn<T: Copy>(ptr: *mut c_void) -> T {
9    debug_assert_eq!(
10        core::mem::size_of::<T>(),
11        core::mem::size_of::<*mut c_void>()
12    );
13    core::mem::transmute_copy(&ptr)
14}
15
16/// Cast a raw pointer to a typed pointer.
17pub fn cast_ptr<T>(ptr: *mut c_void) -> *mut T {
18    ptr as *mut T
19}
20
21/// Read a value from a raw pointer (unaligned-safe).
22///
23/// # Safety
24/// - `ptr` must be valid for reads of `size_of::<T>()` bytes.
25pub unsafe fn read_ptr_value<T: Copy>(ptr: *const c_void) -> T {
26    core::ptr::read_unaligned(ptr as *const T)
27}
28
29/// Write a value to a raw pointer (unaligned-safe).
30///
31/// # Safety
32/// - `ptr` must be valid for writes of `size_of::<T>()` bytes.
33pub unsafe fn write_ptr_value<T>(ptr: *mut c_void, value: T) {
34    core::ptr::write_unaligned(ptr as *mut T, value);
35}