|
| 1 | +use core::fmt::{Display, Formatter, Result}; |
| 2 | + |
| 3 | +#[allow(missing_debug_implementations)] |
| 4 | +pub struct DisplayWrapper<T>(pub T); |
| 5 | + |
| 6 | +macro_rules! display_int { |
| 7 | + ($ty:ty) => { |
| 8 | + impl Display for DisplayWrapper<$ty> { |
| 9 | + #[inline] |
| 10 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result { |
| 11 | + let n = self.0; |
| 12 | + let is_negative = n < 0; |
| 13 | + let n = (!(n as u128)).wrapping_add(1); |
| 14 | + display_int(n, is_negative, f) |
| 15 | + } |
| 16 | + } |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +display_int!(i8); |
| 21 | +display_int!(i16); |
| 22 | +display_int!(i32); |
| 23 | +display_int!(i64); |
| 24 | +display_int!(i128); |
| 25 | +display_int!(isize); |
| 26 | + |
| 27 | +macro_rules! display_uint { |
| 28 | + ($ty:ty) => { |
| 29 | + impl Display for DisplayWrapper<$ty> { |
| 30 | + #[inline] |
| 31 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result { |
| 32 | + display_int(self.0 as u128, false, f) |
| 33 | + } |
| 34 | + } |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +display_uint!(u8); |
| 39 | +display_uint!(u16); |
| 40 | +display_uint!(u32); |
| 41 | +display_uint!(u64); |
| 42 | +display_uint!(u128); |
| 43 | +display_uint!(usize); |
| 44 | + |
| 45 | +impl Display for DisplayWrapper<*const ()> { |
| 46 | + #[inline] |
| 47 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result { |
| 48 | + format_ptr(self.0.addr(), f) |
| 49 | + } |
| 50 | +} |
| 51 | +impl Display for DisplayWrapper<*mut ()> { |
| 52 | + #[inline] |
| 53 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result { |
| 54 | + format_ptr(self.0.addr(), f) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[inline] |
| 59 | +fn format_ptr(addr: usize, f: &mut Formatter<'_>) -> Result { |
| 60 | + const HEX: [u8; 16] = *b"0123456789abcdef"; |
| 61 | + let mut buf = [0u8; 42]; |
| 62 | + let mut cur = buf.len(); |
| 63 | + |
| 64 | + let mut n = addr; |
| 65 | + while n >= 16 { |
| 66 | + let d = n % 16; |
| 67 | + n /= 16; |
| 68 | + cur -= 1; |
| 69 | + buf[cur] = HEX[d]; |
| 70 | + } |
| 71 | + cur -= 1; |
| 72 | + buf[cur] = HEX[n]; |
| 73 | + |
| 74 | + cur -= 1; |
| 75 | + buf[cur] = b'x'; |
| 76 | + cur -= 1; |
| 77 | + buf[cur] = b'0'; |
| 78 | + |
| 79 | + // SAFETY: The buffer is initially ASCII and we only write ASCII bytes to it. |
| 80 | + let s = unsafe { core::str::from_utf8_unchecked(&buf[cur..]) }; |
| 81 | + f.write_str(s) |
| 82 | +} |
| 83 | + |
| 84 | +impl Display for DisplayWrapper<char> { |
| 85 | + #[inline] |
| 86 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result { |
| 87 | + let mut buf = [0u8; 4]; |
| 88 | + let s = self.0.encode_utf8(&mut buf); |
| 89 | + f.write_str(s) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl Display for DisplayWrapper<bool> { |
| 94 | + #[inline] |
| 95 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result { |
| 96 | + let s = match self.0 { |
| 97 | + true => "true", |
| 98 | + false => "false", |
| 99 | + }; |
| 100 | + f.write_str(s) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +#[inline] |
| 105 | +fn display_int(mut n: u128, is_negative: bool, f: &mut Formatter<'_>) -> Result { |
| 106 | + let mut buf = [0u8; 42]; |
| 107 | + let mut cur = buf.len(); |
| 108 | + |
| 109 | + while n >= 10 { |
| 110 | + let d = n % 10; |
| 111 | + n /= 10; |
| 112 | + cur -= 1; |
| 113 | + buf[cur] = (d as u8) + b'0'; |
| 114 | + } |
| 115 | + cur -= 1; |
| 116 | + buf[cur] = (n as u8) + b'0'; |
| 117 | + if is_negative { |
| 118 | + cur -= 1; |
| 119 | + buf[cur] = b'-'; |
| 120 | + } |
| 121 | + // SAFETY: The buffer is initially ASCII and we only write ASCII bytes to it. |
| 122 | + let s = unsafe { core::str::from_utf8_unchecked(&buf[cur..]) }; |
| 123 | + f.write_str(s) |
| 124 | +} |
0 commit comments