]> git.lizzy.rs Git - rust.git/blob - tests/ui/packed/packed-struct-vec.rs
Rollup merge of #106958 - jyn514:labels, r=m-ou-se
[rust.git] / tests / ui / packed / packed-struct-vec.rs
1 // run-pass
2
3 use std::fmt;
4 use std::mem;
5
6 #[repr(packed)]
7 #[derive(Copy, Clone)]
8 struct Foo1 {
9     bar: u8,
10     baz: u64
11 }
12
13 impl PartialEq for Foo1 {
14     fn eq(&self, other: &Foo1) -> bool {
15         self.bar == other.bar && self.baz == other.baz
16     }
17 }
18
19 impl fmt::Debug for Foo1 {
20     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21         let bar = self.bar;
22         let baz = self.baz;
23
24         f.debug_struct("Foo1")
25             .field("bar", &bar)
26             .field("baz", &baz)
27             .finish()
28     }
29 }
30
31 #[repr(packed(2))]
32 #[derive(Copy, Clone)]
33 struct Foo2 {
34     bar: u8,
35     baz: u64
36 }
37
38 impl PartialEq for Foo2 {
39     fn eq(&self, other: &Foo2) -> bool {
40         self.bar == other.bar && self.baz == other.baz
41     }
42 }
43
44 impl fmt::Debug for Foo2 {
45     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46         let bar = self.bar;
47         let baz = self.baz;
48
49         f.debug_struct("Foo2")
50             .field("bar", &bar)
51             .field("baz", &baz)
52             .finish()
53     }
54 }
55
56 #[repr(C, packed(4))]
57 #[derive(Copy, Clone)]
58 struct Foo4C {
59     bar: u8,
60     baz: u64
61 }
62
63 impl PartialEq for Foo4C {
64     fn eq(&self, other: &Foo4C) -> bool {
65         self.bar == other.bar && self.baz == other.baz
66     }
67 }
68
69 impl fmt::Debug for Foo4C {
70     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71         let bar = self.bar;
72         let baz = self.baz;
73
74         f.debug_struct("Foo4C")
75             .field("bar", &bar)
76             .field("baz", &baz)
77             .finish()
78     }
79 }
80
81 pub fn main() {
82     let foo1s = [Foo1 { bar: 1, baz: 2 }; 10];
83
84     assert_eq!(mem::align_of::<[Foo1; 10]>(), 1);
85     assert_eq!(mem::size_of::<[Foo1; 10]>(), 90);
86
87     for i in 0..10 {
88         assert_eq!(foo1s[i], Foo1 { bar: 1, baz: 2});
89     }
90
91     for &foo in &foo1s {
92         assert_eq!(foo, Foo1 { bar: 1, baz: 2 });
93     }
94
95     let foo2s = [Foo2 { bar: 1, baz: 2 }; 10];
96
97     assert_eq!(mem::align_of::<[Foo2; 10]>(), 2);
98     assert_eq!(mem::size_of::<[Foo2; 10]>(), 100);
99
100     for i in 0..10 {
101         assert_eq!(foo2s[i], Foo2 { bar: 1, baz: 2});
102     }
103
104     for &foo in &foo2s {
105         assert_eq!(foo, Foo2 { bar: 1, baz: 2 });
106     }
107
108     let foo4s = [Foo4C { bar: 1, baz: 2 }; 10];
109
110     assert_eq!(mem::align_of::<[Foo4C; 10]>(), 4);
111     assert_eq!(mem::size_of::<[Foo4C; 10]>(), 120);
112
113     for i in 0..10 {
114         assert_eq!(foo4s[i], Foo4C { bar: 1, baz: 2});
115     }
116
117     for &foo in &foo4s {
118         assert_eq!(foo, Foo4C { bar: 1, baz: 2 });
119     }
120 }