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