88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11+ // aux-build:union.rs
12+
1113#![ feature( untagged_unions) ]
1214
15+ extern crate union;
1316use std:: mem:: { size_of, align_of, zeroed} ;
1417
1518union U {
1619 a : u8 ,
20+ b : u16
1721}
1822
19- union U64 {
20- a : u64 ,
21- }
23+ fn local ( ) {
24+ assert_eq ! ( size_of :: < U > ( ) , 2 ) ;
25+ assert_eq ! ( align_of :: < U > ( ) , 2 ) ;
2226
23- union W {
24- a : u8 ,
25- b : u64 ,
26- }
27+ let u = U { a : 10 } ;
28+ unsafe {
29+ assert_eq ! ( u. a, 10 ) ;
30+ let U { a } = u;
31+ assert_eq ! ( a, 10 ) ;
32+ }
2733
28- #[ repr( C ) ]
29- union Y {
30- f1 : u16 ,
31- f2 : [ u8 ; 4 ] ,
34+ let mut w = U { b : 0 } ;
35+ unsafe {
36+ assert_eq ! ( w. a, 0 ) ;
37+ assert_eq ! ( w. b, 0 ) ;
38+ w. a = 1 ;
39+ assert_eq ! ( w. a, 1 ) ;
40+ assert_eq ! ( w. b, 1 ) ;
41+ }
3242}
3343
34- fn main ( ) {
35- assert_eq ! ( size_of:: <U >( ) , 1 ) ;
36- assert_eq ! ( size_of:: <U64 >( ) , 8 ) ;
37- assert_eq ! ( size_of:: <W >( ) , 8 ) ;
38- assert_eq ! ( align_of:: <U >( ) , 1 ) ;
39- assert_eq ! ( align_of:: <U64 >( ) , align_of:: <u64 >( ) ) ;
40- assert_eq ! ( align_of:: <W >( ) , align_of:: <u64 >( ) ) ;
41- assert_eq ! ( size_of:: <Y >( ) , 4 ) ;
42- assert_eq ! ( align_of:: <Y >( ) , 2 ) ;
44+ fn xcrate ( ) {
45+ assert_eq ! ( size_of:: <union :: U >( ) , 2 ) ;
46+ assert_eq ! ( align_of:: <union :: U >( ) , 2 ) ;
4347
44- let u = U { a : 10 } ;
48+ let u = union :: U { a : 10 } ;
4549 unsafe {
4650 assert_eq ! ( u. a, 10 ) ;
47- let U { a } = u;
51+ let union :: U { a } = u;
4852 assert_eq ! ( a, 10 ) ;
4953 }
5054
51- let mut w = W { b : 0 } ;
55+ let mut w = union :: U { b : 0 } ;
5256 unsafe {
5357 assert_eq ! ( w. a, 0 ) ;
5458 assert_eq ! ( w. b, 0 ) ;
@@ -57,3 +61,8 @@ fn main() {
5761 assert_eq ! ( w. b, 1 ) ;
5862 }
5963}
64+
65+ fn main ( ) {
66+ local ( ) ;
67+ xcrate ( ) ;
68+ }
0 commit comments