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