Skip to content

Add hexadecimal formatting of integers with fmt::Debug - #48978

Merged
bors merged 1 commit into
rust-lang:masterfrom
SimonSapin:debug-hex
Mar 19, 2018
Merged

Add hexadecimal formatting of integers with fmt::Debug#48978
bors merged 1 commit into
rust-lang:masterfrom
SimonSapin:debug-hex

Conversation

@SimonSapin

Copy link
Copy Markdown
Contributor

This can be used for integers within a larger types which implements Debug (possibly through derive) but not fmt::UpperHex or fmt::LowerHex.

assert!(format!("{:02x?}",b"Foo\0") == "[46, 6f, 6f, 00]");assert!(format!("{:02X?}",b"Foo\0") == "[46, 6F, 6F, 00]");

RFC: rust-lang/rfcs#2226

The new formatting string syntax (x? and X?) is insta-stable in this PR because I don’t know how to change a built-in proc macro’s behavior based of a feature gate. I can look into adding that, but I also strongly suspect that keeping this feature unstable for a time period would not be useful as possibly no-one would use it during that time.

This PR does not add the new (public) fmt::Formatter proposed in the API because:

  • There was some skepticism on response to this part of the RFC
  • It is not possible to implement as-is without larger changes to fmt, because Formatter at the moment has no easy way to tell apart for example Octal from Binary: it only has a function pointer for the relevant fmt() method.

If some integer-like type outside of std want to implement this behavior, another RFC will likely need to propose a different public API for Formatter.

@rust-highfive

Copy link
Copy Markdown
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @KodrAus (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

This can be used for integers within a larger types which implements Debug
(possibly through derive) but not fmt::UpperHex or fmt::LowerHex.
```rust
assert!(format!("{:02x?}", b"Foo\0") == "[46, 6f, 6f, 00]");
assert!(format!("{:02X?}", b"Foo\0") == "[46, 6F, 6F, 00]");
```
RFC: rust-lang/rfcs#2226
@shepmaster

Copy link
Copy Markdown
Member

Ping from triage, @KodrAus !

@KodrAusKodrAus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @SimonSapin! So, as I understand it, we've got special DebugLowerHex, and DebugUpperHex variants because hex is a nice representation for bit-packed data, so we don't need to combine other formatting flags with ?.

At first I thought it was a little unfortunate that we can't treat ? like a flag that could be combined with others arbitrarily, but looking at the changes that need to be made to the Debug impls on numbers this doesn't seem like such a problem.

So all that's to say this looks good to me!

@KodrAus

Copy link
Copy Markdown
Contributor

@bors r+

@bors

bors commented Mar 18, 2018

Copy link
Copy Markdown
Collaborator

📌 Commit 4897935 has been approved by KodrAus

@borsbors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 18, 2018
@SimonSapin

Copy link
Copy Markdown
ContributorAuthor

DebugLowerHex and DebugUpperHex are bits inside a semi-private flags: u32 field. (It has pub so that the expansion format_args! can construct it, but it is an #[doc(hidden)] module marked as "internal".)

And yes, in the current implementation the fact that ? vs other "formatter types" was used is not easy to determine form Formatter. We can still change that internal representation, though.

@bors

bors commented Mar 19, 2018

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 4897935 with merge 152217d...

bors added a commit that referenced this pull request Mar 19, 2018
Add hexadecimal formatting of integers with fmt::Debug
This can be used for integers within a larger types which implements Debug (possibly through derive) but not fmt::UpperHex or fmt::LowerHex.
```rust
assert!(format!("{:02x?}", b"Foo\0") == "[46, 6f, 6f, 00]");
assert!(format!("{:02X?}", b"Foo\0") == "[46, 6F, 6F, 00]");
```
RFC: rust-lang/rfcs#2226
The new formatting string syntax (`x?` and `X?`) is insta-stable in this PR because I don’t know how to change a built-in proc macro’s behavior based of a feature gate. I can look into adding that, but I also strongly suspect that keeping this feature unstable for a time period would not be useful as possibly no-one would use it during that time.
This PR does not add the new (public) `fmt::Formatter` proposed in the API because:
* There was some skepticism on response to this part of the RFC
* It is not possible to implement as-is without larger changes to `fmt`, because `Formatter` at the moment has no easy way to tell apart for example `Octal` from `Binary`: it only has a function pointer for the relevant `fmt()` method.
If some integer-like type outside of `std` want to implement this behavior, another RFC will likely need to propose a different public API for `Formatter`.
@bors

bors commented Mar 19, 2018

Copy link
Copy Markdown
Collaborator

☀️ Test successful - status-appveyor, status-travis
Approved by: KodrAus
Pushing 152217d to master...

@bors
bors merged commit 4897935 into rust-lang:masterMar 19, 2018
@borsbors mentioned this pull request Mar 19, 2018
@SimonSapin
SimonSapin deleted the debug-hex branch March 19, 2018 06:35
@SimonSapinSimonSapin added T-libs-api [DEPRECATED; DO NOT USE] relnotes Marks issues that should be documented in the release notes of the next release. labels Mar 23, 2018
@SimonSapin

Copy link
Copy Markdown
ContributorAuthor

I’m unsure if this is release-notes-worthy, tagging to let someone else decide.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

relnotesMarks issues that should be documented in the release notes of the next release.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-libs-api[DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@SimonSapin@rust-highfive@shepmaster@KodrAus@bors