| 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 |
#![allow(warnings)] |
| 10 |
|
| 11 |
use std::{marker::PhantomData, option::IntoIter}; |
| 12 |
|
| 13 |
use { |
| 14 |
static_assertions::assert_impl_all, |
| 15 |
zerocopy::{FromBytes, FromZeroes}, |
| 16 |
}; |
| 17 |
|
| 18 |
// A union is `FromBytes` if: |
| 19 |
// - all fields are `FromBytes` |
| 20 |
|
| 21 |
#[derive(Clone, Copy, FromZeroes, FromBytes)] |
| 22 |
union Zst { |
| 23 |
a: (), |
| 24 |
} |
| 25 |
|
| 26 |
assert_impl_all!(Zst: FromBytes); |
| 27 |
|
| 28 |
#[derive(FromZeroes, FromBytes)] |
| 29 |
union One { |
| 30 |
a: u8, |
| 31 |
} |
| 32 |
|
| 33 |
assert_impl_all!(One: FromBytes); |
| 34 |
|
| 35 |
#[derive(FromZeroes, FromBytes)] |
| 36 |
union Two { |
| 37 |
a: u8, |
| 38 |
b: Zst, |
| 39 |
} |
| 40 |
|
| 41 |
assert_impl_all!(Two: FromBytes); |
| 42 |
|
| 43 |
#[derive(FromZeroes, FromBytes)] |
| 44 |
union TypeParams<'a, T: Copy, I: Iterator> |
| 45 |
where |
| 46 |
I::Item: Copy, |
| 47 |
{ |
| 48 |
a: T, |
| 49 |
c: I::Item, |
| 50 |
d: u8, |
| 51 |
e: PhantomData<&'a [u8]>, |
| 52 |
f: PhantomData<&'static str>, |
| 53 |
g: PhantomData<String>, |
| 54 |
} |
| 55 |
|
| 56 |
assert_impl_all!(TypeParams<'static, (), IntoIter<()>>: FromBytes); |
| 57 |
|
| 58 |
// Deriving `FromBytes` should work if the union has bounded parameters. |
| 59 |
|
| 60 |
#[derive(FromZeroes, FromBytes)] |
| 61 |
#[repr(C)] |
| 62 |
union WithParams<'a: 'b, 'b: 'a, const N: usize, T: 'a + 'b + FromBytes> |
| 63 |
where |
| 64 |
'a: 'b, |
| 65 |
'b: 'a, |
| 66 |
T: 'a + 'b + Copy + FromBytes, |
| 67 |
{ |
| 68 |
a: [T; N], |
| 69 |
b: PhantomData<&'a &'b ()>, |
| 70 |
} |
| 71 |
|
| 72 |
assert_impl_all!(WithParams<'static, 'static, 42, u8>: FromBytes); |