File: align-enum.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (54 lines) | stat: -rw-r--r-- 1,115 bytes parent folder | download | duplicates (5)
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
//@ run-pass
#![allow(dead_code)]

use std::mem;

// Raising alignment
#[repr(align(16))]
enum Align16 {
    Foo { foo: u32 },
    Bar { bar: u32 },
}

// Raise alignment by maximum
#[repr(align(1), align(16))]
#[repr(align(32))]
#[repr(align(4))]
enum Align32 {
    Foo,
    Bar,
}

// Not reducing alignment
#[repr(align(4))]
enum AlsoAlign16 {
    Foo { limb_with_align16: Align16 },
    Bar,
}

// No niche for discriminant when used as limb
#[repr(align(16))]
struct NoNiche16(u64, u64);

// Discriminant will require extra space, but enum needs to stay compatible
// with alignment 16
#[repr(align(1))]
enum AnotherAlign16 {
    Foo { limb_with_noniche16: NoNiche16 },
    Bar,
    Baz,
}

fn main() {
    assert_eq!(mem::align_of::<Align16>(), 16);
    assert_eq!(mem::size_of::<Align16>(), 16);

    assert_eq!(mem::align_of::<Align32>(), 32);
    assert_eq!(mem::size_of::<Align32>(), 32);

    assert_eq!(mem::align_of::<AlsoAlign16>(), 16);
    assert_eq!(mem::size_of::<AlsoAlign16>(), 16);

    assert_eq!(mem::align_of::<AnotherAlign16>(), 16);
    assert_eq!(mem::size_of::<AnotherAlign16>(), 32);
}