/rust/registry/src/index.crates.io-6f17d22bba15001f/enumset-1.1.5/src/repr/primitive.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::repr::EnumSetTypeRepr; |
2 | | |
3 | | macro_rules! prim { |
4 | | ($name:ty, $width:expr, $preferred_array_len:expr) => { |
5 | | const _: () = { |
6 | | fn lo(v: $name) -> u64 { |
7 | | v as u64 |
8 | | } |
9 | | fn hi(v: $name) -> u64 { |
10 | | ((v as u128) >> 64) as u64 |
11 | | } |
12 | | |
13 | | impl EnumSetTypeRepr for $name { |
14 | | const PREFERRED_ARRAY_LEN: usize = $preferred_array_len; |
15 | | const WIDTH: u32 = $width; |
16 | | const EMPTY: Self = 0; |
17 | | |
18 | | #[inline(always)] |
19 | 2.11M | fn is_empty(&self) -> bool { |
20 | 2.11M | *self == 0 |
21 | 2.11M | } |
22 | | |
23 | | #[inline(always)] |
24 | 532k | fn add_bit(&mut self, bit: u32) { |
25 | 532k | *self |= 1 << bit as $name; |
26 | 532k | } |
27 | | #[inline(always)] |
28 | 1.88M | fn remove_bit(&mut self, bit: u32) { |
29 | 1.88M | *self &= !(1 << bit as $name); |
30 | 1.88M | } |
31 | | #[inline(always)] |
32 | 1.40M | fn has_bit(&self, bit: u32) -> bool { |
33 | 1.40M | (self & (1 << bit as $name)) != 0 |
34 | 1.40M | } |
35 | | |
36 | | #[inline(always)] |
37 | 171k | fn count_ones(&self) -> u32 { |
38 | 171k | (*self).count_ones() |
39 | 171k | } |
40 | | #[inline(always)] |
41 | | fn leading_zeros(&self) -> u32 { |
42 | | (*self).leading_zeros() |
43 | | } |
44 | | #[inline(always)] |
45 | 1.88M | fn trailing_zeros(&self) -> u32 { |
46 | 1.88M | (*self).trailing_zeros() |
47 | 1.88M | } |
48 | | |
49 | | #[inline(always)] |
50 | 54.8k | fn and_not(&self, other: Self) -> Self { |
51 | 54.8k | (*self) & !other |
52 | 54.8k | } |
53 | | |
54 | | type Iter = PrimitiveIter<Self>; |
55 | | #[inline] |
56 | 171k | fn iter(self) -> Self::Iter { |
57 | 171k | PrimitiveIter(self) |
58 | 171k | } <u16 as enumset::repr::EnumSetTypeRepr>::iter Line | Count | Source | 56 | 171k | fn iter(self) -> Self::Iter { | 57 | 171k | PrimitiveIter(self) | 58 | 171k | } |
Unexecuted instantiation: <u16 as enumset::repr::EnumSetTypeRepr>::iter Unexecuted instantiation: <u16 as enumset::repr::EnumSetTypeRepr>::iter Unexecuted instantiation: <u16 as enumset::repr::EnumSetTypeRepr>::iter Unexecuted instantiation: <u16 as enumset::repr::EnumSetTypeRepr>::iter |
59 | | |
60 | | #[inline(always)] |
61 | | fn from_u8(v: u8) -> Self { |
62 | | v as $name |
63 | | } |
64 | | #[inline(always)] |
65 | | fn from_u16(v: u16) -> Self { |
66 | | v as $name |
67 | | } |
68 | | #[inline(always)] |
69 | | fn from_u32(v: u32) -> Self { |
70 | | v as $name |
71 | | } |
72 | | #[inline(always)] |
73 | | fn from_u64(v: u64) -> Self { |
74 | | v as $name |
75 | | } |
76 | | #[inline(always)] |
77 | | fn from_u128(v: u128) -> Self { |
78 | | v as $name |
79 | | } |
80 | | #[inline(always)] |
81 | | fn from_usize(v: usize) -> Self { |
82 | | v as $name |
83 | | } |
84 | | |
85 | | #[inline(always)] |
86 | | fn to_u8(&self) -> u8 { |
87 | | (*self) as u8 |
88 | | } |
89 | | #[inline(always)] |
90 | | fn to_u16(&self) -> u16 { |
91 | | (*self) as u16 |
92 | | } |
93 | | #[inline(always)] |
94 | | fn to_u32(&self) -> u32 { |
95 | | (*self) as u32 |
96 | | } |
97 | | #[inline(always)] |
98 | | fn to_u64(&self) -> u64 { |
99 | | (*self) as u64 |
100 | | } |
101 | | #[inline(always)] |
102 | | fn to_u128(&self) -> u128 { |
103 | | (*self) as u128 |
104 | | } |
105 | | #[inline(always)] |
106 | 0 | fn to_usize(&self) -> usize { |
107 | 0 | (*self) as usize |
108 | 0 | } |
109 | | |
110 | | #[inline(always)] |
111 | | fn from_u8_opt(v: u8) -> Option<Self> { |
112 | | v.try_into().ok() |
113 | | } |
114 | | #[inline(always)] |
115 | | fn from_u16_opt(v: u16) -> Option<Self> { |
116 | | v.try_into().ok() |
117 | | } |
118 | | #[inline(always)] |
119 | | fn from_u32_opt(v: u32) -> Option<Self> { |
120 | | v.try_into().ok() |
121 | | } |
122 | | #[inline(always)] |
123 | 54.8k | fn from_u64_opt(v: u64) -> Option<Self> { |
124 | 54.8k | v.try_into().ok() |
125 | 54.8k | } |
126 | | #[inline(always)] |
127 | | fn from_u128_opt(v: u128) -> Option<Self> { |
128 | | v.try_into().ok() |
129 | | } |
130 | | #[inline(always)] |
131 | | fn from_usize_opt(v: usize) -> Option<Self> { |
132 | | v.try_into().ok() |
133 | | } |
134 | | |
135 | | #[inline(always)] |
136 | | fn to_u8_opt(&self) -> Option<u8> { |
137 | | (*self).try_into().ok() |
138 | | } |
139 | | #[inline(always)] |
140 | | fn to_u16_opt(&self) -> Option<u16> { |
141 | | (*self).try_into().ok() |
142 | | } |
143 | | #[inline(always)] |
144 | | fn to_u32_opt(&self) -> Option<u32> { |
145 | | (*self).try_into().ok() |
146 | | } |
147 | | #[inline(always)] |
148 | 42.4k | fn to_u64_opt(&self) -> Option<u64> { |
149 | 42.4k | (*self).try_into().ok() |
150 | 42.4k | } |
151 | | #[inline(always)] |
152 | | fn to_u128_opt(&self) -> Option<u128> { |
153 | | (*self).try_into().ok() |
154 | | } |
155 | | #[inline(always)] |
156 | | fn to_usize_opt(&self) -> Option<usize> { |
157 | | (*self).try_into().ok() |
158 | | } |
159 | | |
160 | | #[inline(always)] |
161 | | fn to_u64_array<const O: usize>(&self) -> [u64; O] { |
162 | | let mut array = [0; O]; |
163 | | if O > 0 { |
164 | | array[0] = lo(*self); |
165 | | } |
166 | | if O > 1 && $preferred_array_len == 2 { |
167 | | array[1] = hi(*self); |
168 | | } |
169 | | array |
170 | | } |
171 | | #[inline(always)] |
172 | | fn to_u64_array_opt<const O: usize>(&self) -> Option<[u64; O]> { |
173 | | if O == 0 && *self != 0 { |
174 | | None |
175 | | } else if O == 1 && hi(*self) != 0 { |
176 | | None |
177 | | } else { |
178 | | Some(self.to_u64_array()) |
179 | | } |
180 | | } |
181 | | |
182 | | #[inline(always)] |
183 | | fn from_u64_array<const O: usize>(v: [u64; O]) -> Self { |
184 | | if O == 0 { |
185 | | 0 |
186 | | } else if O > 1 && $preferred_array_len == 2 { |
187 | | Self::from_u128(v[0] as u128 | ((v[1] as u128) << 64)) |
188 | | } else { |
189 | | Self::from_u64(v[0]) |
190 | | } |
191 | | } |
192 | | #[inline(always)] |
193 | | fn from_u64_array_opt<const O: usize>(v: [u64; O]) -> Option<Self> { |
194 | | if O == 0 { |
195 | | Some(0) |
196 | | } else if O == 1 { |
197 | | Self::from_u64_opt(v[0]) |
198 | | } else { |
199 | | for i in 2..O { |
200 | | if v[i] != 0 { |
201 | | return None; |
202 | | } |
203 | | } |
204 | | Self::from_u128_opt(v[0] as u128 | ((v[1] as u128) << 64)) |
205 | | } |
206 | | } |
207 | | |
208 | | #[inline(always)] |
209 | | fn to_u64_slice(&self, out: &mut [u64]) { |
210 | | if out.len() > 0 { |
211 | | out[0] = lo(*self); |
212 | | } |
213 | | if out.len() > 1 && $preferred_array_len == 2 { |
214 | | out[1] = hi(*self); |
215 | | } |
216 | | for i in $preferred_array_len..out.len() { |
217 | | out[i] = 0; |
218 | | } |
219 | | } |
220 | | #[inline(always)] |
221 | | #[must_use] |
222 | | fn to_u64_slice_opt(&self, out: &mut [u64]) -> Option<()> { |
223 | | if out.len() == 0 && *self != 0 { |
224 | | None |
225 | | } else if out.len() == 1 && hi(*self) != 0 { |
226 | | None |
227 | | } else { |
228 | | self.to_u64_slice(out); |
229 | | Some(()) |
230 | | } |
231 | | } |
232 | | |
233 | | #[inline(always)] |
234 | | fn from_u64_slice(v: &[u64]) -> Self { |
235 | | if v.len() == 0 { |
236 | | 0 |
237 | | } else if v.len() > 1 && $preferred_array_len == 2 { |
238 | | Self::from_u128(v[0] as u128 | ((v[1] as u128) << 64)) |
239 | | } else { |
240 | | Self::from_u64(v[0]) |
241 | | } |
242 | | } |
243 | | #[inline(always)] |
244 | | fn from_u64_slice_opt(v: &[u64]) -> Option<Self> { |
245 | | if v.len() == 0 { |
246 | | Some(0) |
247 | | } else if v.len() == 1 { |
248 | | Self::from_u64_opt(v[0]) |
249 | | } else { |
250 | | for i in 2..v.len() { |
251 | | if v[i] != 0 { |
252 | | return None; |
253 | | } |
254 | | } |
255 | | Self::from_u128_opt(v[0] as u128 | ((v[1] as u128) << 64)) |
256 | | } |
257 | | } |
258 | | } |
259 | | }; |
260 | | }; |
261 | | } |
262 | | prim!(u8, 8, 1); |
263 | | prim!(u16, 16, 1); |
264 | | prim!(u32, 32, 1); |
265 | | prim!(u64, 64, 1); |
266 | | prim!(u128, 128, 2); |
267 | | |
268 | | #[derive(Copy, Clone, Debug)] |
269 | | #[repr(transparent)] |
270 | | pub struct PrimitiveIter<T: EnumSetTypeRepr>(pub T); |
271 | | |
272 | | impl<T: EnumSetTypeRepr> Iterator for PrimitiveIter<T> { |
273 | | type Item = u32; |
274 | | |
275 | 2.06M | fn next(&mut self) -> Option<Self::Item> { |
276 | 2.06M | if self.0.is_empty() { |
277 | 171k | None |
278 | | } else { |
279 | 1.88M | let bit = self.0.trailing_zeros(); |
280 | 1.88M | self.0.remove_bit(bit); |
281 | 1.88M | Some(bit) |
282 | | } |
283 | 2.06M | } <enumset::repr::primitive::PrimitiveIter<u16> as core::iter::traits::iterator::Iterator>::next Line | Count | Source | 275 | 2.06M | fn next(&mut self) -> Option<Self::Item> { | 276 | 2.06M | if self.0.is_empty() { | 277 | 171k | None | 278 | | } else { | 279 | 1.88M | let bit = self.0.trailing_zeros(); | 280 | 1.88M | self.0.remove_bit(bit); | 281 | 1.88M | Some(bit) | 282 | | } | 283 | 2.06M | } |
Unexecuted instantiation: <enumset::repr::primitive::PrimitiveIter<u16> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <enumset::repr::primitive::PrimitiveIter<u16> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <enumset::repr::primitive::PrimitiveIter<u16> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <enumset::repr::primitive::PrimitiveIter<u16> as core::iter::traits::iterator::Iterator>::next |
284 | | |
285 | 171k | fn size_hint(&self) -> (usize, Option<usize>) { |
286 | 171k | let left = self.0.count_ones() as usize; |
287 | 171k | (left, Some(left)) |
288 | 171k | } |
289 | | } |
290 | | |
291 | | impl<T: EnumSetTypeRepr> DoubleEndedIterator for PrimitiveIter<T> { |
292 | | fn next_back(&mut self) -> Option<Self::Item> { |
293 | | if self.0.is_empty() { |
294 | | None |
295 | | } else { |
296 | | let bit = T::WIDTH - 1 - self.0.leading_zeros(); |
297 | | self.0.remove_bit(bit); |
298 | | Some(bit) |
299 | | } |
300 | | } |
301 | | } |