1 // Copyright 2019 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #![allow(warnings)] 6 7 use std::{marker::PhantomData, option::IntoIter}; 8 9 use zerocopy::FromBytes; 10 11 struct IsFromBytes<T: FromBytes>(T); 12 13 // Fail compilation if `$ty: !FromBytes`. 14 macro_rules! is_from_bytes { 15 ($ty:ty) => { 16 const _: () = { 17 let _: IsFromBytes<$ty>; 18 }; 19 }; 20 } 21 22 // A union is `FromBytes` if: 23 // - all fields are `FromBytes` 24 25 #[derive(Clone, Copy, FromBytes)] 26 union Zst { 27 a: (), 28 } 29 30 is_from_bytes!(Zst); 31 32 #[derive(FromBytes)] 33 union One { 34 a: u8, 35 } 36 37 is_from_bytes!(One); 38 39 #[derive(FromBytes)] 40 union Two { 41 a: u8, 42 b: Zst, 43 } 44 45 is_from_bytes!(Two); 46 47 #[derive(FromBytes)] 48 union TypeParams<'a, T: Copy, I: Iterator> 49 where 50 I::Item: Copy, 51 { 52 a: T, 53 c: I::Item, 54 d: u8, 55 e: PhantomData<&'a [u8]>, 56 f: PhantomData<&'static str>, 57 g: PhantomData<String>, 58 } 59 60 is_from_bytes!(TypeParams<'static, (), IntoIter<()>>); 61