| 1 |
// Copyright 2019 The Fuchsia Authors |
| 2 |
// |
| 3 |
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 |
| 4 |
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT |
| 5 |
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. |
| 6 |
// This file may not be copied, modified, or distributed except according to |
| 7 |
// those terms. |
| 8 |
|
| 9 |
// See comment in `include.rs` for why we disable the prelude. |
| 10 |
#![no_implicit_prelude] |
| 11 |
#![allow(warnings)] |
| 12 |
|
| 13 |
include!("include.rs"); |
| 14 |
|
| 15 |
// A union is `imp::FromBytes` if: |
| 16 |
// - all fields are `imp::FromBytes` |
| 17 |
|
| 18 |
#[derive(Clone, Copy, imp::Immutable, imp::FromBytes)] |
| 19 |
union Zst { |
| 20 |
a: (), |
| 21 |
} |
| 22 |
|
| 23 |
util_assert_impl_all!(Zst: imp::FromBytes); |
| 24 |
test_trivial_is_bit_valid!(Zst => test_zst_trivial_is_bit_valid); |
| 25 |
|
| 26 |
#[derive(imp::Immutable, imp::FromBytes)] |
| 27 |
union One { |
| 28 |
a: u8, |
| 29 |
} |
| 30 |
|
| 31 |
util_assert_impl_all!(One: imp::FromBytes); |
| 32 |
test_trivial_is_bit_valid!(One => test_one_trivial_is_bit_valid); |
| 33 |
|
| 34 |
#[derive(imp::Immutable, imp::FromBytes)] |
| 35 |
union Two { |
| 36 |
a: u8, |
| 37 |
b: Zst, |
| 38 |
} |
| 39 |
|
| 40 |
util_assert_impl_all!(Two: imp::FromBytes); |
| 41 |
test_trivial_is_bit_valid!(Two => test_two_trivial_is_bit_valid); |
| 42 |
|
| 43 |
#[derive(imp::Immutable, imp::FromBytes)] |
| 44 |
union TypeParams<'a, T: imp::Copy, I: imp::Iterator> |
| 45 |
where |
| 46 |
I::Item: imp::Copy, |
| 47 |
{ |
| 48 |
a: T, |
| 49 |
c: I::Item, |
| 50 |
d: u8, |
| 51 |
e: imp::PhantomData<&'a [u8]>, |
| 52 |
f: imp::PhantomData<&'static str>, |
| 53 |
g: imp::PhantomData<imp::String>, |
| 54 |
} |
| 55 |
|
| 56 |
util_assert_impl_all!(TypeParams<'static, (), imp::IntoIter<()>>: imp::FromBytes); |
| 57 |
test_trivial_is_bit_valid!(TypeParams<'static, (), imp::IntoIter<()>> => test_type_params_trivial_is_bit_valid); |
| 58 |
|
| 59 |
// Deriving `imp::FromBytes` should work if the union has bounded parameters. |
| 60 |
|
| 61 |
#[derive(imp::Immutable, imp::FromBytes)] |
| 62 |
#[repr(C)] |
| 63 |
union WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::FromBytes, const N: usize> |
| 64 |
where |
| 65 |
'a: 'b, |
| 66 |
'b: 'a, |
| 67 |
T: 'a + 'b + imp::Copy + imp::FromBytes, |
| 68 |
{ |
| 69 |
a: [T; N], |
| 70 |
b: imp::PhantomData<&'a &'b ()>, |
| 71 |
} |
| 72 |
|
| 73 |
util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::FromBytes); |
| 74 |
test_trivial_is_bit_valid!(WithParams<'static, 'static, u8, 42> => test_with_params_trivial_is_bit_valid); |