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