| 1 |
// Copyright 2024 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 |
// Make sure that the derive macros will respect the |
| 10 |
// `#[zerocopy(crate = "...")]` attribute when renaming the crate. |
| 11 |
|
| 12 |
// See comment in `include.rs` for why we disable the prelude. |
| 13 |
#![no_implicit_prelude] |
| 14 |
#![allow(warnings)] |
| 15 |
|
| 16 |
include!("include.rs"); |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn test_gen_custom_zerocopy() { |
| 20 |
#[derive( |
| 21 |
imp::ByteEq, |
| 22 |
imp::ByteHash, |
| 23 |
imp::IntoBytes, |
| 24 |
imp::FromBytes, |
| 25 |
imp::Unaligned, |
| 26 |
imp::Immutable, |
| 27 |
imp::KnownLayout, |
| 28 |
)] |
| 29 |
#[zerocopy(crate = "fake_zerocopy")] |
| 30 |
#[repr(packed)] |
| 31 |
struct SomeStruct { |
| 32 |
a: u16, |
| 33 |
b: u32, |
| 34 |
} |
| 35 |
|
| 36 |
impl AssertNotZerocopyIntoBytes for SomeStruct {} |
| 37 |
impl AssertNotZerocopyFromBytes for SomeStruct {} |
| 38 |
impl AssertNotZerocopyUnaligned for SomeStruct {} |
| 39 |
impl AssertNotZerocopyImmutable for SomeStruct {} |
| 40 |
impl AssertNotZerocopyKnownLayout for SomeStruct {} |
| 41 |
|
| 42 |
fake_zerocopy::assert::<SomeStruct>(); |
| 43 |
} |
| 44 |
|
| 45 |
mod fake_zerocopy { |
| 46 |
use ::std::{io, ptr::NonNull, unimplemented}; |
| 47 |
|
| 48 |
pub use super::imp::*; |
| 49 |
|
| 50 |
pub fn assert<T>() |
| 51 |
where |
| 52 |
T: IntoBytes + FromBytes + Unaligned + Immutable, |
| 53 |
{ |
| 54 |
} |
| 55 |
|
| 56 |
pub unsafe trait IntoBytes { |
| 57 |
fn only_derive_is_allowed_to_implement_this_trait() |
| 58 |
where |
| 59 |
Self: Sized; |
| 60 |
|
| 61 |
fn as_bytes(&self) -> &[u8] |
| 62 |
where |
| 63 |
Self: Immutable, |
| 64 |
{ |
| 65 |
unimplemented!() |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
pub unsafe trait FromBytes: FromZeros { |
| 70 |
fn only_derive_is_allowed_to_implement_this_trait() |
| 71 |
where |
| 72 |
Self: Sized; |
| 73 |
} |
| 74 |
|
| 75 |
pub unsafe trait Unaligned { |
| 76 |
fn only_derive_is_allowed_to_implement_this_trait() |
| 77 |
where |
| 78 |
Self: Sized; |
| 79 |
} |
| 80 |
|
| 81 |
pub unsafe trait Immutable { |
| 82 |
fn only_derive_is_allowed_to_implement_this_trait() |
| 83 |
where |
| 84 |
Self: Sized; |
| 85 |
} |
| 86 |
|
| 87 |
pub unsafe trait KnownLayout { |
| 88 |
fn only_derive_is_allowed_to_implement_this_trait() |
| 89 |
where |
| 90 |
Self: Sized; |
| 91 |
|
| 92 |
type PointerMetadata: PointerMetadata; |
| 93 |
|
| 94 |
type MaybeUninit: ?Sized + KnownLayout<PointerMetadata = Self::PointerMetadata>; |
| 95 |
|
| 96 |
const LAYOUT: DstLayout; |
| 97 |
|
| 98 |
fn raw_from_ptr_len(bytes: NonNull<u8>, meta: Self::PointerMetadata) -> NonNull<Self>; |
| 99 |
|
| 100 |
fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata; |
| 101 |
} |
| 102 |
|
| 103 |
macro_rules! impl_ty { |
| 104 |
($ty:ty $(as $generic:ident)?) => { |
| 105 |
unsafe impl$(<$generic: IntoBytes>)? IntoBytes for $ty { |
| 106 |
fn only_derive_is_allowed_to_implement_this_trait() |
| 107 |
where |
| 108 |
Self: Sized, |
| 109 |
{ |
| 110 |
unimplemented!() |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
unsafe impl$(<$generic: FromBytes>)? FromBytes for $ty { |
| 115 |
fn only_derive_is_allowed_to_implement_this_trait() |
| 116 |
where |
| 117 |
Self: Sized, |
| 118 |
{ |
| 119 |
unimplemented!() |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
unsafe impl$(<$generic: Unaligned>)? Unaligned for $ty { |
| 124 |
fn only_derive_is_allowed_to_implement_this_trait() |
| 125 |
where |
| 126 |
Self: Sized, |
| 127 |
{ |
| 128 |
unimplemented!() |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
unsafe impl$(<$generic: Immutable>)? Immutable for $ty { |
| 133 |
fn only_derive_is_allowed_to_implement_this_trait() |
| 134 |
where |
| 135 |
Self: Sized, |
| 136 |
{ |
| 137 |
unimplemented!() |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
unsafe impl$(<$generic: KnownLayout>)? KnownLayout for $ty { |
| 142 |
fn only_derive_is_allowed_to_implement_this_trait() |
| 143 |
where |
| 144 |
Self: Sized, |
| 145 |
{ |
| 146 |
unimplemented!() |
| 147 |
} |
| 148 |
|
| 149 |
type PointerMetadata = (); |
| 150 |
|
| 151 |
type MaybeUninit = (); |
| 152 |
|
| 153 |
const LAYOUT: DstLayout = DstLayout::new_zst(None); |
| 154 |
|
| 155 |
fn raw_from_ptr_len( |
| 156 |
bytes: NonNull<u8>, |
| 157 |
meta: Self::PointerMetadata, |
| 158 |
) -> NonNull<Self> { |
| 159 |
unimplemented!() |
| 160 |
} |
| 161 |
|
| 162 |
fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata { |
| 163 |
unimplemented!() |
| 164 |
} |
| 165 |
} |
| 166 |
}; |
| 167 |
} |
| 168 |
|
| 169 |
impl_ty!(()); |
| 170 |
impl_ty!(u16); |
| 171 |
impl_ty!(u32); |
| 172 |
impl_ty!([T] as T); |
| 173 |
impl_ty!(::std::mem::MaybeUninit<T> as T); |
| 174 |
} |
| 175 |
|
| 176 |
pub trait AssertNotZerocopyIntoBytes {} |
| 177 |
impl<T: imp::IntoBytes> AssertNotZerocopyIntoBytes for T {} |
| 178 |
|
| 179 |
pub trait AssertNotZerocopyFromBytes {} |
| 180 |
impl<T: imp::FromBytes> AssertNotZerocopyFromBytes for T {} |
| 181 |
|
| 182 |
pub trait AssertNotZerocopyUnaligned {} |
| 183 |
impl<T: imp::Unaligned> AssertNotZerocopyUnaligned for T {} |
| 184 |
|
| 185 |
pub trait AssertNotZerocopyImmutable {} |
| 186 |
impl<T: imp::Immutable> AssertNotZerocopyImmutable for T {} |
| 187 |
|
| 188 |
pub trait AssertNotZerocopyKnownLayout {} |
| 189 |
impl<T: imp::KnownLayout> AssertNotZerocopyKnownLayout for T {} |