-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Debug-format fat pointers with their metadata for better insight #93544
base: master
Are you sure you want to change the base?
Conversation
r? @m-ou-se (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
09e6e9b
to
f35a9a5
Compare
This comment has been minimized.
This comment has been minimized.
Man, I get the weirdest compiler errors... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Just two small nits:
Use ptr::metadata() fn to improve debug prints of fat pointers. Co-authored-by: Mara Bos <m-ou.se@m-ou.se>
f7b56b2
to
30e5888
Compare
The job Click to see the possible cause of the failure (guessed by this bot)
|
r? @oli-obk I'll look at the failure tomorrow |
The bug here was fixed by #92248, you should be able to use |
r? @m-ou-se |
impl PtrMetadataFmt for usize { | ||
#[inline] | ||
fn fmt_ptr(&self, ptr: *const (), f: &mut Formatter<'_>) -> Result { | ||
write!(f, "({:p}, {})", ptr, *self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(bikeshedding) I wonder how silly something like *[_] { data: 0x..., len: 123 }
would be. (and have meta
instead of len
in the general case, I guess?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand, what's the *[_]
part?
// Regular pointer / default impl | ||
impl<T> PtrMetadataFmt for T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to improve this in one of two ways:
- split the
()
case from the default:()
is "guaranteed simple" (but maybe specializing on the original pointee beingSized
would be stronger)- the default would only be hit by custom DSTs (and unreachable today? could print some indication of metadata existing but not being understood, or just fit an
unreachable!()
in there)
- add a new sealed trait, and a bound for it on
Pointee::Metadata
(just like it requiresCopy
,Eq
, etc. today)- ("sealed" as in a
pub trait
in a privatemod
, not nameable outsidecore
- for now, anyway) - the new trait could just be this PR's
PtrMetadataFmt
for now, even if we may want to change it in the future (see below) - no specialization should be needed, since
Pointee
would be claiming it's always implemented for valid metadata types - whenever we get custom DSTs, it would be a nice bonus to force the user to implement such a trait, effectively making them decide how raw pointers to their custom DST should be formatted
- this is when we'd remove the "sealing"
- maybe it should just be a
fmt::Debug
bound and not let the user see the data pointer or choose how it is printed? - but this part is really all a bikshed for the future
- ("sealed" as in a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know exactly what you mean, in fact, I've been meaning to propose in #81513 that Pointee::Metadata
should maybe be its own trait (and yeah initially sealed by all means), which could enable attaching arbitrary things to it that might be needed in the future, be it debug printing or something else.
I initially did try the fmt::Debug
bound approach but I think there was issues with it, altough I don't remember off the top of my head at the moment...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially did try the
fmt::Debug
bound approach but I think there was issues with it, altough I don't remember off the top of my head at the moment...
Right, because Debug
on Pointee::Metadata
gets me the ability to debug-print the metadata, but in this case I need to format the whole pointer+metadata combo somehow, ideally preserving the simple "just hex number" format for simple pointers...
Edit: But I could use a custom sealed trait, sure, I'll try that later and wil update...
FYI I have paused work on this due to stuff not being resolved in #81513 |
☔ The latest upstream changes (presumably #125443) made this pull request unmergeable. Please resolve the merge conflicts. |
New iteration of #84927 (since I can no longer reopen that one).
Basically, add metadata via
ptr::metadata()
to debug prints of fat pointers (ie. to slices and trait objects).Thin ptr debug format as well as all
{:p}
should be unadffected.Example prints:
This time I managed to leverage specialization instead of
size_of
shennanigans.Blocked on #81513