Skip to content

Commit a329a61

Browse files
committed
alloc: impl fmt::Pointer for Rc, Arc and Box
Closes rust-lang#24091
1 parent b2e65ee commit a329a61

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

src/liballoc/arc.rs

+7
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,13 @@ impl<T: fmt::Debug> fmt::Debug for Arc<T> {
668668
}
669669
}
670670

671+
#[stable(feature = "rust1", since = "1.0.0")]
672+
impl<T> fmt::Pointer for Arc<T> {
673+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
674+
fmt::Pointer::fmt(&*self._ptr, f)
675+
}
676+
}
677+
671678
#[stable(feature = "rust1", since = "1.0.0")]
672679
impl<T: Default + Sync + Send> Default for Arc<T> {
673680
#[stable(feature = "rust1", since = "1.0.0")]

src/liballoc/boxed.rs

+10
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,16 @@ impl<T: fmt::Debug + ?Sized> fmt::Debug for Box<T> {
275275
}
276276
}
277277

278+
#[stable(feature = "rust1", since = "1.0.0")]
279+
impl<T> fmt::Pointer for Box<T> {
280+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
281+
// It's not possible to extract the inner Uniq directly from the Box,
282+
// instead we cast it to a *const which aliases the Unique
283+
let ptr: *const T = &**self;
284+
fmt::Pointer::fmt(&ptr, f)
285+
}
286+
}
287+
278288
#[stable(feature = "rust1", since = "1.0.0")]
279289
impl<T: ?Sized> Deref for Box<T> {
280290
type Target = T;

src/liballoc/rc.rs

+7
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,13 @@ impl<T: fmt::Debug> fmt::Debug for Rc<T> {
634634
}
635635
}
636636

637+
#[stable(feature = "rust1", since = "1.0.0")]
638+
impl<T> fmt::Pointer for Rc<T> {
639+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
640+
fmt::Pointer::fmt(&*self._ptr, f)
641+
}
642+
}
643+
637644
/// A weak version of `Rc<T>`.
638645
///
639646
/// Weak references do not count when determining if the inner value should be

src/libcore/ptr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ use mem;
9494
use clone::Clone;
9595
use intrinsics;
9696
use ops::Deref;
97+
use core::fmt;
9798
use option::Option::{self, Some, None};
9899
use marker::{PhantomData, Send, Sized, Sync};
99100
use nonzero::NonZero;
@@ -570,3 +571,10 @@ impl<T:?Sized> Deref for Unique<T> {
570571
unsafe { mem::transmute(&*self.pointer) }
571572
}
572573
}
574+
575+
#[stable(feature = "rust1", since = "1.0.0")]
576+
impl<T> fmt::Pointer for Unique<T> {
577+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
578+
fmt::Pointer::fmt(&*self.pointer, f)
579+
}
580+
}

src/libsyntax/ptr.rs

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ impl<T: Display> Display for P<T> {
111111
}
112112
}
113113

114+
#[stable(feature = "rust1", since = "1.0.0")]
115+
impl<T> fmt::Pointer for P<T> {
116+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117+
fmt::Pointer::fmt(&self.ptr, f)
118+
}
119+
}
120+
114121
impl<T: Hash> Hash for P<T> {
115122
fn hash<H: Hasher>(&self, state: &mut H) {
116123
(**self).hash(state);
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(libc)]
12+
extern crate libc;
13+
use std::ptr;
14+
use std::rc::Rc;
15+
use std::sync::Arc;
16+
17+
fn main() {
18+
let p: *const libc::c_void = ptr::null();
19+
let rc = Rc::new(1usize);
20+
let arc = Arc::new(1usize);
21+
let b = Box::new("hi");
22+
23+
let _ = format!("{:p}{:p}{:p}",
24+
rc, arc, b);
25+
26+
assert_eq!(format!("{:p}", p),
27+
"0x0");
28+
}

0 commit comments

Comments
 (0)