Skip to main content

object_rainbow/numeric/
private.rs

1use std::{fmt::Display, num::NonZero};
2
3use crate::{enumkind::UsizeTag, *};
4
5pub trait HasOtherSign {
6    type OtherSign;
7}
8
9type Os<T> = <T as HasOtherSign>::OtherSign;
10
11#[derive(ParseAsInline, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub struct Le<T>(pub T);
14#[derive(ParseAsInline, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16pub struct Be<T>(pub T);
17
18impl<T: Display> Display for Le<T> {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        self.0.fmt(f)
21    }
22}
23
24impl<T: Display> Display for Be<T> {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        self.0.fmt(f)
27    }
28}
29
30impl<T> Le<T> {
31    pub const fn new(value: T) -> Self {
32        Self(value)
33    }
34}
35
36impl<T> Be<T> {
37    pub const fn new(value: T) -> Self {
38        Self(value)
39    }
40}
41
42impl<T> From<T> for Be<T> {
43    fn from(n: T) -> Self {
44        Self(n)
45    }
46}
47
48impl<T> From<T> for Le<T> {
49    fn from(n: T) -> Self {
50        Self(n)
51    }
52}
53
54impl<T> Deref for Le<T> {
55    type Target = T;
56
57    fn deref(&self) -> &Self::Target {
58        &self.0
59    }
60}
61
62impl<T> Deref for Be<T> {
63    type Target = T;
64
65    fn deref(&self) -> &Self::Target {
66        &self.0
67    }
68}
69
70macro_rules! signs {
71    ($u:ty, $i:ty) => {
72        impl HasOtherSign for $u {
73            type OtherSign = $i;
74        }
75        impl HasOtherSign for $i {
76            type OtherSign = $u;
77        }
78    };
79}
80
81macro_rules! nz_unsigned_niche {
82    ($n:ty) => {
83        impl MaybeHasNiche for NonZero<$n> {
84            type MnArray = SomeNiche<ZeroNiche<<Self as Size>::Size>>;
85        }
86    };
87}
88
89macro_rules! nz_any_sign {
90    ($n:ty) => {
91        impl MaybeHasNiche for $n {
92            type MnArray = NoNiche<ZeroNoNiche<<Self as Size>::Size>>;
93        }
94
95        impl ToOutput for NonZero<$n> {
96            fn to_output(&self, output: &mut impl Output) {
97                self.get().to_output(output);
98            }
99        }
100
101        impl<I: ParseInput> Parse<I> for NonZero<$n> {
102            fn parse(input: I) -> crate::Result<Self> {
103                ParseInline::parse_as_inline(input)
104            }
105        }
106
107        impl<I: ParseInput> ParseInline<I> for NonZero<$n> {
108            fn parse_inline(input: &mut I) -> crate::Result<Self> {
109                NonZero::new(input.parse_inline::<$n>()?).ok_or(Error::Zero)
110            }
111        }
112
113        impl Size for NonZero<$n> {
114            const SIZE: usize = <$n as Size>::SIZE;
115            type Size = <$n as Size>::Size;
116        }
117
118        impl Equivalent<$n> for Option<NonZero<$n>> {
119            fn into_equivalent(self) -> $n {
120                self.map(NonZero::get).unwrap_or_default()
121            }
122            fn from_equivalent(object: $n) -> Self {
123                NonZero::new(object)
124            }
125        }
126    };
127}
128
129macro_rules! lebe {
130    ($n:ty) => {
131        impl UsizeTag for $n {
132            fn from_usize(n: usize) -> Self {
133                n.try_into().expect("discriminant out of range")
134            }
135            fn to_usize(&self) -> usize {
136                (*self).try_into().expect("discriminant out of range")
137            }
138            fn try_to_usize(&self) -> Option<usize> {
139                (*self).try_into().ok()
140            }
141        }
142
143        impl UsizeTag for NonZero<$n> {
144            fn from_usize(n: usize) -> Self {
145                Self::new(
146                    n.checked_add(1)
147                        .expect("discriminant out of range")
148                        .try_into()
149                        .expect("discriminant out of range"),
150                )
151                .unwrap()
152            }
153            fn to_usize(&self) -> usize {
154                usize::try_from(self.get())
155                    .expect("discriminant out of range")
156                    .checked_sub(1)
157                    .unwrap()
158            }
159            fn try_to_usize(&self) -> Option<usize> {
160                usize::try_from(self.get()).ok()?.checked_sub(1)
161            }
162        }
163
164        impl ToOutput for Le<$n> {
165            fn to_output(&self, output: &mut impl Output) {
166                if output.is_real() {
167                    output.write(&self.0.to_le_bytes());
168                }
169            }
170        }
171
172        impl ToOutput for Be<$n> {
173            fn to_output(&self, output: &mut impl Output) {
174                if output.is_real() {
175                    output.write(&self.0.to_be_bytes());
176                }
177            }
178        }
179
180        impl ToOutput for Le<NonZero<$n>> {
181            fn to_output(&self, output: &mut impl Output) {
182                if output.is_real() {
183                    output.write(&self.0.get().to_le_bytes());
184                }
185            }
186        }
187
188        impl ToOutput for Be<NonZero<$n>> {
189            fn to_output(&self, output: &mut impl Output) {
190                if output.is_real() {
191                    output.write(&self.0.get().to_be_bytes());
192                }
193            }
194        }
195
196        impl InlineOutput for Le<$n> {}
197        impl InlineOutput for Be<$n> {}
198
199        impl<I: ParseInput> ParseInline<I> for Le<$n> {
200            fn parse_inline(input: &mut I) -> crate::Result<Self> {
201                Ok(Self(<$n>::from_le_bytes(input.parse_chunk()?)))
202            }
203        }
204
205        impl<I: ParseInput> ParseInline<I> for Be<$n> {
206            fn parse_inline(input: &mut I) -> crate::Result<Self> {
207                Ok(Self(<$n>::from_be_bytes(input.parse_chunk()?)))
208            }
209        }
210
211        impl<I: ParseInput> ParseInline<I> for Le<NonZero<$n>> {
212            fn parse_inline(input: &mut I) -> crate::Result<Self> {
213                NonZero::new(input.parse_inline::<Le<$n>>()?.0)
214                    .ok_or(Error::Zero)
215                    .map(Le)
216            }
217        }
218
219        impl<I: ParseInput> ParseInline<I> for Be<NonZero<$n>> {
220            fn parse_inline(input: &mut I) -> crate::Result<Self> {
221                NonZero::new(input.parse_inline::<Be<$n>>()?.0)
222                    .ok_or(Error::Zero)
223                    .map(Be)
224            }
225        }
226
227        impl Size for Le<$n> {
228            const SIZE: usize = std::mem::size_of::<$n>();
229            type Size = typenum::generic_const_mappings::U<{ Self::SIZE }>;
230        }
231
232        impl Size for Be<$n> {
233            const SIZE: usize = std::mem::size_of::<$n>();
234            type Size = typenum::generic_const_mappings::U<{ Self::SIZE }>;
235        }
236
237        impl MaybeHasNiche for Le<$n> {
238            type MnArray = NoNiche<ZeroNoNiche<<Self as Size>::Size>>;
239        }
240
241        impl MaybeHasNiche for Be<$n> {
242            type MnArray = NoNiche<ZeroNoNiche<<Self as Size>::Size>>;
243        }
244
245        impl MaybeHasNiche for Le<NonZero<$n>> {
246            type MnArray = SomeNiche<ZeroNiche<<Self as Size>::Size>>;
247        }
248
249        impl MaybeHasNiche for Be<NonZero<$n>> {
250            type MnArray = SomeNiche<ZeroNiche<<Self as Size>::Size>>;
251        }
252
253        impl Tagged for $n {}
254        impl Tagged for NonZero<$n> {}
255        impl Tagged for Le<$n> {}
256        impl Tagged for Le<NonZero<$n>> {}
257        impl Tagged for Be<$n> {}
258        impl Tagged for Be<NonZero<$n>> {}
259        impl ListHashes for $n {}
260        impl ListHashes for NonZero<$n> {}
261        impl ListHashes for Le<$n> {}
262        impl ListHashes for Le<NonZero<$n>> {}
263        impl ListHashes for Be<$n> {}
264        impl ListHashes for Be<NonZero<$n>> {}
265        impl Topological for $n {}
266        impl Topological for NonZero<$n> {}
267        impl Topological for Le<$n> {}
268        impl Topological for Le<NonZero<$n>> {}
269        impl Topological for Be<$n> {}
270        impl Topological for Be<NonZero<$n>> {}
271
272        impl Size for Le<NonZero<$n>> {
273            const SIZE: usize = <Le<$n> as Size>::SIZE;
274            type Size = <Le<$n> as Size>::Size;
275        }
276
277        impl Size for Be<NonZero<$n>> {
278            const SIZE: usize = <Be<$n> as Size>::SIZE;
279            type Size = <Be<$n> as Size>::Size;
280        }
281
282        impl Equivalent<Le<$n>> for Option<Le<NonZero<$n>>> {
283            fn into_equivalent(self) -> Le<$n> {
284                Le(self.map(|object| object.0.get()).unwrap_or_default())
285            }
286            fn from_equivalent(object: Le<$n>) -> Self {
287                NonZero::new(object.0).map(Le)
288            }
289        }
290
291        impl Equivalent<Be<$n>> for Option<Be<NonZero<$n>>> {
292            fn into_equivalent(self) -> Be<$n> {
293                Be(self.map(|object| object.0.get()).unwrap_or_default())
294            }
295            fn from_equivalent(object: Be<$n>) -> Self {
296                NonZero::new(object.0).map(Be)
297            }
298        }
299
300        impl Equivalent<Le<Os<$n>>> for Le<$n> {
301            fn into_equivalent(self) -> Le<Os<$n>> {
302                Le(self.0 as _)
303            }
304            fn from_equivalent(object: Le<Os<$n>>) -> Self {
305                Le(object.0 as _)
306            }
307        }
308
309        impl Equivalent<Be<Os<$n>>> for Be<$n> {
310            fn into_equivalent(self) -> Be<Os<$n>> {
311                Be(self.0 as _)
312            }
313            fn from_equivalent(object: Be<Os<$n>>) -> Self {
314                Be(object.0 as _)
315            }
316        }
317
318        impl Equivalent<Le<NonZero<Os<$n>>>> for Le<NonZero<$n>> {
319            fn into_equivalent(self) -> Le<NonZero<Os<$n>>> {
320                Le(NonZero::new(self.0.get() as _).unwrap())
321            }
322            fn from_equivalent(object: Le<NonZero<Os<$n>>>) -> Self {
323                Le(NonZero::new(object.0.get() as _).unwrap())
324            }
325        }
326
327        impl Equivalent<Be<NonZero<Os<$n>>>> for Be<NonZero<$n>> {
328            fn into_equivalent(self) -> Be<NonZero<Os<$n>>> {
329                Be(NonZero::new(self.0.get() as _).unwrap())
330            }
331            fn from_equivalent(object: Be<NonZero<Os<$n>>>) -> Self {
332                Be(NonZero::new(object.0.get() as _).unwrap())
333            }
334        }
335    };
336}
337
338macro_rules! float {
339    ($n:ty) => {
340        impl ToOutput for Le<$n> {
341            fn to_output(&self, output: &mut impl Output) {
342                if output.is_real() {
343                    output.write(&self.0.to_le_bytes());
344                }
345            }
346        }
347
348        impl ToOutput for Be<$n> {
349            fn to_output(&self, output: &mut impl Output) {
350                if output.is_real() {
351                    output.write(&self.0.to_be_bytes());
352                }
353            }
354        }
355
356        impl InlineOutput for Le<$n> {}
357        impl InlineOutput for Be<$n> {}
358
359        impl<I: ParseInput> ParseInline<I> for Le<$n> {
360            fn parse_inline(input: &mut I) -> crate::Result<Self> {
361                Ok(Self(<$n>::from_le_bytes(input.parse_chunk()?)))
362            }
363        }
364
365        impl<I: ParseInput> ParseInline<I> for Be<$n> {
366            fn parse_inline(input: &mut I) -> crate::Result<Self> {
367                Ok(Self(<$n>::from_be_bytes(input.parse_chunk()?)))
368            }
369        }
370
371        impl Size for Le<$n> {
372            const SIZE: usize = std::mem::size_of::<$n>();
373            type Size = typenum::generic_const_mappings::U<{ Self::SIZE }>;
374        }
375
376        impl Size for Be<$n> {
377            const SIZE: usize = std::mem::size_of::<$n>();
378            type Size = typenum::generic_const_mappings::U<{ Self::SIZE }>;
379        }
380
381        impl MaybeHasNiche for Le<$n> {
382            type MnArray = NoNiche<ZeroNoNiche<<Self as Size>::Size>>;
383        }
384
385        impl MaybeHasNiche for Be<$n> {
386            type MnArray = NoNiche<ZeroNoNiche<<Self as Size>::Size>>;
387        }
388
389        impl ListHashes for Le<$n> {}
390        impl ListHashes for Be<$n> {}
391        impl Topological for Le<$n> {}
392        impl Topological for Be<$n> {}
393        impl Tagged for Le<$n> {}
394        impl Tagged for Be<$n> {}
395    };
396}
397
398macro_rules! byte_ordered {
399    ($n:ty) => {
400        impl ToOutput for $n {
401            fn to_output(&self, output: &mut impl Output) {
402                if output.is_real() {
403                    output.write(&(self ^ Self::MIN).to_be_bytes());
404                }
405            }
406        }
407
408        impl InlineOutput for $n {}
409
410        impl<I: ParseInput> Parse<I> for $n {
411            fn parse(input: I) -> crate::Result<Self> {
412                ParseInline::parse_as_inline(input)
413            }
414        }
415
416        impl<I: ParseInput> ParseInline<I> for $n {
417            fn parse_inline(input: &mut I) -> crate::Result<Self> {
418                Ok(Self::from_be_bytes(input.parse_chunk()?) ^ Self::MIN)
419            }
420        }
421
422        impl Size for $n {
423            const SIZE: usize = std::mem::size_of::<$n>();
424            type Size = typenum::generic_const_mappings::U<{ Self::SIZE }>;
425        }
426    };
427}
428
429signs!(u8, i8);
430signs!(u16, i16);
431signs!(u32, i32);
432signs!(u64, i64);
433signs!(u128, i128);
434
435nz_unsigned_niche!(u8);
436nz_any_sign!(u8);
437nz_any_sign!(i8);
438
439nz_unsigned_niche!(u16);
440nz_any_sign!(u16);
441nz_any_sign!(i16);
442
443nz_unsigned_niche!(u32);
444nz_any_sign!(u32);
445nz_any_sign!(i32);
446
447nz_unsigned_niche!(u64);
448nz_any_sign!(u64);
449nz_any_sign!(i64);
450
451nz_unsigned_niche!(u128);
452nz_any_sign!(u128);
453nz_any_sign!(i128);
454
455byte_ordered!(i8);
456
457byte_ordered!(u16);
458byte_ordered!(i16);
459
460byte_ordered!(u32);
461byte_ordered!(i32);
462
463byte_ordered!(u64);
464byte_ordered!(i64);
465
466byte_ordered!(u128);
467byte_ordered!(i128);
468
469lebe!(u8);
470lebe!(i8);
471
472lebe!(u16);
473lebe!(i16);
474
475lebe!(u32);
476lebe!(i32);
477
478lebe!(u64);
479lebe!(i64);
480
481lebe!(u128);
482lebe!(i128);
483
484float!(f32);
485float!(f64);
486
487#[test]
488fn nonzero() {
489    assert_eq!(Option::<super::Le<u8>>::SIZE, 2);
490    assert_eq!(Option::<super::Le<NonZero<u8>>>::SIZE, 1);
491    assert_eq!(Option::<super::Le<u16>>::SIZE, 3);
492    assert_eq!(Option::<super::Le<NonZero<u16>>>::SIZE, 2);
493}
494
495impl<T: UsizeTag> UsizeTag for Le<T> {
496    fn from_usize(n: usize) -> Self {
497        Self(UsizeTag::from_usize(n))
498    }
499
500    fn to_usize(&self) -> usize {
501        self.0.to_usize()
502    }
503
504    fn try_to_usize(&self) -> Option<usize> {
505        self.0.try_to_usize()
506    }
507}
508
509impl<T: UsizeTag> UsizeTag for Be<T> {
510    fn from_usize(n: usize) -> Self {
511        Self(UsizeTag::from_usize(n))
512    }
513
514    fn to_usize(&self) -> usize {
515        self.0.to_usize()
516    }
517
518    fn try_to_usize(&self) -> Option<usize> {
519        self.0.try_to_usize()
520    }
521}