|
| 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 | + |
| 12 | +use std::mem; |
| 13 | + |
| 14 | +// Univariant C-like enum |
| 15 | +#[repr(i32)] |
| 16 | +enum Univariant { |
| 17 | + X = 17 |
| 18 | +} |
| 19 | + |
| 20 | +#[repr(u16)] |
| 21 | +enum UnivariantWithoutDescr { |
| 22 | + Y |
| 23 | +} |
| 24 | + |
| 25 | +pub fn main() { |
| 26 | + { |
| 27 | + assert_eq!(4, mem::size_of::<Univariant>()); |
| 28 | + assert_eq!(17, Univariant::X as i32); |
| 29 | + |
| 30 | + let enums: &[Univariant] = |
| 31 | + &[Univariant::X, Univariant::X, Univariant::X]; |
| 32 | + let ints: &[i32] = unsafe { mem::transmute(enums) }; |
| 33 | + // check it has the same memory layout as i32 |
| 34 | + assert_eq!(&[17, 17, 17], ints); |
| 35 | + } |
| 36 | + |
| 37 | + { |
| 38 | + assert_eq!(2, mem::size_of::<UnivariantWithoutDescr>()); |
| 39 | + let descr = UnivariantWithoutDescr::Y as u16; |
| 40 | + |
| 41 | + let enums: &[UnivariantWithoutDescr] = |
| 42 | + &[UnivariantWithoutDescr::Y, UnivariantWithoutDescr::Y, UnivariantWithoutDescr::Y]; |
| 43 | + let ints: &[u16] = unsafe { mem::transmute(enums) }; |
| 44 | + // check it has the same memory layout as u16 |
| 45 | + assert_eq!(&[descr, descr, descr], ints); |
| 46 | + } |
| 47 | +} |
0 commit comments