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
66
67
68
69
|
// compile-flags: -Z print-type-sizes
// build-pass
// ignore-pass
// ^-- needed because `--pass check` does not emit the output needed.
// FIXME: consider using an attribute instead of side-effects.
// This file illustrates how packing is handled; it should cause
// the elimination of padding that would normally be introduced
// to satisfy alignment desirata.
//
// It avoids using u64/i64 because on some targets that is only 4-byte
// aligned (while on most it is 8-byte aligned) and so the resulting
// padding and overall computed sizes can be quite different.
#![allow(dead_code)]
#![feature(start)]
#[derive(Default)]
#[repr(packed)]
struct Packed1 {
a: u8,
b: u8,
g: i32,
c: u8,
h: i16,
d: u8,
}
#[derive(Default)]
#[repr(packed(2))]
struct Packed2 {
a: u8,
b: u8,
g: i32,
c: u8,
h: i16,
d: u8,
}
#[derive(Default)]
#[repr(packed(2))]
#[repr(C)]
struct Packed2C {
a: u8,
b: u8,
g: i32,
c: u8,
h: i16,
d: u8,
}
#[derive(Default)]
struct Padded {
a: u8,
b: u8,
g: i32,
c: u8,
h: i16,
d: u8,
}
#[start]
fn start(_: isize, _: *const *const u8) -> isize {
let _c: Packed1 = Default::default();
let _d: Packed2 = Default::default();
let _e: Packed2C = Default::default();
let _f: Padded = Default::default();
0
}
|