Skip to content
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

fmt::Pointer padding #24186

Merged
merged 3 commits into from
Apr 11, 2015
Merged

fmt::Pointer padding #24186

merged 3 commits into from
Apr 11, 2015

Conversation

richo
Copy link
Contributor

@richo richo commented Apr 8, 2015

This pads out the printing of pointers to their native width.

Extracted from and rebased on top of #24144

@rust-highfive
Copy link
Collaborator

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

@alexcrichton
Copy link
Member

This isn't the behavior that I would personally expect from these routines, could you explain a little bit more about why you'd like to make this change?

@richo
Copy link
Contributor Author

richo commented Apr 8, 2015

Yeah, so this morning I did a little more poking, and discovered that I was misremembering what printf("%p".. does in C. If the goal is to continue to do what C does, then I'm perfectly happy to just close this.

In general, I find that 99% of the time I'm printing a pointer is because I'll copypaste it elsewhere (generally a python repl), do some math on it, then do something with it elsewhere. Given this workflow, padding them all to the native pointer width of the platform cases them to line up neatly, make it easy to eyeball if you have a bunch of values all on page boundaries, etc.

The rationale for the change is pretty much entirely that I would assume this will only be used in debugging, and for that usecase, padded pointers seem much more user friendly to me.

@alexcrichton
Copy link
Member

With the debug builders work @sfackler has been doing the # flag to formatting is starting to mean "debug mode", so another option would be to gate this form of formatting based on that flag being present or not.

@richo
Copy link
Contributor Author

richo commented Apr 8, 2015

My personal preference would be this, but making {#:p} invoke this behaviour would be totally agreeable. Has his work landed?

@alexcrichton
Copy link
Member

It should actually just be as simple as checking for Alternate in the flags of the formatter whenever you're coming in to formatting a pointer. In that sense {:#p} should work just fine today (it just doesn't do anything based on the # flag)

@richo
Copy link
Contributor Author

richo commented Apr 9, 2015

Oh, amazing. I did wonder why the current code sets Alternate unconditionally, but didn't rock the boat.

I'll update the PR when I get a sec.

EDIT: While I'm asking questions, do I need to unfrob the flags on the way back out? It wasn't very clear to me.

@richo richo force-pushed the pad-pointers branch 4 times, most recently from 335405d to a56ea72 Compare April 9, 2015 20:27
@richo
Copy link
Contributor Author

richo commented Apr 9, 2015

I think this is good to go, r? @alexcrichton

Preempting your nit, the body of the Pointer formatter is kinda awkward. I considered adding a PrefixBaseChar flag, which would avoid needing to hijack Alternative. Happy to do that if you think it's worthwhile.

#[cfg(target_pointer_width = "32")]
const POINTER_PADDING: Option<usize> = Some(10);
#[cfg(target_pointer_width = "64")]
const POINTER_PADDING: Option<usize> = Some(18);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wherever possible I tend to prefer if cfg!(...) over #[cfg], and it looks like for all uses of #[cfg] here the cfg!-based form would suffice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I guess the only correctness issue with it is that right now, if you build on a platform that doesn't have 32 or 64 bit wide pointers, it will explode violently.

Granted, there's no obvious sign of anyone trying that soon, but the most obvious cfg! based construction will silently skip a function call on such a platform. How defensive is it reaonable to be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm more worried that #[cfg] code isn't typechecked at all if it's for a different platform, often leading to really annoying cross-platform bugs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*compile errors

#[cfg(target_pointer_width = "32")]
const PTR: &'static str = "0x00001234";
#[cfg(target_pointer_width = "64")]
const PTR: &'static str = "0x0000000000001234";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last #[cfg] here

@richo
Copy link
Contributor Author

richo commented Apr 10, 2015

Ok, think we're finally good to go 🎉

@alexcrichton
Copy link
Member

@bors: r+ 64da4e1

bors added a commit that referenced this pull request Apr 10, 2015
This pads out the printing of pointers to their native width.

Extracted from and rebased on top of #24144
@bors
Copy link
Contributor

bors commented Apr 10, 2015

⌛ Testing commit 64da4e1 with merge 97f1eab...

@bors
Copy link
Contributor

bors commented Apr 10, 2015

💔 Test failed - auto-mac-32-opt

@alexcrichton
Copy link
Member

@bors: retry

On Fri, Apr 10, 2015 at 4:49 PM, bors notifications@github.com wrote:

[image: 💔] Test failed - auto-mac-32-opt
http://buildbot.rust-lang.org/builders/auto-mac-32-opt/builds/4449


Reply to this email directly or view it on GitHub
#24186 (comment).

@bors
Copy link
Contributor

bors commented Apr 11, 2015

⌛ Testing commit 64da4e1 with merge c87ec1e...

bors added a commit that referenced this pull request Apr 11, 2015
This pads out the printing of pointers to their native width.

Extracted from and rebased on top of #24144
@bors bors merged commit 64da4e1 into rust-lang:master Apr 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants