| 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::IntoBytes` if: |
| 16 |
// - all fields are `imp::IntoBytes` |
| 17 |
// - `repr(C)` or `repr(transparent)` and |
| 18 |
// - no padding (size of union equals size of each field type) |
| 19 |
// - `repr(packed)` |
| 20 |
|
| 21 |
#[derive(imp::IntoBytes, Clone, Copy)] |
| 22 |
#[repr(C)] |
| 23 |
union CZst { |
| 24 |
a: (), |
| 25 |
} |
| 26 |
|
| 27 |
util_assert_impl_all!(CZst: imp::IntoBytes); |
| 28 |
|
| 29 |
#[derive(imp::IntoBytes)] |
| 30 |
#[repr(C)] |
| 31 |
union C { |
| 32 |
a: u8, |
| 33 |
b: u8, |
| 34 |
} |
| 35 |
|
| 36 |
util_assert_impl_all!(C: imp::IntoBytes); |
| 37 |
|
| 38 |
// Transparent unions are unstable; see issue #60405 |
| 39 |
// <https://github.com/rust-lang/rust/issues/60405> for more information. |
| 40 |
|
| 41 |
// #[derive(imp::IntoBytes)] |
| 42 |
// #[repr(transparent)] |
| 43 |
// union Transparent { |
| 44 |
// a: u8, |
| 45 |
// b: CZst, |
| 46 |
// } |
| 47 |
|
| 48 |
// is_as_bytes!(Transparent); |
| 49 |
|
| 50 |
#[derive(imp::IntoBytes)] |
| 51 |
#[repr(C, packed)] |
| 52 |
union CZstPacked { |
| 53 |
a: (), |
| 54 |
} |
| 55 |
|
| 56 |
util_assert_impl_all!(CZstPacked: imp::IntoBytes); |
| 57 |
|
| 58 |
#[derive(imp::IntoBytes)] |
| 59 |
#[repr(C, packed)] |
| 60 |
union CPacked { |
| 61 |
a: u8, |
| 62 |
b: i8, |
| 63 |
} |
| 64 |
|
| 65 |
util_assert_impl_all!(CPacked: imp::IntoBytes); |
| 66 |
|
| 67 |
#[derive(imp::IntoBytes)] |
| 68 |
#[repr(C, packed)] |
| 69 |
union CMultibytePacked { |
| 70 |
a: i32, |
| 71 |
b: u32, |
| 72 |
c: f32, |
| 73 |
} |
| 74 |
|
| 75 |
util_assert_impl_all!(CMultibytePacked: imp::IntoBytes); |