Skip to content

Commit 4fbb2b1

Browse files
committed
fmt: Pad pointers to native pointer width
1 parent f65eb96 commit 4fbb2b1

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
@@ -14,6 +14,11 @@ use std::ptr;
1414
use std::rc::Rc;
1515
use std::sync::Arc;
1616

17+
#[cfg(target_pointer_width = "32")]
18+
const PTR: &'static str = "0x00000000";
19+
#[cfg(target_pointer_width = "64")]
20+
const PTR: &'static str = "0x0000000000000000";
21+
1722
fn main() {
1823
let p: *const libc::c_void = ptr::null();
1924
let rc = Rc::new(1usize);
@@ -24,5 +29,5 @@ fn main() {
2429
rc, arc, b);
2530

2631
assert_eq!(format!("{:p}", p),
27-
"0x0");
32+
PTR.to_string());
2833
}

0 commit comments

Comments
 (0)