reweb3_num/bint/
fmt.rs

1use core::fmt::{Binary, Debug, Display, Formatter, LowerExp, LowerHex, Octal, UpperExp, UpperHex};
2
3macro_rules! fmt_trait {
4    ($BInt: ident, $trait: tt) => {
5        impl<const N: usize> $trait for $BInt<N> {
6            #[inline]
7            fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
8                $trait::fmt(&self.bits, f)
9            }
10        }
11    };
12}
13
14macro_rules! fmt {
15    ($BUint: ident, $BInt: ident, $Digit: ident) => {
16        fmt_trait!($BInt, Binary);
17
18        impl<const N: usize> Display for $BInt<N> {
19            #[inline]
20            fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
21                f.pad_integral(!self.is_negative(), "", &format!("{}", self.unsigned_abs()))
22            }
23        }
24
25        impl<const N: usize> Debug for $BInt<N> {
26            #[inline]
27            fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
28                Display::fmt(&self, f)
29            }
30        }
31
32        impl<const N: usize> LowerExp for $BInt<N> {
33            #[inline]
34            fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
35                let uint = self.unsigned_abs();
36                f.pad_integral(!self.is_negative(), "", &format!("{:e}", uint))
37            }
38        }
39        fmt_trait!($BInt, LowerHex);
40        fmt_trait!($BInt, Octal);
41
42        impl<const N: usize> UpperExp for $BInt<N> {
43            #[inline]
44            fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
45                let uint = self.unsigned_abs();
46                f.pad_integral(!self.is_negative(), "", &format!("{:E}", uint))
47            }
48        }
49
50        fmt_trait!($BInt, UpperHex);
51    };
52}
53
54crate::macro_impl!(fmt);