]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/packed-struct-size.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / packed-struct-size.rs
1 // Copyright 2013 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
12
13 use std::mem;
14
15 #[repr(packed)]
16 struct S4 {
17     a: u8,
18     b: [u8;  3],
19 }
20
21 #[repr(packed)]
22 struct S5 {
23     a: u8,
24     b: u32
25 }
26
27 #[repr(packed)]
28 struct S13 {
29     a: i64,
30     b: f32,
31     c: u8,
32 }
33
34 enum Foo {
35     Bar = 1,
36     Baz = 2
37 }
38
39 #[repr(packed)]
40 struct S3_Foo {
41     a: u8,
42     b: u16,
43     c: Foo
44 }
45
46 #[repr(packed)]
47 struct S7_Option {
48     a: f32,
49     b: u8,
50     c: u16,
51     d: Option<Box<f64>>
52 }
53
54 // Placing packed structs in statics should work
55 static TEST_S4: S4 = S4 { a: 1, b: [2, 3, 4] };
56 static TEST_S5: S5 = S5 { a: 3, b: 67 };
57 static TEST_S3_Foo: S3_Foo = S3_Foo { a: 1, b: 2, c: Foo::Baz };
58
59
60 pub fn main() {
61     assert_eq!(mem::size_of::<S4>(), 4);
62     assert_eq!(mem::size_of::<S5>(), 5);
63     assert_eq!(mem::size_of::<S13>(), 13);
64     assert_eq!(mem::size_of::<S3_Foo>(), 3 + mem::size_of::<Foo>());
65     assert_eq!(mem::size_of::<S7_Option>(), 7 + mem::size_of::<Option<Box<f64>>>());
66 }