diff options
| author | Richo Healey <richo@psych0tik.net> | 2015-04-07 17:53:55 -0700 |
|---|---|---|
| committer | Richo Healey <richo@psych0tik.net> | 2015-04-09 18:03:47 -0700 |
| commit | 333eb85d4b7dc51ada5202c35d5c67663111f403 (patch) | |
| tree | 188d018b2315941074cc0892c6a54dc75cd932d5 /src/libcore | |
| parent | 6436e348e97a09c2155d0dcd710416e6e0d84371 (diff) | |
| download | rust-333eb85d4b7dc51ada5202c35d5c67663111f403.tar.gz rust-333eb85d4b7dc51ada5202c35d5c67663111f403.zip | |
fmt: {:p#} formats pointers padded to native width
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/fmt/mod.rs | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index be804327663..2a7569e3b73 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -847,9 +847,33 @@ impl Display for char { #[stable(feature = "rust1", since = "1.0.0")] impl<T> 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.flags & 1 << (FlagV1::Alternate as u32) > 0 { + f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32); + + if let None = f.width { + // The formats need two extra bytes, for the 0x + if cfg!(target_pointer_width = "32") { + f.width = Some(10); + } + if cfg!(target_pointer_width = "64") { + f.width = Some(18); + } + } + } f.flags |= 1 << (FlagV1::Alternate as u32); + let ret = LowerHex::fmt(&(*self as usize), f); - f.flags &= !(1 << (FlagV1::Alternate as u32)); + + f.width = old_width; + f.flags = old_flags; + ret } } |
