devela/num/int/wrapper/
namespace.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// devela::num::int::wrapper::namespace
//
//! Defines the [`Int`] namespace wrapper.
//

#[doc = crate::TAG_NAMESPACE!()]
/// Provides comprehensive integer operations on `T`, most of them *const*.
///
/// It's implemented for:
/// - all the integer primitives: `i8`, …, `i128`, `u8`, …, `u128`.
///
/// Specific implementations can vary between signed and signed numeric types,
/// but documentation is the same for all bit sizes:
/// - `i32` methods documentation related to:
/// [base][Self#integer-base-related-methods-for-i32],
/// [core][Self#integer-core-methods-for-i32],
/// [combinatorics][Self#integer-combinatorics-related-methods-for-i32],
/// [division][Self#integer-division-related-methods-for-i32],
/// [factors][Self#integer-factors-related-methods-for-i32],
/// [modulo][Self#integer-modulo-related-methods-for-i32],
/// [primes][Self#integer-prime-related-methods-for-i32],
/// [root][Self#integer-root-related-methods-for-i32].
/// - `u32` methods documentation related to:
/// [base][Self#integer-base-related-methods-for-u32],
/// [core][Self#integer-core-methods-for-u32],
/// [combinatorics][Self#integer-combinatorics-related-methods-for-u32],
/// [division][Self#integer-division-related-methods-for-u32],
/// [factors][Self#integer-factors-related-methods-for-u32],
/// [modulo][Self#integer-modulo-related-methods-for-u32],
/// [primes][Self#integer-prime-related-methods-for-u32],
/// [root][Self#integer-root-related-methods-for-u32].
///
/// See also the related trait [`NumInt`][crate::NumInt].
#[repr(transparent)]
pub struct Int<T>(pub T);

crate::num::impl_ops![Int:
    i8:"_int_i8",
    i16:"_int_i16",
    i32:"_int_i32",
    i64:"_int_i64",
    i128:"_int_i128",
    isize:"_int_isize"];
crate::num::impl_ops![Int: (no_neg)
    u8:"_int_i8",
    u16:"_int_u16",
    u32:"_int_u32",
    u64:"_int_u64",
    u128:"_int_u128",
    usize:"_int_usize"];

#[rustfmt::skip]
mod core_impls {
    use core::fmt;
    use crate::{Hash, Hasher, Int, Ordering, ValueQuant};

    impl<T: Clone> Clone for Int<T> {
        #[must_use]
        fn clone(&self) -> Self { Self(self.0.clone()) }
    }
    impl<T: Copy> Copy for Int<T> {}
    impl<T: fmt::Debug> fmt::Debug for Int<T> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.debug_tuple("Int").field(&self.0).finish()
        }
    }
    impl<T: fmt::Display> fmt::Display for Int<T> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
    }
    impl<T: fmt::Binary> fmt::Binary for Int<T> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Binary::fmt(&self.0, f) }
    }
    impl<T: fmt::Octal> fmt::Octal for Int<T> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Octal::fmt(&self.0, f) }
    }
    impl<T: fmt::LowerHex> fmt::LowerHex for Int<T> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) }
    }
    impl<T: fmt::UpperHex> fmt::UpperHex for Int<T> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::UpperHex::fmt(&self.0, f) }
    }
    impl<T: fmt::UpperExp> fmt::UpperExp for Int<T> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::UpperExp::fmt(&self.0, f) }
    }
    impl<T: fmt::LowerExp> fmt::LowerExp for Int<T> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerExp::fmt(&self.0, f) }
    }

    /* eq */

    impl<T: PartialEq> PartialEq for Int<T> {
        #[must_use]
        fn eq(&self, other: &Self) -> bool { self.0.eq(&other.0) }
    }
    impl<T: Eq> Eq for Int<T> {}
    // with the inner value:
    impl<T: PartialEq> PartialEq<T> for Int<T> {
        #[must_use]
        fn eq(&self, other: &T) -> bool { self.0.eq(other) }
    }
    // with ValueQuant:
    impl<T: PartialEq> PartialEq<ValueQuant<T, T>> for ValueQuant<Int<T>, Int<T>> {
        #[must_use]
        fn eq(&self, other: &ValueQuant<T, T>) -> bool {
            self.v.eq(&other.v) && self.q.eq(&other.q)
        }
    }
    impl<T: PartialEq> PartialEq<ValueQuant<Int<T>, Int<T>>> for ValueQuant<T, T> {
        #[must_use]
        fn eq(&self, other: &ValueQuant<Int<T>, Int<T>>) -> bool {
            self.v.eq(&other.v.0) && self.q.eq(&other.q.0)
        }
    }
    // with ValueQuant and tuple:
    impl<T: PartialEq> PartialEq<(T, T)> for ValueQuant<Int<T>, Int<T>> {
        #[must_use]
        fn eq(&self, other: &(T, T)) -> bool {
            self.v.eq(&other.0) && self.q.eq(&other.1)
        }
    }
    impl<T: PartialEq> PartialEq<(Int<T>, Int<T>)> for ValueQuant<T, T> {
        #[must_use]
        fn eq(&self, other: &(Int<T>, Int<T>)) -> bool {
            self.v.eq(&other.0.0) && self.q.eq(&other.1.0)
        }
    }

    /* ord*/

    impl<T: PartialOrd> PartialOrd for Int<T> {
        #[must_use]
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            self.0.partial_cmp(&other.0)
        }
    }
    impl<T: Ord> Ord for Int<T> {
        #[must_use]
        fn cmp(&self, other: &Self) -> Ordering {
            self.0.cmp(&other.0)
        }
    }
    // with the inner value:
    impl<T: PartialOrd> PartialOrd<T> for Int<T> {
        #[must_use]
        fn partial_cmp(&self, other: &T) -> Option<Ordering> {
            self.0.partial_cmp(other)
        }
    }

    impl<T: Hash> Hash for Int<T> {
        fn hash<H: Hasher>(&self, state: &mut H) {
            self.0.hash(state);
        }
    }
    impl<T: Hasher> Hasher for Int<T> {
        #[must_use]
        fn finish(&self) -> u64 {
            self.0.finish()
        }
        fn write(&mut self, bytes: &[u8]) {
            self.0.write(bytes);
        }
    }
}