]>
Commit | Line | Data |
---|---|---|
3b2f2976 XL |
1 | // Copyright 2017 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 | ||
b7449926 XL |
11 | // run-pass |
12 | #![feature(min_const_fn)] | |
3b2f2976 XL |
13 | |
14 | use std::mem; | |
15 | ||
16 | // Get around the limitations of CTFE in today's Rust. | |
17 | const fn choice_u64(c: bool, a: u64, b: u64) -> u64 { | |
18 | (-(c as i64) as u64) & a | (-(!c as i64) as u64) & b | |
19 | } | |
20 | ||
21 | const fn max_usize(a: usize, b: usize) -> usize { | |
22 | choice_u64(a > b, a as u64, b as u64) as usize | |
23 | } | |
24 | ||
25 | const fn align_to(size: usize, align: usize) -> usize { | |
26 | (size + (align - 1)) & !(align - 1) | |
27 | } | |
28 | ||
29 | const fn packed_union_size_of<A, B>() -> usize { | |
30 | max_usize(mem::size_of::<A>(), mem::size_of::<B>()) | |
31 | } | |
32 | ||
33 | const fn union_align_of<A, B>() -> usize { | |
34 | max_usize(mem::align_of::<A>(), mem::align_of::<B>()) | |
35 | } | |
36 | ||
37 | const fn union_size_of<A, B>() -> usize { | |
38 | align_to(packed_union_size_of::<A, B>(), union_align_of::<A, B>()) | |
39 | } | |
40 | ||
41 | macro_rules! fake_union { | |
42 | ($name:ident { $a:ty, $b:ty }) => ( | |
43 | struct $name { | |
44 | _align: ([$a; 0], [$b; 0]), | |
45 | _bytes: [u8; union_size_of::<$a, $b>()] | |
46 | } | |
47 | ) | |
48 | } | |
49 | ||
50 | // Check that we can (poorly) emulate unions by | |
51 | // calling size_of and align_of at compile-time. | |
52 | fake_union!(U { u16, [u8; 3] }); | |
53 | ||
54 | fn test(u: U) { | |
55 | assert_eq!(mem::size_of_val(&u._bytes), 4); | |
56 | } | |
57 | ||
58 | fn main() { | |
59 | assert_eq!(mem::size_of::<U>(), 4); | |
60 | assert_eq!(mem::align_of::<U>(), 2); | |
61 | } |