From c5f73ed80c9c37cceaa25ed20c0f88f9d324bd8c Mon Sep 17 00:00:00 2001 From: Kamal Marhubi Date: Sun, 7 Feb 2016 17:57:01 -0500 Subject: [PATCH] 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 Pointer for *const T - impl Pointer for *mut T - impl fmt::Pointer for Box - impl fmt::Pointer for Rc - impl fmt::Pointer for Arc --- src/liballoc/arc.rs | 2 +- src/liballoc/boxed.rs | 2 +- src/liballoc/rc.rs | 2 +- src/libcore/fmt/mod.rs | 10 +++++----- src/libcoretest/fmt/mod.rs | 8 ++++++++ 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index 424a689bcb0..fa0185d5cfd 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -879,7 +879,7 @@ impl fmt::Debug for Arc { } #[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Pointer for Arc { +impl fmt::Pointer for Arc { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Pointer::fmt(&*self._ptr, f) } diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index be140469eb6..a7cd59d4dfe 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -442,7 +442,7 @@ impl fmt::Debug for Box { } #[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Pointer for Box { +impl fmt::Pointer for Box { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // It's not possible to extract the inner Uniq directly from the Box, // instead we cast it to a *const which aliases the Unique diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 3c4bea95ba1..162312e2457 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -688,7 +688,7 @@ impl fmt::Debug for Rc { } #[stable(feature = "rust1", since = "1.0.0")] -impl fmt::Pointer for Rc { +impl fmt::Pointer for Rc { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Pointer::fmt(&*self._ptr, f) } diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 37f03d731dc..28db24d3147 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -1384,7 +1384,7 @@ impl Display for char { } #[stable(feature = "rust1", since = "1.0.0")] -impl Pointer for *const T { +impl Pointer for *const T { fn fmt(&self, f: &mut Formatter) -> Result { let old_width = f.width; let old_flags = f.flags; @@ -1402,7 +1402,7 @@ impl Pointer for *const T { } f.flags |= 1 << (FlagV1::Alternate as u32); - let ret = LowerHex::fmt(&(*self as usize), f); + let ret = LowerHex::fmt(&(*self as *const () as usize), f); f.width = old_width; f.flags = old_flags; @@ -1412,7 +1412,7 @@ impl Pointer for *const T { } #[stable(feature = "rust1", since = "1.0.0")] -impl Pointer for *mut T { +impl Pointer for *mut T { fn fmt(&self, f: &mut Formatter) -> Result { // FIXME(#23542) Replace with type ascription. #![allow(trivial_casts)] @@ -1421,7 +1421,7 @@ impl Pointer for *mut T { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T> Pointer for &'a T { +impl<'a, T: ?Sized> Pointer for &'a T { fn fmt(&self, f: &mut Formatter) -> Result { // FIXME(#23542) Replace with type ascription. #![allow(trivial_casts)] @@ -1430,7 +1430,7 @@ impl<'a, T> Pointer for &'a T { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T> Pointer for &'a mut T { +impl<'a, T: ?Sized> Pointer for &'a mut T { fn fmt(&self, f: &mut Formatter) -> Result { // FIXME(#23542) Replace with type ascription. #![allow(trivial_casts)] diff --git a/src/libcoretest/fmt/mod.rs b/src/libcoretest/fmt/mod.rs index 99ea39c619f..ed33596e1c2 100644 --- a/src/libcoretest/fmt/mod.rs +++ b/src/libcoretest/fmt/mod.rs @@ -20,3 +20,11 @@ fn test_format_flags() { assert_eq!(format!("{: >3}", 'a'), " a"); } + +#[test] +fn test_pointer_formats_data_pointer() { + let b: &[u8] = b""; + let s: &str = ""; + assert_eq!(format!("{:p}", s), format!("{:p}", s.as_ptr())); + assert_eq!(format!("{:p}", b), format!("{:p}", b.as_ptr())); +}