Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0bca351

Browse files
committedJun 13, 2021
fmt: Format fat pointers as (addr, meta) for better insight
Use the new ptr::metadata() fn to improve debug prints of fat pointers. Pointee::Metadata gets an additional Debug bound, impls already implemented it anyway.
1 parent fb3ea63 commit 0bca351

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed
 

‎library/core/src/fmt/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use crate::cell::{Cell, Ref, RefCell, RefMut, UnsafeCell};
66
use crate::char::EscapeDebugExtArgs;
7+
use crate::intrinsics::size_of;
78
use crate::iter;
89
use crate::marker::PhantomData;
910
use crate::mem;
1011
use crate::num::flt2dec;
1112
use crate::ops::Deref;
13+
use crate::ptr;
1214
use crate::result;
1315
use crate::str;
1416

@@ -2130,6 +2132,15 @@ impl Display for char {
21302132
#[stable(feature = "rust1", since = "1.0.0")]
21312133
impl<T: ?Sized> Pointer for *const T {
21322134
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2135+
if size_of::<*const T>() > size_of::<*const ()>() {
2136+
// Fat pointer, format as (address, metadata)
2137+
let meta = ptr::metadata(*self);
2138+
return f.debug_tuple("")
2139+
.field(&(*self as *const ()))
2140+
.field(&meta)
2141+
.finish();
2142+
}
2143+
21332144
let old_width = f.width;
21342145
let old_flags = f.flags;
21352146

‎library/core/src/ptr/metadata.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![unstable(feature = "ptr_metadata", issue = "81513")]
22

3-
use crate::fmt;
3+
use crate::fmt::{self, Debug};
44
use crate::hash::{Hash, Hasher};
55

66
/// Provides the pointer metadata type of any pointed-to type.
@@ -56,7 +56,7 @@ pub trait Pointee {
5656
// NOTE: Keep trait bounds in `static_assert_expected_bounds_for_metadata`
5757
// in `library/core/src/ptr/metadata.rs`
5858
// in sync with those here:
59-
type Metadata: Copy + Send + Sync + Ord + Hash + Unpin;
59+
type Metadata: Copy + Send + Sync + Ord + Hash + Unpin + Debug;
6060
}
6161

6262
/// Pointers to types implementing this trait alias are “thin”.

‎library/core/tests/fmt/mod.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ mod builders;
22
mod float;
33
mod num;
44

5+
use core::any;
6+
use core::fmt;
7+
use core::ptr;
8+
59
#[test]
610
fn test_format_flags() {
711
// No residual flags left by pointer formatting
@@ -13,10 +17,20 @@ fn test_format_flags() {
1317

1418
#[test]
1519
fn test_pointer_formats_data_pointer() {
16-
let b: &[u8] = b"";
17-
let s: &str = "";
18-
assert_eq!(format!("{:p}", s), format!("{:p}", s.as_ptr()));
19-
assert_eq!(format!("{:p}", b), format!("{:p}", b.as_ptr()));
20+
// Thin ptr
21+
let thinptr = &42 as *const i32;
22+
assert_eq!(format!("{:p}", thinptr), format!("{:?}", thinptr as *const ()));
23+
24+
// Ptr with length
25+
let b: &[u8] = b"hello";
26+
let s: &str = "hello";
27+
assert_eq!(format!("{:p}", b), format!("({:?}, 5)", b.as_ptr()));
28+
assert_eq!(format!("{:p}", s), format!("({:?}, 5)", s.as_ptr()));
29+
30+
// Ptr with v-table
31+
let mut any: Box<dyn any::Any> = Box::new(42);
32+
let dyn_ptr = &mut *any as *mut dyn any::Any;
33+
assert_eq!(format!("{:p}", dyn_ptr), format!("({:?}, {:?})", dyn_ptr as *const (), ptr::metadata(dyn_ptr)));
2034
}
2135

2236
#[test]
@@ -33,8 +47,8 @@ fn test_estimated_capacity() {
3347
fn pad_integral_resets() {
3448
struct Bar;
3549

36-
impl core::fmt::Display for Bar {
37-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
50+
impl fmt::Display for Bar {
51+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3852
"1".fmt(f)?;
3953
f.pad_integral(true, "", "5")?;
4054
"1".fmt(f)

‎library/core/tests/ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ fn ptr_metadata_bounds() {
500500
fn static_assert_expected_bounds_for_metadata<Meta>()
501501
where
502502
// Keep this in sync with the associated type in `library/core/src/ptr/metadata.rs`
503-
Meta: Copy + Send + Sync + Ord + std::hash::Hash + Unpin,
503+
Meta: Copy + Send + Sync + Ord + std::hash::Hash + Unpin + Debug,
504504
{
505505
}
506506
}

0 commit comments

Comments
 (0)
Failed to load comments.