devela/num/int/wrapper/
namespace.rs

1// devela::num::int::wrapper::namespace
2//
3//! Defines the [`Int`] namespace wrapper.
4//
5
6#[doc = crate::TAG_NAMESPACE!()]
7/// Provides comprehensive integer operations on `T`, most of them *const*.
8///
9/// It's implemented for:
10/// - all the integer primitives: `i8`, …, `i128`, `u8`, …, `u128`.
11///
12/// Specific implementations can vary between signed and signed numeric types,
13/// but documentation is the same for all bit sizes:
14/// - `i32` methods documentation related to:
15/// [base][Self#integer-base-related-methods-for-i32],
16/// [core][Self#integer-core-methods-for-i32],
17/// [combinatorics][Self#integer-combinatorics-related-methods-for-i32],
18/// [division][Self#integer-division-related-methods-for-i32],
19/// [factors][Self#integer-factors-related-methods-for-i32],
20/// [modulo][Self#integer-modulo-related-methods-for-i32],
21/// [primes][Self#integer-prime-related-methods-for-i32],
22/// [root][Self#integer-root-related-methods-for-i32].
23/// - `u32` methods documentation related to:
24/// [base][Self#integer-base-related-methods-for-u32],
25/// [core][Self#integer-core-methods-for-u32],
26/// [combinatorics][Self#integer-combinatorics-related-methods-for-u32],
27/// [division][Self#integer-division-related-methods-for-u32],
28/// [factors][Self#integer-factors-related-methods-for-u32],
29/// [modulo][Self#integer-modulo-related-methods-for-u32],
30/// [primes][Self#integer-prime-related-methods-for-u32],
31/// [root][Self#integer-root-related-methods-for-u32].
32///
33/// See also the related trait [`NumInt`][crate::NumInt].
34#[repr(transparent)]
35pub struct Int<T>(pub T);
36
37crate::num::impl_ops![Int:
38    i8:"_int_i8",
39    i16:"_int_i16",
40    i32:"_int_i32",
41    i64:"_int_i64",
42    i128:"_int_i128",
43    isize:"_int_isize"];
44crate::num::impl_ops![Int: (no_neg)
45    u8:"_int_i8",
46    u16:"_int_u16",
47    u32:"_int_u32",
48    u64:"_int_u64",
49    u128:"_int_u128",
50    usize:"_int_usize"];
51
52#[rustfmt::skip]
53mod core_impls {
54    use core::fmt;
55    use crate::{Hash, Hasher, Int, Ordering, ValueQuant};
56
57    impl<T: Clone> Clone for Int<T> {
58        #[must_use]
59        fn clone(&self) -> Self { Self(self.0.clone()) }
60    }
61    impl<T: Copy> Copy for Int<T> {}
62    impl<T: fmt::Debug> fmt::Debug for Int<T> {
63        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64            f.debug_tuple("Int").field(&self.0).finish()
65        }
66    }
67    impl<T: fmt::Display> fmt::Display for Int<T> {
68        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
69    }
70    impl<T: fmt::Binary> fmt::Binary for Int<T> {
71        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Binary::fmt(&self.0, f) }
72    }
73    impl<T: fmt::Octal> fmt::Octal for Int<T> {
74        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Octal::fmt(&self.0, f) }
75    }
76    impl<T: fmt::LowerHex> fmt::LowerHex for Int<T> {
77        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) }
78    }
79    impl<T: fmt::UpperHex> fmt::UpperHex for Int<T> {
80        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::UpperHex::fmt(&self.0, f) }
81    }
82    impl<T: fmt::UpperExp> fmt::UpperExp for Int<T> {
83        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::UpperExp::fmt(&self.0, f) }
84    }
85    impl<T: fmt::LowerExp> fmt::LowerExp for Int<T> {
86        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerExp::fmt(&self.0, f) }
87    }
88
89    /* eq */
90
91    impl<T: PartialEq> PartialEq for Int<T> {
92        #[must_use]
93        fn eq(&self, other: &Self) -> bool { self.0.eq(&other.0) }
94    }
95    impl<T: Eq> Eq for Int<T> {}
96    // with the inner value:
97    impl<T: PartialEq> PartialEq<T> for Int<T> {
98        #[must_use]
99        fn eq(&self, other: &T) -> bool { self.0.eq(other) }
100    }
101    // with ValueQuant:
102    impl<T: PartialEq> PartialEq<ValueQuant<T, T>> for ValueQuant<Int<T>, Int<T>> {
103        #[must_use]
104        fn eq(&self, other: &ValueQuant<T, T>) -> bool {
105            self.v.eq(&other.v) && self.q.eq(&other.q)
106        }
107    }
108    impl<T: PartialEq> PartialEq<ValueQuant<Int<T>, Int<T>>> for ValueQuant<T, T> {
109        #[must_use]
110        fn eq(&self, other: &ValueQuant<Int<T>, Int<T>>) -> bool {
111            self.v.eq(&other.v.0) && self.q.eq(&other.q.0)
112        }
113    }
114    // with ValueQuant and tuple:
115    impl<T: PartialEq> PartialEq<(T, T)> for ValueQuant<Int<T>, Int<T>> {
116        #[must_use]
117        fn eq(&self, other: &(T, T)) -> bool {
118            self.v.eq(&other.0) && self.q.eq(&other.1)
119        }
120    }
121    impl<T: PartialEq> PartialEq<(Int<T>, Int<T>)> for ValueQuant<T, T> {
122        #[must_use]
123        fn eq(&self, other: &(Int<T>, Int<T>)) -> bool {
124            self.v.eq(&other.0.0) && self.q.eq(&other.1.0)
125        }
126    }
127
128    /* ord*/
129
130    impl<T: PartialOrd> PartialOrd for Int<T> {
131        #[must_use]
132        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
133            self.0.partial_cmp(&other.0)
134        }
135    }
136    impl<T: Ord> Ord for Int<T> {
137        #[must_use]
138        fn cmp(&self, other: &Self) -> Ordering {
139            self.0.cmp(&other.0)
140        }
141    }
142    // with the inner value:
143    impl<T: PartialOrd> PartialOrd<T> for Int<T> {
144        #[must_use]
145        fn partial_cmp(&self, other: &T) -> Option<Ordering> {
146            self.0.partial_cmp(other)
147        }
148    }
149
150    impl<T: Hash> Hash for Int<T> {
151        fn hash<H: Hasher>(&self, state: &mut H) {
152            self.0.hash(state);
153        }
154    }
155    impl<T: Hasher> Hasher for Int<T> {
156        #[must_use]
157        fn finish(&self) -> u64 {
158            self.0.finish()
159        }
160        fn write(&mut self, bytes: &[u8]) {
161            self.0.write(bytes);
162        }
163    }
164}