devela/num/frac/wrapper/
mod.rs

1// devela::num::frac::wrapper
2//
3//! Fraction-related wrapper struct.
4//
5// TOC
6// - define Frac struct
7// - implement core traits
8
9#[cfg(doc)]
10use crate::Int;
11
12mod impl_frac;
13
14#[doc = crate::TAG_NAMESPACE!()]
15/// Provides comprehensive fractional operations on `T`, most of them *const*.
16///
17/// It's implemented for:
18/// - arrays: `[i8; 2]`… `[u128; 2]`; `[Int<i8>; 2]`… `[Int<u128>; 2]`.
19///
20/// The documentation is the same for all bit sizes. For example these are:
21/// - Methods for `[i32; 2]`:
22/// [core][Self#fraction-related-methods-for-i32-2];
23/// for `[Int<i32>; 2]`:
24/// [core][Self#fraction-related-methods-for-inti32-2].
25#[repr(transparent)]
26pub struct Frac<T>(pub T);
27
28#[rustfmt::skip]
29mod core_impls {
30    use {super::Frac, core::{fmt, hash}};
31
32    impl<T: Clone> Clone for Frac<T> {
33        #[must_use]
34        fn clone(&self) -> Self { Self(self.0.clone()) }
35    }
36    impl<T: Copy> Copy for Frac<T> {}
37    impl<T: fmt::Debug> fmt::Debug for Frac<T> {
38        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39            f.debug_tuple("Frac").field(&self.0).finish()
40        }
41    }
42    impl<T: fmt::Display> fmt::Display for Frac<T> {
43        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
44    }
45    impl<T: fmt::Binary> fmt::Binary for Frac<T> {
46        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Binary::fmt(&self.0, f) }
47    }
48    impl<T: fmt::Octal> fmt::Octal for Frac<T> {
49        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Octal::fmt(&self.0, f) }
50    }
51    impl<T: fmt::LowerHex> fmt::LowerHex for Frac<T> {
52        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) }
53    }
54    impl<T: fmt::UpperHex> fmt::UpperHex for Frac<T> {
55        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::UpperHex::fmt(&self.0, f) }
56    }
57    impl<T: fmt::UpperExp> fmt::UpperExp for Frac<T> {
58        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::UpperExp::fmt(&self.0, f) }
59    }
60    impl<T: fmt::LowerExp> fmt::LowerExp for Frac<T> {
61        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerExp::fmt(&self.0, f) }
62    }
63    impl<T: hash::Hash> hash::Hash for Frac<T> {
64        fn hash<H: hash::Hasher>(&self, state: &mut H) { self.0.hash(state); }
65    }
66    impl<T: hash::Hasher> hash::Hasher for Frac<T> {
67        #[must_use]
68        fn finish(&self) -> u64 { self.0.finish() }
69        fn write(&mut self, bytes: &[u8]) { self.0.write(bytes) }
70    }
71}