From a329a61b9bf01bfc3f9f7316bb800b2bc8b76382 Mon Sep 17 00:00:00 2001 From: Richo Healey Date: Tue, 7 Apr 2015 00:40:22 -0700 Subject: [PATCH] alloc: impl fmt::Pointer for Rc, Arc and Box Closes #24091 --- src/liballoc/arc.rs | 7 +++++++ src/liballoc/boxed.rs | 10 +++++++++ src/liballoc/rc.rs | 7 +++++++ src/libcore/ptr.rs | 8 ++++++++ src/libsyntax/ptr.rs | 7 +++++++ src/test/run-pass/fmt-pointer-trait.rs | 28 ++++++++++++++++++++++++++ 6 files changed, 67 insertions(+) create mode 100644 src/test/run-pass/fmt-pointer-trait.rs diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index 68bde147611..ff176d81591 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -668,6 +668,13 @@ impl fmt::Debug for Arc { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl fmt::Pointer for Arc { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Pointer::fmt(&*self._ptr, f) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Default for Arc { #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 2801cf38cb7..4468e425a85 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -275,6 +275,16 @@ impl fmt::Debug for Box { } } +#[stable(feature = "rust1", since = "1.0.0")] +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 + let ptr: *const T = &**self; + fmt::Pointer::fmt(&ptr, f) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Deref for Box { type Target = T; diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 56822cfe28a..67805d10a4a 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -634,6 +634,13 @@ impl fmt::Debug for Rc { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl fmt::Pointer for Rc { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Pointer::fmt(&*self._ptr, f) + } +} + /// A weak version of `Rc`. /// /// Weak references do not count when determining if the inner value should be diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index ff51e25fcbf..36b33e7581d 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -94,6 +94,7 @@ use mem; use clone::Clone; use intrinsics; use ops::Deref; +use core::fmt; use option::Option::{self, Some, None}; use marker::{PhantomData, Send, Sized, Sync}; use nonzero::NonZero; @@ -570,3 +571,10 @@ impl Deref for Unique { unsafe { mem::transmute(&*self.pointer) } } } + +#[stable(feature = "rust1", since = "1.0.0")] +impl fmt::Pointer for Unique { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Pointer::fmt(&*self.pointer, f) + } +} diff --git a/src/libsyntax/ptr.rs b/src/libsyntax/ptr.rs index 7e0bcd3e1dc..5032cd57eeb 100644 --- a/src/libsyntax/ptr.rs +++ b/src/libsyntax/ptr.rs @@ -111,6 +111,13 @@ impl Display for P { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl fmt::Pointer for P { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Pointer::fmt(&self.ptr, f) + } +} + impl Hash for P { fn hash(&self, state: &mut H) { (**self).hash(state); diff --git a/src/test/run-pass/fmt-pointer-trait.rs b/src/test/run-pass/fmt-pointer-trait.rs new file mode 100644 index 00000000000..be8ecde6783 --- /dev/null +++ b/src/test/run-pass/fmt-pointer-trait.rs @@ -0,0 +1,28 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(libc)] +extern crate libc; +use std::ptr; +use std::rc::Rc; +use std::sync::Arc; + +fn main() { + let p: *const libc::c_void = ptr::null(); + let rc = Rc::new(1usize); + let arc = Arc::new(1usize); + let b = Box::new("hi"); + + let _ = format!("{:p}{:p}{:p}", + rc, arc, b); + + assert_eq!(format!("{:p}", p), + "0x0"); +}