File tree 3 files changed +34
-1
lines changed
3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -844,12 +844,32 @@ impl Display for char {
844
844
}
845
845
}
846
846
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
+
847
853
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
848
854
impl < T > Pointer for * const T {
849
855
fn fmt ( & self , f : & mut Formatter ) -> Result {
856
+ let old_width = f. width ;
857
+ let old_flags = f. flags ;
858
+
859
+ if f. flags & 1 << ( FlagV1 :: Alternate as u32 ) > 0 {
860
+ f. flags |= 1 << ( FlagV1 :: SignAwareZeroPad as u32 ) ;
861
+
862
+ if let None = f. width {
863
+ f. width = POINTER_PADDING ;
864
+ }
865
+ }
850
866
f. flags |= 1 << ( FlagV1 :: Alternate as u32 ) ;
867
+
851
868
let ret = LowerHex :: fmt ( & ( * self as usize ) , f) ;
852
- f. flags &= !( 1 << ( FlagV1 :: Alternate as u32 ) ) ;
869
+
870
+ f. width = old_width;
871
+ f. flags = old_flags;
872
+
853
873
ret
854
874
}
855
875
}
Original file line number Diff line number Diff line change @@ -14,6 +14,11 @@ use std::ptr;
14
14
use std:: rc:: Rc ;
15
15
use std:: sync:: Arc ;
16
16
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
+
17
22
fn main ( ) {
18
23
let p: * const libc:: c_void = ptr:: null ( ) ;
19
24
let rc = Rc :: new ( 1usize ) ;
@@ -23,6 +28,8 @@ fn main() {
23
28
let _ = format ! ( "{:p}{:p}{:p}" ,
24
29
rc, arc, b) ;
25
30
31
+ assert_eq ! ( format!( "{:#p}" , p) ,
32
+ PTR . to_string( ) ) ;
26
33
assert_eq ! ( format!( "{:p}" , p) ,
27
34
"0x0" ) ;
28
35
}
Original file line number Diff line number Diff line change @@ -42,6 +42,10 @@ impl fmt::Display for C {
42
42
macro_rules! t {
43
43
( $a: expr, $b: expr) => { assert_eq!( $a, $b) }
44
44
}
45
+ #[ cfg( target_pointer_width = "32" ) ]
46
+ const PTR : & ' static str = "0x00001234" ;
47
+ #[ cfg( target_pointer_width = "64" ) ]
48
+ const PTR : & ' static str = "0x0000000000001234" ;
45
49
46
50
pub fn main ( ) {
47
51
// Various edge cases without formats
@@ -72,6 +76,8 @@ pub fn main() {
72
76
t ! ( format!( "{:X}" , 10_usize ) , "A" ) ;
73
77
t ! ( format!( "{}" , "foo" ) , "foo" ) ;
74
78
t ! ( format!( "{}" , "foo" . to_string( ) ) , "foo" ) ;
79
+ t ! ( format!( "{:#p}" , 0x1234 as * const isize ) , PTR ) ;
80
+ t ! ( format!( "{:#p}" , 0x1234 as * mut isize ) , PTR ) ;
75
81
t ! ( format!( "{:p}" , 0x1234 as * const isize ) , "0x1234" ) ;
76
82
t ! ( format!( "{:p}" , 0x1234 as * mut isize ) , "0x1234" ) ;
77
83
t ! ( format!( "{:x}" , A ) , "aloha" ) ;
You can’t perform that action at this time.
0 commit comments