]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/packed-struct-generic-layout.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / test / run-pass / packed-struct-generic-layout.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 use std::mem;
13
14 #[repr(packed)]
15 struct S<T, S> {
16 a: T,
17 b: u8,
18 c: S
19 }
20
21 pub fn main() {
22 unsafe {
23 let s = S { a: 0xff_ff_ff_ffu32, b: 1, c: 0xaa_aa_aa_aa as i32 };
24 let transd : [u8; 9] = mem::transmute(s);
25 // Don't worry about endianness, the numbers are palindromic.
26 assert_eq!(transd,
27 [0xff, 0xff, 0xff, 0xff,
28 1,
29 0xaa, 0xaa, 0xaa, 0xaa]);
30
31
32 let s = S { a: 1u8, b: 2u8, c: 0b10000001_10000001 as i16};
33 let transd : [u8; 4] = mem::transmute(s);
34 // Again, no endianness problems.
35 assert_eq!(transd,
36 [1, 2, 0b10000001, 0b10000001]);
37 }
38 }