Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upHexadecimal integers with fmt::Debug, including within larger types #2226
Conversation
This was referenced Nov 24, 2017
This comment has been minimized.
This comment has been minimized.
fstirlitz
commented
Nov 25, 2017
|
It seems like the output of current Re 'Unresolved questions': given that you're asking about other possible formatting styles, adding an 'ASCII literal' style for |
This comment has been minimized.
This comment has been minimized.
|
That unresolved question is specifically about other existing formatting traits like |
This comment has been minimized.
This comment has been minimized.
|
FWIW |
This comment has been minimized.
This comment has been minimized.
|
The problem here is that So we need to decide whether |
This comment has been minimized.
This comment has been minimized.
ExpHP
commented
Nov 26, 2017
|
What makes hexadecimal so special? I've wanted this kind of argument forwarding for floats since forever, considering that the default format is prone to generate things like |
This comment has been minimized.
This comment has been minimized.
|
Hexadecimal is special because one octet (which is a fairly fundamental unit) is exactly 2 hex digits, so it is a compact way to visualize bit-packed data. Before this RFC, hexadecimal and " I’m not sure what’s your point about floats. If you want to use fixed-precision formatting for floats within larger structures, the formatting string syntax already allows you to specify both a precision and |
This comment has been minimized.
This comment has been minimized.
ExpHP
commented
Nov 26, 2017
|
Does #46233 enable the use of |
This comment has been minimized.
This comment has been minimized.
|
Ah ok, sorry I misunderstood your previous comment. I think that fits in the “Other formatting types/traits?” unresolved question. I don’t have an opinion on it. |
This comment has been minimized.
This comment has been minimized.
ExpHP
commented
Nov 27, 2017
•
|
Okay. Even if this does not include such capability, I think my primary concern more than anything else is to make sure that this does not preclude future extensions such as (strawman syntax) Personally, I'm still holding out for some kind of "general number formatting" option like // Added in e.g. Rust 1.25
fn number_radix(&self) -> u32;
fn number_uppercase(&self) -> bool;
// Added in e.g. Rust 1.27
// true for {:e?} and {:E?}
fn number_exponential(&self) -> bool;
// Added in e.g. Rust 1.33
// true for {:g?} and {:G?}
// never simultaneously true with number_exponential()
fn number_general(&self) -> bool; |
This comment has been minimized.
This comment has been minimized.
|
Right, the |
SimonSapin
referenced this pull request
Nov 28, 2017
Merged
Make fmt::DebugList and friends forward formatting parameters #46233
This comment has been minimized.
This comment has been minimized.
|
cc @rust-lang/libs, highfive failed to assign a reviewer here. |
This comment has been minimized.
This comment has been minimized.
ia0
commented
Nov 29, 2017
|
If it can help, one possible alternative would be to have both the decimal and hexadecimal representations. The idea is that Debug is meant for debugging, so we don't really know in advance what we want to see. If there is a debugging level, it could be used to control the amount of displayed information.
The size used for the hexadecimal comments could be inferred from the type ( Advantages:
Disadvantages:
I have no opinion about which way to go, but just felt that such an alternative could be considered before committing to a decision (in particular if we have a way to control the debug verbosity). |
This comment has been minimized.
This comment has been minimized.
This example is a very small slice. This formatting would get overly verbose very quickly.
Such control belongs inside the formatting string. It should be written once by the person that wrote for example the unit test, not up to every user not to forget to set some boilerplate environment variable. |
This comment has been minimized.
This comment has been minimized.
ia0
commented
Nov 30, 2017
|
Yes, ideally the debug verbosity level would be in the format string, although that would change the format string API. If it's fine to go that way, then the remaining differences would be:
would give
instead of
I guess it depends on the use-case. For slices, it seems better to choose only one of decimal or hexadecimal since the values are homogeneous. But for more complex values, enforcing the user to choose one seems too constraining. |
This comment has been minimized.
This comment has been minimized.
This RFC is proposing to extend the formatting string syntax and And to be honest, using comment syntax in formatting output seems really unusual and not appropriate for |
This comment has been minimized.
This comment has been minimized.
ia0
commented
Nov 30, 2017
|
I am not sure this is unusual when you are debugging. For example some debuggers comment the assembly with the decimal, hexadecimal, character, or string value based on heuristics: Is the issue with heterogeneous values (see |
Centril
added
the
T-libs
label
Dec 6, 2017
This comment has been minimized.
This comment has been minimized.
|
cc @sfackler @alexcrichton, can one of you take a look at this RFC? |
This comment has been minimized.
This comment has been minimized.
|
This seems like a pretty slick RFC, thanks again for writing it up @SimonSapin! I'm mostly eyeing this from the perspective of "what do we need to stabilize to get the maximal benefit from this". It'd all for sure start out as unstable, but one part I like about this RFC is that there's a very tiny sliver I think we'll need to stabilize to actually get benefits, namely the new syntax in format strings. I'm not so sure about the Along those lines I sort of mostly see this RFC as "let's add some more syntax and then here's some details of how to implement it in libstd", where for now the biggest thing to debate I think is the syntax and the mechanics that go along with it. I personally really like that you don't need to add to Overall I'd be willing to merge, but would just want a gut check against other languages (especially Python) to see if this is already a feature somewhere else that we can draw experience from. |
This comment has been minimized.
This comment has been minimized.
|
Python classes can have separate As to hexadecimal formatting, Python also has |
This comment has been minimized.
This comment has been minimized.
|
Oh ok so just to confirm, at least with Python, if you have a custom composite like: struct Foo {
a: i64,
}There's no automatic way to format that with hex vs decimal? |
This comment has been minimized.
This comment has been minimized.
quodlibetor
commented
Jan 16, 2018
|
No, there's a magic method you can implement that allows you to customize formatting: class MyHex:
def __init__(self, val):
self.val = val
def __format__(self, spec):
if spec == 'x':
return hex(self.val)
if spec == '':
return str(self.val)
return 'unknown spec'>>> h = MyHex(89)
>>> '{:x}'.format(h)
'0x59'
>>> '{}'.format(h)
'89'
>>> '{:jk}'.format(h)
'unknown spec'You get the raw string, so you would need to implement all the custom hex In general Python doesn't have anything nearly as fancy as |
This comment has been minimized.
This comment has been minimized.
ExpHP
commented
Jan 17, 2018
•
There's no way to automatically format that, period.
so basically you need to do what |
This comment has been minimized.
This comment has been minimized.
|
Ok thanks for the info @quodlibetor and @ExpHP! In that case sounds like there's not a lot of prior art on this specifically. As a result since it seems reasonable to me I'll.... @rfcbot fcp merge |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Jan 18, 2018
•
|
Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
rfcbot
added
the
proposed-final-comment-period
label
Jan 18, 2018
KodrAus
reviewed
Jan 18, 2018
| [summary]: #summary | ||
|
|
||
| Add support for formatting integers as hexadecimal with the `fmt::Debug` trait, | ||
| including when the occur within larger types. |
This comment has been minimized.
This comment has been minimized.
| For example, an RGBA color encoded in `u32` with 8 bits per channel is easier to understand | ||
| when shown as `00CC44FF` than `13387007`. | ||
|
|
||
| The `std::fmt::UpperHex` and `std::fmt::LowerHex` provide hexadecimal formatting |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
SimonSapin
added some commits
Jan 21, 2018
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Feb 16, 2018
|
|
1 similar comment
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Feb 16, 2018
|
|
rfcbot
added
final-comment-period
and removed
proposed-final-comment-period
labels
Feb 16, 2018
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Feb 26, 2018
|
The final comment period is now complete. |
alexcrichton
referenced this pull request
Feb 27, 2018
Closed
Tracking issue for hexadecimal integers with fmt::Debug output #48584
This comment has been minimized.
This comment has been minimized.
|
Ok! I'll now merge this with a tracking issue |
alexcrichton
merged commit 16cfef8
into
rust-lang:master
Feb 27, 2018
This comment has been minimized.
This comment has been minimized.
quodlibetor
commented
Mar 1, 2018
|
The correct rendered link (the target of the original comment is deleted) is now: https://github.com/rust-lang/rfcs/blob/master/text/2226-fmt-debug-hex.md |
This comment has been minimized.
This comment has been minimized.
|
@quodlibetor Fixed, thanks! |
SimonSapin
deleted the
SimonSapin:fmt-debug-hex
branch
Mar 2, 2018
SimonSapin
added a commit
to SimonSapin/rust
that referenced
this pull request
Mar 13, 2018
SimonSapin
referenced this pull request
Mar 13, 2018
Merged
Add hexadecimal formatting of integers with fmt::Debug #48978
This comment has been minimized.
This comment has been minimized.
|
rust-lang/rust#48978 implements this RFC, but without the new |
SimonSapin commentedNov 24, 2017
•
edited by Centril
Add support for formatting integers as hexadecimal with the
fmt::Debugtrait,including when they occur within larger types.
Rendered