-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
offset_from.rs
51 lines (43 loc) · 1.51 KB
/
offset_from.rs
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
48
49
50
51
// run-pass
#![feature(const_ptr_offset_from)]
struct Struct {
field: (),
}
#[repr(C)]
struct Struct2 {
data: u8,
field: u8,
}
pub const OFFSET: usize = {
let uninit = std::mem::MaybeUninit::<Struct>::uninit();
let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
// The following statement is UB (taking the address of an uninitialized field).
// Const eval doesn't detect this right now, but it may stop compiling at some point
// in the future.
let field_ptr = unsafe { &(*base_ptr).field as *const () as *const u8 };
let offset = unsafe { field_ptr.offset_from(base_ptr as *const u8) };
offset as usize
};
pub const OFFSET_2: usize = {
let uninit = std::mem::MaybeUninit::<Struct2>::uninit();
let base_ptr: *const Struct2 = &uninit as *const _ as *const Struct2;
let field_ptr = unsafe { &(*base_ptr).field as *const u8 };
let offset = unsafe { field_ptr.offset_from(base_ptr as *const u8) };
offset as usize
};
pub const OVERFLOW: isize = {
let uninit = std::mem::MaybeUninit::<Struct2>::uninit();
let base_ptr: *const Struct2 = &uninit as *const _ as *const Struct2;
let field_ptr = unsafe { &(*base_ptr).field as *const u8 };
unsafe { (base_ptr as *const u8).offset_from(field_ptr) }
};
pub const OFFSET_EQUAL_INTS: isize = {
let ptr = 1 as *const u8;
unsafe { ptr.offset_from(ptr) }
};
fn main() {
assert_eq!(OFFSET, 0);
assert_eq!(OFFSET_2, 1);
assert_eq!(OVERFLOW, -1);
assert_eq!(OFFSET_EQUAL_INTS, 0);
}