]> git.proxmox.com Git - rustc.git/blame - library/core/tests/const_ptr.rs
New upstream version 1.52.0+dfsg1
[rustc.git] / library / core / tests / const_ptr.rs
CommitLineData
5869c6ff
XL
1// Aligned to two bytes
2const DATA: [u16; 2] = [u16::from_ne_bytes([0x01, 0x23]), u16::from_ne_bytes([0x45, 0x67])];
3
4const fn unaligned_ptr() -> *const u16 {
5 // Since DATA.as_ptr() is aligned to two bytes, adding 1 byte to that produces an unaligned *const u16
6 unsafe { (DATA.as_ptr() as *const u8).add(1) as *const u16 }
7}
8
9#[test]
10fn read() {
11 use core::ptr;
12
13 const FOO: i32 = unsafe { ptr::read(&42 as *const i32) };
14 assert_eq!(FOO, 42);
15
16 const ALIGNED: i32 = unsafe { ptr::read_unaligned(&42 as *const i32) };
17 assert_eq!(ALIGNED, 42);
18
19 const UNALIGNED_PTR: *const u16 = unaligned_ptr();
20
21 const UNALIGNED: u16 = unsafe { ptr::read_unaligned(UNALIGNED_PTR) };
22 assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
23}
24
25#[test]
26fn const_ptr_read() {
27 const FOO: i32 = unsafe { (&42 as *const i32).read() };
28 assert_eq!(FOO, 42);
29
30 const ALIGNED: i32 = unsafe { (&42 as *const i32).read_unaligned() };
31 assert_eq!(ALIGNED, 42);
32
33 const UNALIGNED_PTR: *const u16 = unaligned_ptr();
34
35 const UNALIGNED: u16 = unsafe { UNALIGNED_PTR.read_unaligned() };
36 assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
37}
38
39#[test]
40fn mut_ptr_read() {
41 const FOO: i32 = unsafe { (&42 as *const i32 as *mut i32).read() };
42 assert_eq!(FOO, 42);
43
44 const ALIGNED: i32 = unsafe { (&42 as *const i32 as *mut i32).read_unaligned() };
45 assert_eq!(ALIGNED, 42);
46
47 const UNALIGNED_PTR: *mut u16 = unaligned_ptr() as *mut u16;
48
49 const UNALIGNED: u16 = unsafe { UNALIGNED_PTR.read_unaligned() };
50 assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
51}
6a06907d 52
36d6ef2b
XL
53//#[test]
54//fn write() {
55// use core::ptr;
56//
57// const fn write_aligned() -> i32 {
58// let mut res = 0;
59// unsafe {
60// ptr::write(&mut res as *mut _, 42);
61// }
62// res
63// }
64// const ALIGNED: i32 = write_aligned();
65// assert_eq!(ALIGNED, 42);
66//
67// const fn write_unaligned() -> [u16; 2] {
68// let mut two_aligned = [0u16; 2];
69// unsafe {
70// let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
71// ptr::write_unaligned(unaligned_ptr, u16::from_ne_bytes([0x23, 0x45]));
72// }
73// two_aligned
74// }
75// const UNALIGNED: [u16; 2] = write_unaligned();
76// assert_eq!(UNALIGNED, [u16::from_ne_bytes([0x00, 0x23]), u16::from_ne_bytes([0x45, 0x00])]);
77//}
78
79//#[test]
80//fn mut_ptr_write() {
81// const fn aligned() -> i32 {
82// let mut res = 0;
83// unsafe {
84// (&mut res as *mut i32).write(42);
85// }
86// res
87// }
88// const ALIGNED: i32 = aligned();
89// assert_eq!(ALIGNED, 42);
90//
91// const fn write_unaligned() -> [u16; 2] {
92// let mut two_aligned = [0u16; 2];
93// unsafe {
94// let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
95// unaligned_ptr.write_unaligned(u16::from_ne_bytes([0x23, 0x45]));
96// }
97// two_aligned
98// }
99// const UNALIGNED: [u16; 2] = write_unaligned();
100// assert_eq!(UNALIGNED, [u16::from_ne_bytes([0x00, 0x23]), u16::from_ne_bytes([0x45, 0x00])]);
101//}