]> git.lizzy.rs Git - rust.git/blob - src/test/ui/print_type_sizes/packed.rs
Rollup merge of #49871 - SimonSapin:int-bytes, r=sfackler
[rust.git] / src / test / ui / print_type_sizes / packed.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // compile-flags: -Z print-type-sizes
12 // compile-pass
13
14 // This file illustrates how packing is handled; it should cause
15 // the elimination of padding that would normally be introduced
16 // to satisfy alignment desirata.
17 //
18 // It avoids using u64/i64 because on some targets that is only 4-byte
19 // aligned (while on most it is 8-byte aligned) and so the resulting
20 // padding and overall computed sizes can be quite different.
21
22 #![allow(dead_code)]
23 #![feature(start)]
24 #![feature(repr_packed)]
25
26 #[derive(Default)]
27 #[repr(packed)]
28 struct Packed1 {
29     a: u8,
30     b: u8,
31     g: i32,
32     c: u8,
33     h: i16,
34     d: u8,
35 }
36
37 #[derive(Default)]
38 #[repr(packed(2))]
39 struct Packed2 {
40     a: u8,
41     b: u8,
42     g: i32,
43     c: u8,
44     h: i16,
45     d: u8,
46 }
47
48 #[derive(Default)]
49 #[repr(packed(2))]
50 #[repr(C)]
51 struct Packed2C {
52     a: u8,
53     b: u8,
54     g: i32,
55     c: u8,
56     h: i16,
57     d: u8,
58 }
59
60 #[derive(Default)]
61 struct Padded {
62     a: u8,
63     b: u8,
64     g: i32,
65     c: u8,
66     h: i16,
67     d: u8,
68 }
69
70 #[start]
71 fn start(_: isize, _: *const *const u8) -> isize {
72     let _c: Packed1 = Default::default();
73     let _d: Packed2 = Default::default();
74     let _e: Packed2C = Default::default();
75     let _f: Padded = Default::default();
76     0
77 }