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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
// run-pass
// revisions: mirunsafeck thirunsafeck
// [thirunsafeck]compile-flags: -Z thir-unsafeck
#![allow(dead_code)]
use std::mem::{size_of, size_of_val, align_of, align_of_val};
#[repr(align(16))]
pub union U16 {
a: u8,
b: u32
}
fn main() {
assert_eq!(align_of::<U16>(), 16);
assert_eq!(size_of::<U16>(), 16);
let u = U16 { a: 10 };
unsafe {
assert_eq!(align_of_val(&u.a), 1);
assert_eq!(size_of_val(&u.a), 1);
assert_eq!(u.a, 10);
}
let u = U16 { b: 11 };
unsafe {
assert_eq!(align_of_val(&u.b), 4);
assert_eq!(size_of_val(&u.b), 4);
assert_eq!(u.b, 11);
}
hybrid::check_hybrid();
}
mod hybrid {
use std::mem::{size_of, align_of};
#[repr(align(16))]
#[derive(Copy, Clone)]
struct S1 {
a: u16,
b: u8,
}
#[repr(align(32))]
union U {
s: S1,
c: u16,
}
#[repr(align(64))]
struct S2 {
d: u8,
u: U,
}
pub fn check_hybrid() {
assert_eq!(align_of::<S1>(), 16);
assert_eq!(size_of::<S1>(), 16);
assert_eq!(align_of::<U>(), 32);
assert_eq!(size_of::<U>(), 32);
assert_eq!(align_of::<S2>(), 64);
assert_eq!(size_of::<S2>(), 64);
}
}
|