Skip to content

Commit c5f73ed

Browse files
committed
Implement fmt::Pointer for pointers to unsized types
This allows printing pointers to unsized types with the {:p} formatting directive. The following impls are extended to unsized types: - impl<'a, T: ?Sized> Pointer for &'a T - impl<'a, T: ?Sized> Pointer for &'a mut T - impl<T: ?Sized> Pointer for *const T - impl<T: ?Sized> Pointer for *mut T - impl<T: ?Sized> fmt::Pointer for Box<T> - impl<T: ?Sized> fmt::Pointer for Rc<T> - impl<T: ?Sized> fmt::Pointer for Arc<T>
1 parent 26105b1 commit c5f73ed

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

src/liballoc/arc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Arc<T> {
879879
}
880880

881881
#[stable(feature = "rust1", since = "1.0.0")]
882-
impl<T> fmt::Pointer for Arc<T> {
882+
impl<T: ?Sized> fmt::Pointer for Arc<T> {
883883
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
884884
fmt::Pointer::fmt(&*self._ptr, f)
885885
}

src/liballoc/boxed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ impl<T: fmt::Debug + ?Sized> fmt::Debug for Box<T> {
442442
}
443443

444444
#[stable(feature = "rust1", since = "1.0.0")]
445-
impl<T> fmt::Pointer for Box<T> {
445+
impl<T: ?Sized> fmt::Pointer for Box<T> {
446446
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
447447
// It's not possible to extract the inner Uniq directly from the Box,
448448
// instead we cast it to a *const which aliases the Unique

src/liballoc/rc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Rc<T> {
688688
}
689689

690690
#[stable(feature = "rust1", since = "1.0.0")]
691-
impl<T> fmt::Pointer for Rc<T> {
691+
impl<T: ?Sized> fmt::Pointer for Rc<T> {
692692
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
693693
fmt::Pointer::fmt(&*self._ptr, f)
694694
}

src/libcore/fmt/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,7 @@ impl Display for char {
13841384
}
13851385

13861386
#[stable(feature = "rust1", since = "1.0.0")]
1387-
impl<T> Pointer for *const T {
1387+
impl<T: ?Sized> Pointer for *const T {
13881388
fn fmt(&self, f: &mut Formatter) -> Result {
13891389
let old_width = f.width;
13901390
let old_flags = f.flags;
@@ -1402,7 +1402,7 @@ impl<T> Pointer for *const T {
14021402
}
14031403
f.flags |= 1 << (FlagV1::Alternate as u32);
14041404

1405-
let ret = LowerHex::fmt(&(*self as usize), f);
1405+
let ret = LowerHex::fmt(&(*self as *const () as usize), f);
14061406

14071407
f.width = old_width;
14081408
f.flags = old_flags;
@@ -1412,7 +1412,7 @@ impl<T> Pointer for *const T {
14121412
}
14131413

14141414
#[stable(feature = "rust1", since = "1.0.0")]
1415-
impl<T> Pointer for *mut T {
1415+
impl<T: ?Sized> Pointer for *mut T {
14161416
fn fmt(&self, f: &mut Formatter) -> Result {
14171417
// FIXME(#23542) Replace with type ascription.
14181418
#![allow(trivial_casts)]
@@ -1421,7 +1421,7 @@ impl<T> Pointer for *mut T {
14211421
}
14221422

14231423
#[stable(feature = "rust1", since = "1.0.0")]
1424-
impl<'a, T> Pointer for &'a T {
1424+
impl<'a, T: ?Sized> Pointer for &'a T {
14251425
fn fmt(&self, f: &mut Formatter) -> Result {
14261426
// FIXME(#23542) Replace with type ascription.
14271427
#![allow(trivial_casts)]
@@ -1430,7 +1430,7 @@ impl<'a, T> Pointer for &'a T {
14301430
}
14311431

14321432
#[stable(feature = "rust1", since = "1.0.0")]
1433-
impl<'a, T> Pointer for &'a mut T {
1433+
impl<'a, T: ?Sized> Pointer for &'a mut T {
14341434
fn fmt(&self, f: &mut Formatter) -> Result {
14351435
// FIXME(#23542) Replace with type ascription.
14361436
#![allow(trivial_casts)]

src/libcoretest/fmt/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ fn test_format_flags() {
2020

2121
assert_eq!(format!("{: >3}", 'a'), " a");
2222
}
23+
24+
#[test]
25+
fn test_pointer_formats_data_pointer() {
26+
let b: &[u8] = b"";
27+
let s: &str = "";
28+
assert_eq!(format!("{:p}", s), format!("{:p}", s.as_ptr()));
29+
assert_eq!(format!("{:p}", b), format!("{:p}", b.as_ptr()));
30+
}

0 commit comments

Comments
 (0)