Skip to content

Commit 09fc998

Browse files
committed
fmt: Pad pointers to native pointer width
1 parent 7b01e08 commit 09fc998

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/libcore/fmt/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -844,12 +844,29 @@ impl Display for char {
844844
}
845845
}
846846

847+
// Two extra bytes for 0x
848+
#[cfg(target_pointer_width = "32")]
849+
const POINTER_PADDING: Option<usize> = Some(10);
850+
#[cfg(target_pointer_width = "64")]
851+
const POINTER_PADDING: Option<usize> = Some(18);
852+
847853
#[stable(feature = "rust1", since = "1.0.0")]
848854
impl<T> Pointer for *const T {
849855
fn fmt(&self, f: &mut Formatter) -> Result {
850856
f.flags |= 1 << (FlagV1::Alternate as u32);
857+
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
858+
859+
let old_width = f.width;
860+
if let None = f.width {
861+
f.width = POINTER_PADDING;
862+
}
863+
851864
let ret = LowerHex::fmt(&(*self as usize), f);
865+
866+
f.width = old_width;
852867
f.flags &= !(1 << (FlagV1::Alternate as u32));
868+
f.flags &= !(1 << (FlagV1::SignAwareZeroPad as u32));
869+
853870
ret
854871
}
855872
}

src/test/run-pass/fmt-pointer-trait.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ use std::ptr;
44
use std::rc::Rc;
55
use std::sync::Arc;
66

7+
#[cfg(target_pointer_width = "32")]
8+
const PTR: &'static str = "0x00000000";
9+
#[cfg(target_pointer_width = "64")]
10+
const PTR: &'static str = "0x0000000000000000";
11+
712
fn main() {
813
let p: *const libc::c_void = ptr::null();
914
let rc = Rc::new(1usize);
@@ -14,5 +19,5 @@ fn main() {
1419
rc, arc, b);
1520

1621
assert_eq!(format!("{:p}", p),
17-
"0x0");
22+
PTR.to_string());
1823
}

0 commit comments

Comments
 (0)