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 // A union is `FromZeroes` if: 19 // - all fields are `FromZeroes` 20 21 #[derive(Clone, Copy, FromZeroes)] 22 union Zst { 23 a: (), 24 } 25 26 assert_impl_all!(Zst: FromZeroes); 27 28 #[derive(FromZeroes)] 29 union One { 30 a: bool, 31 } 32 33 assert_impl_all!(One: FromZeroes); 34 35 #[derive(FromZeroes)] 36 union Two { 37 a: bool, 38 b: Zst, 39 } 40 41 assert_impl_all!(Two: FromZeroes); 42 43 #[derive(FromZeroes)] 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<()>>: FromZeroes); 57 58 // Deriving `FromZeroes` should work if the union has bounded parameters. 59 60 #[derive(FromZeroes)] 61 #[repr(C)] 62 union WithParams<'a: 'b, 'b: 'a, const N: usize, T: 'a + 'b + FromZeroes> 63 where 64 'a: 'b, 65 'b: 'a, 66 T: 'a + 'b + Copy + FromZeroes, 67 { 68 a: [T; N], 69 b: PhantomData<&'a &'b ()>, 70 } 71 72 assert_impl_all!(WithParams<'static, 'static, 42, u8>: FromZeroes); 73