blob: 4e363dbf81e0847cf847e6364385ef1dec4edbf9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
use std::mem;
// If this is `None`, the metadata becomes padding.
type T = Option<&'static str>;
fn main() {
unsafe {
let mut p: mem::MaybeUninit<T> = mem::MaybeUninit::zeroed();
// The copy when `T` is returned from `transmute` should destroy padding
// (even when we use `write_unaligned`, which under the hood uses an untyped copy).
p.as_mut_ptr().write_unaligned(mem::transmute((0usize, 0usize)));
// Null epresents `None`.
assert!(matches!(*p.as_ptr(), None));
// The second part, with the length, becomes padding.
let c = &p as *const _ as *const u8;
// Read a padding byte.
let _val = *c.add(mem::size_of::<*const u8>());
//~^ERROR: uninitialized
}
}
|