]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-size_of_val-align_of_val.rs
Do not suggest `let_else` if no bindings would be introduced
[rust.git] / src / test / ui / consts / const-size_of_val-align_of_val.rs
1 // run-pass
2
3 #![feature(const_size_of_val, const_align_of_val)]
4 #![feature(const_size_of_val_raw, const_align_of_val_raw, layout_for_ptr)]
5
6 use std::mem;
7
8 struct Foo(u32);
9
10 #[derive(Clone, Copy)]
11 struct Bar {
12     _x: u8,
13     _y: u16,
14     _z: u8,
15 }
16
17 union Ugh {
18     _a: [u8; 3],
19     _b: Bar,
20 }
21
22 const FOO: Foo = Foo(4);
23 const BAR: Bar = Bar { _x: 4, _y: 1, _z: 2 };
24 const UGH: Ugh = Ugh { _a: [0; 3] };
25
26 const SIZE_OF_FOO: usize = mem::size_of_val(&FOO);
27 const SIZE_OF_BAR: usize = mem::size_of_val(&BAR);
28 const SIZE_OF_UGH: usize = mem::size_of_val(&UGH);
29
30 const ALIGN_OF_FOO: usize = mem::align_of_val(&FOO);
31 const ALIGN_OF_BAR: usize = mem::align_of_val(&BAR);
32 const ALIGN_OF_UGH: usize = mem::align_of_val(&UGH);
33
34 const SIZE_OF_SLICE: usize = mem::size_of_val("foobar".as_bytes());
35
36 const SIZE_OF_DANGLING: usize = unsafe { mem::size_of_val_raw(0x100 as *const i32) };
37 const ALIGN_OF_DANGLING: usize = unsafe { mem::align_of_val_raw(0x100 as *const i16) };
38
39 fn main() {
40     assert_eq!(SIZE_OF_FOO, mem::size_of::<Foo>());
41     assert_eq!(SIZE_OF_BAR, mem::size_of::<Bar>());
42     assert_eq!(SIZE_OF_UGH, mem::size_of::<Ugh>());
43
44     assert_eq!(ALIGN_OF_FOO, mem::align_of::<Foo>());
45     assert_eq!(ALIGN_OF_BAR, mem::align_of::<Bar>());
46     assert_eq!(ALIGN_OF_UGH, mem::align_of::<Ugh>());
47
48     assert_eq!(SIZE_OF_DANGLING, mem::size_of::<i32>());
49     assert_eq!(ALIGN_OF_DANGLING, mem::align_of::<i16>());
50
51     assert_eq!(SIZE_OF_SLICE, "foobar".len());
52 }