Auto merge of #24186 - richo:pad-pointers, r=alexcrichton
This pads out the printing of pointers to their native width. Extracted from and rebased on top of #24144
This commit is contained in:
commit
c87ec1edb1
3 changed files with 40 additions and 4 deletions
|
@ -847,9 +847,32 @@ impl Display for char {
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<T> Pointer for *const T {
|
impl<T> Pointer for *const T {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
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);
|
||||||
|
} else {
|
||||||
|
f.width = Some(18);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
f.flags |= 1 << (FlagV1::Alternate as u32);
|
f.flags |= 1 << (FlagV1::Alternate as u32);
|
||||||
|
|
||||||
let ret = LowerHex::fmt(&(*self as usize), f);
|
let ret = LowerHex::fmt(&(*self as usize), f);
|
||||||
f.flags &= !(1 << (FlagV1::Alternate as u32));
|
|
||||||
|
f.width = old_width;
|
||||||
|
f.flags = old_flags;
|
||||||
|
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,13 @@ fn main() {
|
||||||
let _ = format!("{:p}{:p}{:p}",
|
let _ = format!("{:p}{:p}{:p}",
|
||||||
rc, arc, b);
|
rc, arc, b);
|
||||||
|
|
||||||
|
if cfg!(target_pointer_width = "32") {
|
||||||
|
assert_eq!(format!("{:#p}", p),
|
||||||
|
"0x00000000");
|
||||||
|
} else {
|
||||||
|
assert_eq!(format!("{:#p}", p),
|
||||||
|
"0x0000000000000000");
|
||||||
|
}
|
||||||
assert_eq!(format!("{:p}", p),
|
assert_eq!(format!("{:p}", p),
|
||||||
"0x0");
|
"0x0");
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,6 +72,13 @@ pub fn main() {
|
||||||
t!(format!("{:X}", 10_usize), "A");
|
t!(format!("{:X}", 10_usize), "A");
|
||||||
t!(format!("{}", "foo"), "foo");
|
t!(format!("{}", "foo"), "foo");
|
||||||
t!(format!("{}", "foo".to_string()), "foo");
|
t!(format!("{}", "foo".to_string()), "foo");
|
||||||
|
if cfg!(target_pointer_width = "32") {
|
||||||
|
t!(format!("{:#p}", 0x1234 as *const isize), "0x00001234");
|
||||||
|
t!(format!("{:#p}", 0x1234 as *mut isize), "0x00001234");
|
||||||
|
} else {
|
||||||
|
t!(format!("{:#p}", 0x1234 as *const isize), "0x0000000000001234");
|
||||||
|
t!(format!("{:#p}", 0x1234 as *mut isize), "0x0000000000001234");
|
||||||
|
}
|
||||||
t!(format!("{:p}", 0x1234 as *const isize), "0x1234");
|
t!(format!("{:p}", 0x1234 as *const isize), "0x1234");
|
||||||
t!(format!("{:p}", 0x1234 as *mut isize), "0x1234");
|
t!(format!("{:p}", 0x1234 as *mut isize), "0x1234");
|
||||||
t!(format!("{:x}", A), "aloha");
|
t!(format!("{:x}", A), "aloha");
|
||||||
|
@ -85,9 +92,8 @@ pub fn main() {
|
||||||
t!(format!("{}", 5 + 5), "10");
|
t!(format!("{}", 5 + 5), "10");
|
||||||
t!(format!("{:#4}", C), "☃123");
|
t!(format!("{:#4}", C), "☃123");
|
||||||
|
|
||||||
// FIXME(#20676)
|
let a: &fmt::Debug = &1;
|
||||||
// let a: &fmt::Debug = &1;
|
t!(format!("{:?}", a), "1");
|
||||||
// t!(format!("{:?}", a), "1");
|
|
||||||
|
|
||||||
|
|
||||||
// Formatting strings and their arguments
|
// Formatting strings and their arguments
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue