1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#![allow(clippy::transmute_ptr_to_ptr)]
use crate::{IsCovariant, Yokeable, ZeroCopyFrom};
use core::{mem, ptr};
macro_rules! copy_yoke_impl {
() => {
#[inline]
fn transform(&self) -> &Self::Output {
self
}
#[inline]
fn transform_owned(self) -> Self::Output {
self
}
#[inline]
unsafe fn make(this: Self::Output) -> Self {
this
}
#[inline]
fn transform_mut<F>(&'a mut self, f: F)
where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
{
f(self)
}
};
}
macro_rules! impl_copy_type {
($ty:ident) => {
unsafe impl<'a> Yokeable<'a> for $ty {
type Output = Self;
copy_yoke_impl!();
}
impl<'a> ZeroCopyFrom<'a, $ty> for $ty {
#[inline]
fn zero_copy_from(this: &'a Self) -> Self {
*this
}
}
unsafe impl<'a> IsCovariant<'a> for $ty {}
};
}
impl_copy_type!(u8);
impl_copy_type!(u16);
impl_copy_type!(u32);
impl_copy_type!(u64);
impl_copy_type!(u128);
impl_copy_type!(i8);
impl_copy_type!(i16);
impl_copy_type!(i32);
impl_copy_type!(i64);
impl_copy_type!(i128);
impl_copy_type!(char);
impl_copy_type!(bool);
macro_rules! unsafe_complex_yoke_impl {
() => {
fn transform(&'a self) -> &'a Self::Output {
unsafe { mem::transmute(self) }
}
fn transform_owned(self) -> Self::Output {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
unsafe {
let ptr: *const Self::Output = (&self as *const Self).cast();
mem::forget(self);
ptr::read(ptr)
}
}
unsafe fn make(from: Self::Output) -> Self {
debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
let ptr: *const Self = (&from as *const Self::Output).cast();
mem::forget(from);
ptr::read(ptr)
}
fn transform_mut<F>(&'a mut self, f: F)
where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
{
unsafe { f(mem::transmute::<&'a mut Self, &'a mut Self::Output>(self)) }
}
};
}
unsafe impl<'a, T: 'static + for<'b> Yokeable<'b>> Yokeable<'a> for Option<T> {
type Output = Option<<T as Yokeable<'a>>::Output>;
unsafe_complex_yoke_impl!();
}
unsafe impl<'a, T1: 'static + for<'b> Yokeable<'b>, T2: 'static + for<'b> Yokeable<'b>> Yokeable<'a>
for (T1, T2)
{
type Output = (<T1 as Yokeable<'a>>::Output, <T2 as Yokeable<'a>>::Output);
unsafe_complex_yoke_impl!();
}
unsafe impl<'a, T: Yokeable<'a>, const N: usize> Yokeable<'a> for [T; N] {
type Output = [<T as Yokeable<'a>>::Output; N];
unsafe_complex_yoke_impl!();
}
macro_rules! array_zcf_impl {
($n:expr; $($i:expr),+) => {
impl<'a, C, T: ZeroCopyFrom<'a, C>> ZeroCopyFrom<'a, [C; $n]> for [T; $n] {
fn zero_copy_from(this: &'a [C; $n]) -> Self {
[
$(
<T as ZeroCopyFrom<C>>::zero_copy_from(&this[$i])
),+
]
}
}
}
}
array_zcf_impl!(1; 0);
array_zcf_impl!(2; 0, 1);
array_zcf_impl!(3; 0, 1, 2);
array_zcf_impl!(4; 0, 1, 2, 3);
array_zcf_impl!(5; 0, 1, 2, 3, 4);
array_zcf_impl!(6; 0, 1, 2, 3, 4, 5);
array_zcf_impl!(7; 0, 1, 2, 3, 4, 5, 6);
array_zcf_impl!(8; 0, 1, 2, 3, 4, 5, 6, 7);
array_zcf_impl!(9; 0, 1, 2, 3, 4, 5, 6, 7, 8);
array_zcf_impl!(10; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
array_zcf_impl!(11; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
array_zcf_impl!(12; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
array_zcf_impl!(13; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
array_zcf_impl!(14; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
array_zcf_impl!(15; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
array_zcf_impl!(16; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);