]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/union/union-basic.rs
Stabilize unions with `Copy` fields and no destructor
[rust.git] / src / test / run-pass / union / union-basic.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 // aux-build:union.rs
12
13 // FIXME: This test case makes little-endian assumptions.
14 // ignore-s390x
15
16 extern crate union;
17 use std::mem::{size_of, align_of, zeroed};
18
19 union U {
20     a: u8,
21     b: u16
22 }
23
24 fn local() {
25     assert_eq!(size_of::<U>(), 2);
26     assert_eq!(align_of::<U>(), 2);
27
28     let u = U { a: 10 };
29     unsafe {
30         assert_eq!(u.a, 10);
31         let U { a } = u;
32         assert_eq!(a, 10);
33     }
34
35     let mut w = U { b: 0 };
36     unsafe {
37         assert_eq!(w.a, 0);
38         assert_eq!(w.b, 0);
39         w.a = 1;
40         assert_eq!(w.a, 1);
41         assert_eq!(w.b, 1);
42     }
43 }
44
45 fn xcrate() {
46     assert_eq!(size_of::<union::U>(), 2);
47     assert_eq!(align_of::<union::U>(), 2);
48
49     let u = union::U { a: 10 };
50     unsafe {
51         assert_eq!(u.a, 10);
52         let union::U { a } = u;
53         assert_eq!(a, 10);
54     }
55
56     let mut w = union::U { b: 0 };
57     unsafe {
58         assert_eq!(w.a, 0);
59         assert_eq!(w.b, 0);
60         w.a = 1;
61         assert_eq!(w.a, 1);
62         assert_eq!(w.b, 1);
63     }
64 }
65
66 fn main() {
67     local();
68     xcrate();
69 }