blob: 98532ad4fb0250df7c51c549021fed6b179719a1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
use std::ptr;
pub trait CheckedPtr<T> {
/// # Safety
///
/// besides a null check the function can't make sure the pointer is valid
/// for the entire size
unsafe fn copy_checked(self, val: *const T, size: usize);
fn write_checked(self, val: T);
}
impl<T> CheckedPtr<T> for *mut T {
unsafe fn copy_checked(self, val: *const T, size: usize) {
if !self.is_null() {
ptr::copy(val, self, size);
}
}
fn write_checked(self, val: T) {
if !self.is_null() {
unsafe {
*self = val;
}
}
}
}
// from https://internals.rust-lang.org/t/discussion-on-offset-of/7440/2
#[macro_export]
macro_rules! offset_of {
($Struct:path, $field:ident) => {{
// Using a separate function to minimize unhygienic hazards
// (e.g. unsafety of #[repr(packed)] field borrows).
// Uncomment `const` when `const fn`s can juggle pointers.
/*const*/
fn offset() -> usize {
let u = std::mem::MaybeUninit::<$Struct>::uninit();
// Use pattern-matching to avoid accidentally going through Deref.
let &$Struct { $field: ref f, .. } = unsafe { &*u.as_ptr() };
let o = (f as *const _ as usize).wrapping_sub(&u as *const _ as usize);
// Triple check that we are within `u` still.
assert!((0..=std::mem::size_of_val(&u)).contains(&o));
o
}
offset()
}};
}
|