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
9use crate::ZeroFrom;
10
11macro_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
23impl_copy_type!(u8);
24impl_copy_type!(u16);
25impl_copy_type!(u32);
26impl_copy_type!(u64);
27impl_copy_type!(u128);
28impl_copy_type!(usize);
29impl_copy_type!(i8);
30impl_copy_type!(i16);
31impl_copy_type!(i32);
32impl_copy_type!(i64);
33impl_copy_type!(i128);
34impl_copy_type!(isize);
35impl_copy_type!(char);
36impl_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
40macro_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
55array_zf_impl!(1; 0);
56array_zf_impl!(2; 0, 1);
57array_zf_impl!(3; 0, 1, 2);
58array_zf_impl!(4; 0, 1, 2, 3);
59array_zf_impl!(5; 0, 1, 2, 3, 4);
60array_zf_impl!(6; 0, 1, 2, 3, 4, 5);
61array_zf_impl!(7; 0, 1, 2, 3, 4, 5, 6);
62array_zf_impl!(8; 0, 1, 2, 3, 4, 5, 6, 7);
63array_zf_impl!(9; 0, 1, 2, 3, 4, 5, 6, 7, 8);
64array_zf_impl!(10; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
65array_zf_impl!(11; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
66array_zf_impl!(12; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
67array_zf_impl!(13; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
68array_zf_impl!(14; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
69array_zf_impl!(15; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
70array_zf_impl!(16; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
71
72macro_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
86tuple_zf_impl!((C1, T1, 0), (C2, T2, 1));
87tuple_zf_impl!((C1, T1, 0), (C2, T2, 1), (C3, T3, 2));
88tuple_zf_impl!((C1, T1, 0), (C2, T2, 1), (C3, T3, 2), (C4, T4, 3));
89tuple_zf_impl!(
90 (C1, T1, 0),
91 (C2, T2, 1),
92 (C3, T3, 2),
93 (C4, T4, 3),
94 (C5, T5, 4)
95);
96tuple_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);
104tuple_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);
113tuple_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);
123tuple_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);
134tuple_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);
146