|
| 1 | +use core::fmt::{Display, Formatter, Result}; |
| 2 | + |
| 3 | +#[allow(missing_debug_implementations)] |
| 4 | +#[unstable(feature = "ub_checks", issue = "none")] |
| 5 | +pub enum DisplayWrapper<'a> { |
| 6 | + Bool(bool), |
| 7 | + Char(char), |
| 8 | + Str(&'a str), |
| 9 | + Ptr(*const ()), |
| 10 | + Uint(u128), |
| 11 | + Int(i128), |
| 12 | +} |
| 13 | + |
| 14 | +impl From<bool> for DisplayWrapper<'_> { |
| 15 | + fn from(b: bool) -> Self { |
| 16 | + Self::Bool(b) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl<'a> From<&'a str> for DisplayWrapper<'a> { |
| 21 | + fn from(s: &'a str) -> Self { |
| 22 | + Self::Str(s) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl From<char> for DisplayWrapper<'_> { |
| 27 | + fn from(c: char) -> Self { |
| 28 | + Self::Char(c) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl From<*const ()> for DisplayWrapper<'_> { |
| 33 | + fn from(c: *const ()) -> Self { |
| 34 | + Self::Ptr(c) |
| 35 | + } |
| 36 | +} |
| 37 | +impl From<*mut ()> for DisplayWrapper<'_> { |
| 38 | + fn from(c: *mut ()) -> Self { |
| 39 | + Self::Ptr(c as *const ()) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl From<u8> for DisplayWrapper<'_> { |
| 44 | + fn from(c: u8) -> Self { |
| 45 | + Self::Uint(c as u128) |
| 46 | + } |
| 47 | +} |
| 48 | +impl From<u16> for DisplayWrapper<'_> { |
| 49 | + fn from(c: u16) -> Self { |
| 50 | + Self::Uint(c as u128) |
| 51 | + } |
| 52 | +} |
| 53 | +impl From<u32> for DisplayWrapper<'_> { |
| 54 | + fn from(c: u32) -> Self { |
| 55 | + Self::Uint(c as u128) |
| 56 | + } |
| 57 | +} |
| 58 | +impl From<u64> for DisplayWrapper<'_> { |
| 59 | + fn from(c: u64) -> Self { |
| 60 | + Self::Uint(c as u128) |
| 61 | + } |
| 62 | +} |
| 63 | +impl From<u128> for DisplayWrapper<'_> { |
| 64 | + fn from(c: u128) -> Self { |
| 65 | + Self::Uint(c as u128) |
| 66 | + } |
| 67 | +} |
| 68 | +impl From<usize> for DisplayWrapper<'_> { |
| 69 | + fn from(c: usize) -> Self { |
| 70 | + Self::Uint(c as u128) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl From<i8> for DisplayWrapper<'_> { |
| 75 | + fn from(c: i8) -> Self { |
| 76 | + Self::Int(c as i128) |
| 77 | + } |
| 78 | +} |
| 79 | +impl From<i16> for DisplayWrapper<'_> { |
| 80 | + fn from(c: i16) -> Self { |
| 81 | + Self::Int(c as i128) |
| 82 | + } |
| 83 | +} |
| 84 | +impl From<i32> for DisplayWrapper<'_> { |
| 85 | + fn from(c: i32) -> Self { |
| 86 | + Self::Int(c as i128) |
| 87 | + } |
| 88 | +} |
| 89 | +impl From<i64> for DisplayWrapper<'_> { |
| 90 | + fn from(c: i64) -> Self { |
| 91 | + Self::Int(c as i128) |
| 92 | + } |
| 93 | +} |
| 94 | +impl From<i128> for DisplayWrapper<'_> { |
| 95 | + fn from(c: i128) -> Self { |
| 96 | + Self::Int(c as i128) |
| 97 | + } |
| 98 | +} |
| 99 | +impl From<isize> for DisplayWrapper<'_> { |
| 100 | + fn from(c: isize) -> Self { |
| 101 | + Self::Int(c as i128) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#[unstable(feature = "ub_checks", issue = "none")] |
| 106 | +impl Display for DisplayWrapper<'_> { |
| 107 | + #[inline] |
| 108 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result { |
| 109 | + const HEX: [u8; 16] = *b"0123456789abcdef"; |
| 110 | + let mut buf = [0u8; 42]; |
| 111 | + let mut cur = buf.len(); |
| 112 | + |
| 113 | + match *self { |
| 114 | + Self::Bool(_) | Self::Str(_) => panic!(), |
| 115 | + Self::Char(c) => { |
| 116 | + let mut buf = [0u8; 4]; |
| 117 | + let s = c.encode_utf8(&mut buf); |
| 118 | + return f.write_str(s); |
| 119 | + } |
| 120 | + Self::Ptr(ptr) => { |
| 121 | + let mut n = ptr.addr(); |
| 122 | + while n >= 16 { |
| 123 | + let d = n % 16; |
| 124 | + n /= 16; |
| 125 | + cur -= 1; |
| 126 | + buf[cur] = HEX[d]; |
| 127 | + } |
| 128 | + cur -= 1; |
| 129 | + buf[cur] = HEX[n]; |
| 130 | + |
| 131 | + cur -= 1; |
| 132 | + buf[cur] = b'x'; |
| 133 | + cur -= 1; |
| 134 | + buf[cur] = b'0'; |
| 135 | + } |
| 136 | + Self::Uint(mut n) => { |
| 137 | + while n >= 10 { |
| 138 | + let d = n % 10; |
| 139 | + n /= 10; |
| 140 | + cur -= 1; |
| 141 | + buf[cur] = (d as u8) + b'0'; |
| 142 | + } |
| 143 | + cur -= 1; |
| 144 | + buf[cur] = (n as u8) + b'0'; |
| 145 | + } |
| 146 | + Self::Int(n) => { |
| 147 | + let is_negative = n < 0; |
| 148 | + let mut n = (!(n as u128)).wrapping_add(1); |
| 149 | + |
| 150 | + while n >= 10 { |
| 151 | + let d = n % 10; |
| 152 | + n /= 10; |
| 153 | + cur -= 1; |
| 154 | + buf[cur] = (d as u8) + b'0'; |
| 155 | + } |
| 156 | + cur -= 1; |
| 157 | + buf[cur] = (n as u8) + b'0'; |
| 158 | + if is_negative { |
| 159 | + cur -= 1; |
| 160 | + buf[cur] = b'-'; |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + // SAFETY: The buffer is initially ASCII and we only write ASCII bytes to it. |
| 165 | + let s = unsafe { core::str::from_utf8_unchecked(&buf[cur..]) }; |
| 166 | + f.write_str(s) |
| 167 | + } |
| 168 | +} |
0 commit comments