1macro_rules! impl_fmt {
4 (impl Fmt for $int:ident;) => {
5 __impl_fmt_base! { Binary for $int }
6 __impl_fmt_base! { Octal for $int }
7 __impl_fmt_base! { LowerHex for $int }
8 __impl_fmt_base! { UpperHex for $int }
9
10 impl ::core::fmt::Debug for $int {
11 #[inline]
12 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
13 #[allow(deprecated)]
16 let flags = f.flags();
17 const DEBUG_LOWER_HEX: u32 = 1 << 4;
18 const DEBUG_UPPER_HEX: u32 = 1 << 5;
19
20 if flags & DEBUG_LOWER_HEX != 0 {
21 ::core::fmt::LowerHex::fmt(self, f)
22 } else if flags & DEBUG_UPPER_HEX != 0 {
23 ::core::fmt::UpperHex::fmt(self, f)
24 } else {
25 ::core::fmt::Display::fmt(self, f)
26 }
27 }
28 }
29
30 impl ::core::fmt::Display for $int {
31 #[allow(unused_comparisons, unused_imports)]
32 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
33 use $crate::uint::AsU256;
34
35 let is_nonnegative = *self >= 0;
36 let n = if is_nonnegative {
37 self.as_u256()
38 } else {
39 (!self.as_u256()).wrapping_add($crate::uint::U256::ONE)
41 };
42 $crate::fmt::fmt_u256(n, is_nonnegative, f)
43 }
44 }
45
46 impl ::core::fmt::LowerExp for $int {
47 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
48 ::core::fmt::LowerExp::fmt(&self.as_f64(), f)
55 }
56 }
57
58 impl ::core::fmt::UpperExp for $int {
59 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
60 ::core::fmt::UpperExp::fmt(&self.as_f64(), f)
61 }
62 }
63 };
64}
65
66macro_rules! __impl_fmt_base {
67 ($base:ident for $int:ident) => {
68 impl ::core::fmt::$base for $int {
69 #[allow(unused_imports)]
70 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
71 use $crate::{fmt::GenericRadix, uint::AsU256};
72 let (abs, is_nonnegative) = if *self < 0 && f.sign_minus() {
73 (self.wrapping_neg(), false)
79 } else {
80 (*self, true)
81 };
82 $crate::fmt::$base.fmt_u256(abs.as_u256(), is_nonnegative, f)
83 }
84 }
85 };
86}