• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // run-pass
2 
3 use std::mem;
4 
5 // Univariant C-like enum
6 #[repr(i32)]
7 enum Univariant {
8     X = 17
9 }
10 
11 #[repr(u16)]
12 enum UnivariantWithoutDescr {
13     Y
14 }
15 
16 #[repr(u8)]
17 enum UnivariantWithData {
18     Z(u8),
19 }
20 
main()21 pub fn main() {
22     {
23         assert_eq!(4, mem::size_of::<Univariant>());
24         assert_eq!(17, Univariant::X as i32);
25 
26         let enums: &[Univariant] =
27             &[Univariant::X, Univariant::X, Univariant::X];
28         let ints: &[i32] = unsafe { mem::transmute(enums) };
29         // check it has the same memory layout as i32
30         assert_eq!(&[17, 17, 17], ints);
31     }
32 
33     {
34         assert_eq!(2, mem::size_of::<UnivariantWithoutDescr>());
35         let descr = UnivariantWithoutDescr::Y as u16;
36 
37         let enums: &[UnivariantWithoutDescr] =
38             &[UnivariantWithoutDescr::Y, UnivariantWithoutDescr::Y, UnivariantWithoutDescr::Y];
39         let ints: &[u16] = unsafe { mem::transmute(enums) };
40         // check it has the same memory layout as u16
41         assert_eq!(&[descr, descr, descr], ints);
42     }
43 
44     {
45         assert_eq!(2, mem::size_of::<UnivariantWithData>());
46 
47         match UnivariantWithData::Z(4) {
48             UnivariantWithData::Z(x) => assert_eq!(x, 4),
49         }
50     }
51 }
52