| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
// In this case consistency between impls is more important |
| 6 |
// than using pointer casts |
| 7 |
#![allow(clippy::transmute_ptr_to_ptr)] |
| 8 |
|
| 9 |
use crate::ZeroFrom; |
| 10 |
|
| 11 |
macro_rules! impl_copy_type { |
| 12 |
($ty:ident) => { |
| 13 |
impl<'a> ZeroFrom<'a, $ty> for $ty { |
| 14 |
#[inline] |
| 15 |
fn zero_from(this: &'a Self) -> Self { |
| 16 |
// Essentially only works when the struct is fully Copy |
| 17 |
*this |
| 18 |
} |
| 19 |
} |
| 20 |
}; |
| 21 |
} |
| 22 |
|
| 23 |
impl_copy_type!(u8); |
| 24 |
impl_copy_type!(u16); |
| 25 |
impl_copy_type!(u32); |
| 26 |
impl_copy_type!(u64); |
| 27 |
impl_copy_type!(u128); |
| 28 |
impl_copy_type!(usize); |
| 29 |
impl_copy_type!(i8); |
| 30 |
impl_copy_type!(i16); |
| 31 |
impl_copy_type!(i32); |
| 32 |
impl_copy_type!(i64); |
| 33 |
impl_copy_type!(i128); |
| 34 |
impl_copy_type!(isize); |
| 35 |
impl_copy_type!(char); |
| 36 |
impl_copy_type!(bool); |
| 37 |
|
| 38 |
// This can be cleaned up once `[T; N]`::each_ref() is stabilized |
| 39 |
// https://github.com/rust-lang/rust/issues/76118 |
| 40 |
macro_rules! array_zf_impl { |
| 41 |
($n:expr; $($i:expr),+) => { |
| 42 |
impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; $n]> for [T; $n] { |
| 43 |
fn zero_from(this: &'a [C; $n]) -> Self { |
| 44 |
[ |
| 45 |
$( |
| 46 |
<T as ZeroFrom<C>>::zero_from(&this[$i]) |
| 47 |
),+ |
| 48 |
|
| 49 |
] |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
array_zf_impl!(1; 0); |
| 56 |
array_zf_impl!(2; 0, 1); |
| 57 |
array_zf_impl!(3; 0, 1, 2); |
| 58 |
array_zf_impl!(4; 0, 1, 2, 3); |
| 59 |
array_zf_impl!(5; 0, 1, 2, 3, 4); |
| 60 |
array_zf_impl!(6; 0, 1, 2, 3, 4, 5); |
| 61 |
array_zf_impl!(7; 0, 1, 2, 3, 4, 5, 6); |
| 62 |
array_zf_impl!(8; 0, 1, 2, 3, 4, 5, 6, 7); |
| 63 |
array_zf_impl!(9; 0, 1, 2, 3, 4, 5, 6, 7, 8); |
| 64 |
array_zf_impl!(10; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 65 |
array_zf_impl!(11; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 66 |
array_zf_impl!(12; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); |
| 67 |
array_zf_impl!(13; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); |
| 68 |
array_zf_impl!(14; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13); |
| 69 |
array_zf_impl!(15; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14); |
| 70 |
array_zf_impl!(16; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); |
| 71 |
|
| 72 |
macro_rules! tuple_zf_impl { |
| 73 |
($(($c:ident, $t:ident, $i:tt)),+) => { |
| 74 |
impl<'zf, $($c, $t: ZeroFrom<'zf, $c>),+> ZeroFrom<'zf, ($($c),+)> |
| 75 |
for ($($t),+) |
| 76 |
{ |
| 77 |
fn zero_from(other: &'zf ($($c),+)) -> Self { |
| 78 |
( |
| 79 |
$(<$t as ZeroFrom<$c>>::zero_from(&other.$i)),+ |
| 80 |
) |
| 81 |
} |
| 82 |
} |
| 83 |
}; |
| 84 |
} |
| 85 |
|
| 86 |
tuple_zf_impl!((C1, T1, 0), (C2, T2, 1)); |
| 87 |
tuple_zf_impl!((C1, T1, 0), (C2, T2, 1), (C3, T3, 2)); |
| 88 |
tuple_zf_impl!((C1, T1, 0), (C2, T2, 1), (C3, T3, 2), (C4, T4, 3)); |
| 89 |
tuple_zf_impl!( |
| 90 |
(C1, T1, 0), |
| 91 |
(C2, T2, 1), |
| 92 |
(C3, T3, 2), |
| 93 |
(C4, T4, 3), |
| 94 |
(C5, T5, 4) |
| 95 |
); |
| 96 |
tuple_zf_impl!( |
| 97 |
(C1, T1, 0), |
| 98 |
(C2, T2, 1), |
| 99 |
(C3, T3, 2), |
| 100 |
(C4, T4, 3), |
| 101 |
(C5, T5, 4), |
| 102 |
(C6, T6, 5) |
| 103 |
); |
| 104 |
tuple_zf_impl!( |
| 105 |
(C1, T1, 0), |
| 106 |
(C2, T2, 1), |
| 107 |
(C3, T3, 2), |
| 108 |
(C4, T4, 3), |
| 109 |
(C5, T5, 4), |
| 110 |
(C6, T6, 5), |
| 111 |
(C7, T7, 6) |
| 112 |
); |
| 113 |
tuple_zf_impl!( |
| 114 |
(C1, T1, 0), |
| 115 |
(C2, T2, 1), |
| 116 |
(C3, T3, 2), |
| 117 |
(C4, T4, 3), |
| 118 |
(C5, T5, 4), |
| 119 |
(C6, T6, 5), |
| 120 |
(C7, T7, 6), |
| 121 |
(C8, T8, 7) |
| 122 |
); |
| 123 |
tuple_zf_impl!( |
| 124 |
(C1, T1, 0), |
| 125 |
(C2, T2, 1), |
| 126 |
(C3, T3, 2), |
| 127 |
(C4, T4, 3), |
| 128 |
(C5, T5, 4), |
| 129 |
(C6, T6, 5), |
| 130 |
(C7, T7, 6), |
| 131 |
(C8, T8, 7), |
| 132 |
(C9, T9, 8) |
| 133 |
); |
| 134 |
tuple_zf_impl!( |
| 135 |
(C1, T1, 0), |
| 136 |
(C2, T2, 1), |
| 137 |
(C3, T3, 2), |
| 138 |
(C4, T4, 3), |
| 139 |
(C5, T5, 4), |
| 140 |
(C6, T6, 5), |
| 141 |
(C7, T7, 6), |
| 142 |
(C8, T8, 7), |
| 143 |
(C9, T9, 8), |
| 144 |
(C10, T10, 9) |
| 145 |
); |