6 // Get around the limitations of CTFE in today's Rust.
7 const fn choice_u64(c: bool, a: u64, b: u64) -> u64 {
8 (-(c as i64) as u64) & a | (-(!c as i64) as u64) & b
11 const fn max_usize(a: usize, b: usize) -> usize {
12 choice_u64(a > b, a as u64, b as u64) as usize
15 const fn align_to(size: usize, align: usize) -> usize {
16 (size + (align - 1)) & !(align - 1)
19 const fn packed_union_size_of<A, B>() -> usize {
20 max_usize(mem::size_of::<A>(), mem::size_of::<B>())
23 const fn union_align_of<A, B>() -> usize {
24 max_usize(mem::align_of::<A>(), mem::align_of::<B>())
27 const fn union_size_of<A, B>() -> usize {
28 align_to(packed_union_size_of::<A, B>(), union_align_of::<A, B>())
31 macro_rules! fake_union {
32 ($name:ident { $a:ty, $b:ty }) => (
34 _align: ([$a; 0], [$b; 0]),
35 _bytes: [u8; union_size_of::<$a, $b>()]
40 // Check that we can (poorly) emulate unions by
41 // calling size_of and align_of at compile-time.
42 fake_union!(U { u16, [u8; 3] });
45 assert_eq!(mem::size_of_val(&u._bytes), 4);
49 assert_eq!(mem::size_of::<U>(), 4);
50 assert_eq!(mem::align_of::<U>(), 2);