diff options
| author | jam1garner <8260240+jam1garner@users.noreply.github.com> | 2021-11-26 13:59:57 -0500 |
|---|---|---|
| committer | jam1garner <8260240+jam1garner@users.noreply.github.com> | 2021-11-26 13:59:57 -0500 |
| commit | 37c8f254ede23a35167018fc9b672f5f7b098f52 (patch) | |
| tree | d01b9028a168a2fd061922e920993bbb917b604b | |
| parent | 454cc5fb86be180b3ec1138b6f2b480fbf3f1388 (diff) | |
| download | rust-37c8f254ede23a35167018fc9b672f5f7b098f52.tar.gz rust-37c8f254ede23a35167018fc9b672f5f7b098f52.zip | |
Use non-generic inner function for pointer formatting
| -rw-r--r-- | library/core/src/fmt/mod.rs | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 80d3270d73c..6fc3cd0b7c4 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -2186,28 +2186,34 @@ impl Display for char { #[stable(feature = "rust1", since = "1.0.0")] impl<T: ?Sized> Pointer for *const T { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - let old_width = f.width; - let old_flags = f.flags; - - // The alternate flag is already treated by LowerHex as being special- - // it denotes whether to prefix with 0x. We use it to work out whether - // or not to zero extend, and then unconditionally set it to get the - // prefix. - if f.alternate() { - f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32); - - if f.width.is_none() { - f.width = Some((usize::BITS / 4) as usize + 2); + /// Since the formatting will be identical for all pointer types, use a non-monomorphized + /// implementation for the actual formatting to reduce the amount of codegen work needed + fn inner(ptr: *const (), f: &mut Formatter<'_>) -> Result { + let old_width = f.width; + let old_flags = f.flags; + + // The alternate flag is already treated by LowerHex as being special- + // it denotes whether to prefix with 0x. We use it to work out whether + // or not to zero extend, and then unconditionally set it to get the + // prefix. + if f.alternate() { + f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32); + + if f.width.is_none() { + f.width = Some((usize::BITS / 4) as usize + 2); + } } - } - f.flags |= 1 << (FlagV1::Alternate as u32); + f.flags |= 1 << (FlagV1::Alternate as u32); + + let ret = LowerHex::fmt(&(ptr as usize), f); - let ret = LowerHex::fmt(&(*self as *const () as usize), f); + f.width = old_width; + f.flags = old_flags; - f.width = old_width; - f.flags = old_flags; + ret + } - ret + inner(*self as *const (), f) } } |
