|
| 1 | +use core::fmt::{Display, Formatter, Result}; |
| 2 | + |
| 3 | +#[allow(missing_debug_implementations)] |
| 4 | +#[unstable(feature = "ub_checks", issue = "none")] |
| 5 | +pub struct DisplayWrapper<T>(#[unstable(feature = "ub_checks", issue = "none")] pub T); |
| 6 | + |
| 7 | +trait Displayable: Sized + Clone + Copy { |
| 8 | + const IS_POINTER: bool; |
| 9 | + const SIGNED: bool; |
| 10 | + #[inline] |
| 11 | + fn addr(self) -> usize { |
| 12 | + unimplemented!() |
| 13 | + } |
| 14 | + #[inline] |
| 15 | + fn as_u128(self) -> u128 { |
| 16 | + unimplemented!() |
| 17 | + } |
| 18 | + #[inline] |
| 19 | + fn as_i128(self) -> i128 { |
| 20 | + unimplemented!() |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl<T> Displayable for *const T { |
| 25 | + const IS_POINTER: bool = true; |
| 26 | + const SIGNED: bool = false; |
| 27 | + #[inline] |
| 28 | + fn addr(self) -> usize { |
| 29 | + self.addr() |
| 30 | + } |
| 31 | +} |
| 32 | +impl<T> Displayable for *mut T { |
| 33 | + const IS_POINTER: bool = true; |
| 34 | + const SIGNED: bool = false; |
| 35 | + #[inline] |
| 36 | + fn addr(self) -> usize { |
| 37 | + self.addr() |
| 38 | + } |
| 39 | +} |
| 40 | +impl Displayable for u8 { |
| 41 | + const IS_POINTER: bool = false; |
| 42 | + const SIGNED: bool = false; |
| 43 | + #[inline] |
| 44 | + fn as_u128(self) -> u128 { |
| 45 | + self as u128 |
| 46 | + } |
| 47 | +} |
| 48 | +impl Displayable for u32 { |
| 49 | + const IS_POINTER: bool = false; |
| 50 | + const SIGNED: bool = false; |
| 51 | + #[inline] |
| 52 | + fn as_u128(self) -> u128 { |
| 53 | + self as u128 |
| 54 | + } |
| 55 | +} |
| 56 | +impl Displayable for u64 { |
| 57 | + const IS_POINTER: bool = false; |
| 58 | + const SIGNED: bool = false; |
| 59 | + #[inline] |
| 60 | + fn as_u128(self) -> u128 { |
| 61 | + self as u128 |
| 62 | + } |
| 63 | +} |
| 64 | +impl Displayable for usize { |
| 65 | + const IS_POINTER: bool = false; |
| 66 | + const SIGNED: bool = false; |
| 67 | + #[inline] |
| 68 | + fn as_u128(self) -> u128 { |
| 69 | + self as u128 |
| 70 | + } |
| 71 | +} |
| 72 | +impl Displayable for u128 { |
| 73 | + const IS_POINTER: bool = false; |
| 74 | + const SIGNED: bool = false; |
| 75 | + #[inline] |
| 76 | + fn as_u128(self) -> u128 { |
| 77 | + self |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl Displayable for isize { |
| 82 | + const IS_POINTER: bool = false; |
| 83 | + const SIGNED: bool = true; |
| 84 | + #[inline] |
| 85 | + fn as_i128(self) -> i128 { |
| 86 | + self as i128 |
| 87 | + } |
| 88 | +} |
| 89 | +impl Displayable for i128 { |
| 90 | + const IS_POINTER: bool = false; |
| 91 | + const SIGNED: bool = true; |
| 92 | + #[inline] |
| 93 | + fn as_i128(self) -> i128 { |
| 94 | + self |
| 95 | + } |
| 96 | +} |
| 97 | +#[unstable(feature = "ub_checks", issue = "none")] |
| 98 | +impl<T: Displayable> Display for DisplayWrapper<T> { |
| 99 | + #[inline] |
| 100 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result { |
| 101 | + const HEX: [u8; 16] = *b"0123456789abcdef"; |
| 102 | + let mut buf = [0u8; 42]; |
| 103 | + let mut cur = buf.len(); |
| 104 | + if T::IS_POINTER { |
| 105 | + let mut n = self.0.addr(); |
| 106 | + while n >= 16 { |
| 107 | + let d = n % 16; |
| 108 | + n /= 16; |
| 109 | + cur -= 1; |
| 110 | + buf[cur] = HEX[d]; |
| 111 | + } |
| 112 | + cur -= 1; |
| 113 | + buf[cur] = HEX[n]; |
| 114 | + |
| 115 | + cur -= 1; |
| 116 | + buf[cur] = b'x'; |
| 117 | + cur -= 1; |
| 118 | + buf[cur] = b'0'; |
| 119 | + } else { |
| 120 | + let mut is_negative = false; |
| 121 | + let mut n = if T::SIGNED { |
| 122 | + let signed = self.0.as_i128(); |
| 123 | + is_negative = signed < 0; |
| 124 | + (!(signed as u128)).wrapping_add(1) |
| 125 | + } else { |
| 126 | + self.0.as_u128() |
| 127 | + }; |
| 128 | + while n >= 10 { |
| 129 | + let d = n % 10; |
| 130 | + n /= 10; |
| 131 | + cur -= 1; |
| 132 | + buf[cur] = (d as u8) + b'0'; |
| 133 | + } |
| 134 | + cur -= 1; |
| 135 | + buf[cur] = (n as u8) + b'0'; |
| 136 | + if is_negative { |
| 137 | + cur -= 1; |
| 138 | + buf[cur] = b'-'; |
| 139 | + } |
| 140 | + } |
| 141 | + let s = unsafe { core::str::from_utf8_unchecked(&buf[cur..]) }; |
| 142 | + f.write_str(s) |
| 143 | + } |
| 144 | +} |
0 commit comments