Skip to main content

devela/num/dom/int/wrapper/
namespace.rs

1// devela/src/num/dom/int/wrapper/namespace.rs
2//
3//! Defines the [`Int`] namespace wrapper.
4//
5
6#[doc = crate::_tags!(num namespace)]
7/// Provides comprehensive integer operations on `T`, all of them *const*.
8#[doc = crate::_doc_meta!{location("num/dom/int")}]
9///
10/// It's implemented for:
11/// - all the integer primitives: `i8`, …, `i128`, `u8`, …, `u128`.
12///
13/// Specific implementations can vary between signed and signed numeric types,
14/// but documentation is the same for all bit sizes:
15/// - `i64` methods documentation related to:
16/// [core][Self#integer-core-methods-for-i64],
17/// [combinatorics][Self#integer-combinatorics-related-methods-for-i64],
18/// [division][Self#integer-division-related-methods-for-i64],
19/// [elementary][Self#integer-elementary-related-methods-for-i64],
20/// [factors][Self#integer-factors-related-methods-for-i64],
21/// [modulo][Self#integer-modulo-related-methods-for-i64],
22/// [primes][Self#integer-prime-related-methods-for-i64],
23/// [root][Self#integer-root-related-methods-for-i64].
24/// - `u32` methods documentation related to:
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/// [elementary][Self#integer-elementary-related-methods-for-u32],
29/// [factors][Self#integer-factors-related-methods-for-u32],
30/// [modulo][Self#integer-modulo-related-methods-for-u32],
31/// [primes][Self#integer-prime-related-methods-for-u32],
32/// [root][Self#integer-root-related-methods-for-u32].
33///
34/// See also the related trait [`NumInt`].
35#[doc = crate::doclink!(custom devela "[`NumInt`]" "num/trait.NumInt.html")]
36#[must_use]
37#[repr(transparent)]
38pub struct Int<T>(pub T);
39
40crate::_num_dom_impl_arith![Int: i8, i16, i32, i64, i128, isize];
41crate::_num_dom_impl_arith![Int: (no_neg) u8, u16, u32, u64, u128, usize];
42#[rustfmt::skip]
43mod core_impls {
44    use crate::{impl_trait, Int, Ordering, ValueQuant};
45
46    impl<T: Clone> Clone for Int<T> {
47        fn clone(&self) -> Self { Self(self.0.clone()) }
48    }
49    impl<T: Copy> Copy for Int<T> {}
50
51    impl_trait![fmt::Debug for Int[T][T] where T |self, f|
52        f.debug_tuple("Int").field(&self.0).finish()
53    ];
54    impl_trait![fmt::Display for Int[T][T] where T |self, f| self.0.fmt(f)];
55    impl_trait![fmt::Binary for Int[T][T] where T |self, f| self.0.fmt(f)];
56    impl_trait![fmt::Octal for Int[T][T] where T |self, f| self.0.fmt(f)];
57    impl_trait![fmt::LowerHex for Int[T][T] where T |self, f| self.0.fmt(f)];
58    impl_trait![fmt::UpperHex for Int[T][T] where T |self, f| self.0.fmt(f)];
59    impl_trait![fmt::LowerExp for Int[T][T] where T |self, f| self.0.fmt(f)];
60    impl_trait![fmt::UpperExp for Int[T][T] where T |self, f| self.0.fmt(f)];
61
62    /* eq */
63
64    impl<T: PartialEq> PartialEq for Int<T> {
65        fn eq(&self, other: &Self) -> bool { self.0.eq(&other.0) }
66    }
67    impl<T: Eq> Eq for Int<T> {}
68    // with the inner value:
69    impl<T: PartialEq> PartialEq<T> for Int<T> {
70        fn eq(&self, other: &T) -> bool { self.0.eq(other) }
71    }
72
73    /* ord*/
74
75    impl<T: PartialOrd> PartialOrd for Int<T> {
76        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
77            self.0.partial_cmp(&other.0)
78        }
79    }
80    impl<T: Ord> Ord for Int<T> {
81        fn cmp(&self, other: &Self) -> Ordering {
82            self.0.cmp(&other.0)
83        }
84    }
85    // with the inner value:
86    impl<T: PartialOrd> PartialOrd<T> for Int<T> {
87        fn partial_cmp(&self, other: &T) -> Option<Ordering> {
88            self.0.partial_cmp(other)
89        }
90    }
91
92    impl_trait![Hash for Int[T][T] where T |self, s| self.0.hash(s)];
93
94    // with ValueQuant:
95    impl<T: PartialEq> PartialEq<ValueQuant<T, T>> for ValueQuant<Int<T>, Int<T>> {
96        fn eq(&self, other: &ValueQuant<T, T>) -> bool {
97            self.v.eq(&other.v) && self.q.eq(&other.q)
98        }
99    }
100    impl<T: PartialEq> PartialEq<ValueQuant<Int<T>, Int<T>>> for ValueQuant<T, T> {
101        fn eq(&self, other: &ValueQuant<Int<T>, Int<T>>) -> bool {
102            self.v.eq(&other.v.0) && self.q.eq(&other.q.0)
103        }
104    }
105    // with ValueQuant and tuple:
106    impl<T: PartialEq> PartialEq<(T, T)> for ValueQuant<Int<T>, Int<T>> {
107        fn eq(&self, other: &(T, T)) -> bool {
108            self.v.eq(&other.0) && self.q.eq(&other.1)
109        }
110    }
111    impl<T: PartialEq> PartialEq<(Int<T>, Int<T>)> for ValueQuant<T, T> {
112        fn eq(&self, other: &(Int<T>, Int<T>)) -> bool {
113            self.v.eq(&other.0.0) && self.q.eq(&other.1.0)
114        }
115    }
116}