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

Implement and forward more formatting traits for slices and arrays #46215

Closed
wants to merge 1 commit into from

Conversation

SimonSapin
Copy link
Contributor

For example:

impl<T: Octal> Octal for [T; 5] { /* … */ }

This is mostly useful for showing binary (non-text) data in hexadecimal.

For example:

```rust
impl<T: Octal> Octal for [T; 5] { /* … */ }
```

This is mostly useful for showing binary (non-text) data in hexadecimal.
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @TimNN (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.

@SimonSapin
Copy link
Contributor Author

CC @rust-lang/libs

I’ve added #[unstable] to avoid "error: This node does not have a stability attribute", but I don’t remember if stability attributes actually work on impls or if they’re insta-stable regardless.

@dtolnay
Copy link
Member

dtolnay commented Nov 23, 2017

I believe the libs team discussed this in a PR or RFC a couple months ago and decided against adding these impls. I will try to find the discussion.

@dtolnay
Copy link
Member

dtolnay commented Nov 23, 2017

"Added additional formatting options for slices" #44751

@SimonSapin
Copy link
Contributor Author

#44751 (comment)

an entire library dedicated to providing high-quality formatting-related utilities for slices and other datatypes, handling information like formatting flags, customization, etc.

That feels very “perfect is the enemy of good” :(

@dtolnay
Copy link
Member

dtolnay commented Nov 23, 2017

Later in the same comment,

This we felt was best prototyped outside the standard library before moving in.

Which is to say experience is the enemy of blind speculation. The design space here is wide enough that we would benefit from lots of experience with this in library form, rather than guessing what people want based on a few people using this while it is only usable on nightly.

@SimonSapin
Copy link
Contributor Author

At least as far as I’m concerned, I’ve wanted this in two kinds of cases: for “printf-debugging” with temporary println!(…); statements that are removed after running few times before committing anything to version control, and for assertion failure messages in tests. In both cases, the audience is developers, not end users, so the exact formatting doesn’t matter too much.

So what is (in my opinion) lacking here is the opposite of an entire library that supports all kinds of combinations, it is “Debug, but in hex” for quick one-liners.

Perhaps a better solution would be a (pair of) flags for Debug to opt into (upper-case/lower-case) hexadecimal for numbers? Something like format!("{:02X?}", slice_of_u8).

(By the way, {:02?} seems to have no effect different from {:?}, is that intentional?)

I hadn’t really managed to find how to express it before, but I’ve always felt like the Octal/UpperHex/LowerHex traits were out of place: the base used for numbers should be orthogonal to the Debug vs Display split.

@kennytm kennytm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Nov 23, 2017
@SimonSapin
Copy link
Contributor Author

SimonSapin commented Nov 23, 2017

(By the way, {:02?} seems to have no effect different from {:?}, is that intentional?)

It does work on integers, not on slices. I think this is due to not using the original Formatter in fmt::DebugList /~https://github.com/rust-lang/rust/blob/1.22.1/src/libcore/fmt/builders.rs#L256.

Edit: #46233 should fix this.

@SimonSapin
Copy link
Contributor Author

This is PR is unlikely to succeed as-is since pretty much the same was rejected before. Closing in favor of rust-lang/rfcs#2226.

@SimonSapin SimonSapin closed this Nov 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants