| 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 |
// See comment in `include.rs` for why we disable the prelude. |
| 10 |
#![no_implicit_prelude] |
| 11 |
#![allow(warnings)] |
| 12 |
|
| 13 |
include!("include.rs"); |
| 14 |
|
| 15 |
// A union is `imp::FromZeros` if: |
| 16 |
// - all fields are `imp::FromZeros` |
| 17 |
|
| 18 |
#[derive(Clone, Copy, imp::Immutable, imp::FromZeros)] |
| 19 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 20 |
union Zst { |
| 21 |
a: (), |
| 22 |
} |
| 23 |
|
| 24 |
util_assert_impl_all!(Zst: imp::FromZeros); |
| 25 |
|
| 26 |
#[derive(imp::Immutable, imp::FromZeros)] |
| 27 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 28 |
union One { |
| 29 |
a: bool, |
| 30 |
} |
| 31 |
|
| 32 |
util_assert_impl_all!(One: imp::FromZeros); |
| 33 |
|
| 34 |
#[derive(imp::Immutable, imp::FromZeros)] |
| 35 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 36 |
union Two { |
| 37 |
a: bool, |
| 38 |
b: Zst, |
| 39 |
} |
| 40 |
|
| 41 |
util_assert_impl_all!(Two: imp::FromZeros); |
| 42 |
|
| 43 |
#[derive(imp::Immutable, imp::FromZeros)] |
| 44 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 45 |
union TypeParams<'a, T: imp::Copy, I: imp::Iterator> |
| 46 |
where |
| 47 |
I::Item: imp::Copy, |
| 48 |
{ |
| 49 |
a: T, |
| 50 |
c: I::Item, |
| 51 |
d: u8, |
| 52 |
e: imp::PhantomData<&'a [u8]>, |
| 53 |
f: imp::PhantomData<&'static str>, |
| 54 |
g: imp::PhantomData<imp::String>, |
| 55 |
} |
| 56 |
|
| 57 |
util_assert_impl_all!(TypeParams<'static, (), imp::IntoIter<()>>: imp::FromZeros); |
| 58 |
|
| 59 |
// Deriving `imp::FromZeros` should work if the union has bounded parameters. |
| 60 |
|
| 61 |
#[derive(imp::Immutable, imp::FromZeros)] |
| 62 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 63 |
#[repr(C)] |
| 64 |
union WithParams<'a: 'b, 'b: 'a, T: 'a + 'b + imp::FromZeros, const N: usize> |
| 65 |
where |
| 66 |
'a: 'b, |
| 67 |
'b: 'a, |
| 68 |
T: 'a + 'b + imp::Copy + imp::FromZeros, |
| 69 |
{ |
| 70 |
a: [T; N], |
| 71 |
b: imp::PhantomData<&'a &'b ()>, |
| 72 |
} |
| 73 |
|
| 74 |
util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::FromZeros); |
| 75 |
|
| 76 |
#[derive(imp::FromZeros)] |
| 77 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 78 |
#[repr(C)] |
| 79 |
union UnsafeCellUnion { |
| 80 |
a: imp::ManuallyDrop<imp::UnsafeCell<u8>>, |
| 81 |
} |
| 82 |
|
| 83 |
util_assert_impl_all!(UnsafeCellUnion: imp::FromZeros); |