Repository navigation
fmt: Improve Debug for FormattingOptions - #166259
Conversation
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
|
r? @hanna-kruppe, as it seems you may have skimmed this already. But feel free to reroll |
|
|
896847d to
7085071
Compare
This comment has been minimized.
This comment has been minimized.
| .field("encoded_precision", &precision) | ||
| .field_with("specifier", |f| { | ||
| write!(f, "\"")?; | ||
| self.write_fmt_specifier(f)?; |
There was a problem hiding this comment.
Having the specifier spelled like this is cute, I'd be happy to accept that despite needing some custom code. What I'm not so sure about is duplicating the same information in both fields and in the compact specifier. Is it really worth having both? I guess the fields match more directly what you get from the getters you'd use in your code.
There was a problem hiding this comment.
I do like having both, the fields are much easier to read but the string hints make it easy to recreate. I think maybe if there was a Display impl it could just be the compact string, but that's probably not worth it.
There was a problem hiding this comment.
I was briefly thinking this could be the alternate format, but since "alternate" is a flag, you can't use it to pick between these without changing the options that you're trying to inspect 🙃
| pub fn write_fmt_specifier(&self, f: &mut Formatter<'_>) -> Result { | ||
| let fill = self.get_fill(); | ||
| if fill != ' ' { | ||
| write!(f, "{fill}")?; |
There was a problem hiding this comment.
Small edge case, maybe not worth worrying about: given format_args!("{:\">}") this will print the " unescaped, so FormattingOptions will print the invalid syntax "">". Similarly, fill == '\\' will be printed as "{\>}" which probably isn't valid syntax. Could fix that by using fill.escape_debug().
Arguably this means the "..." wrappers that FormattingOption adds should be part of this method, because if we do escaping then using it in other contexts might be wrong. Do you have other uses in mind?
Unfortunately, adding the " here would make the tests more repetitive. I guess this could be fixed by replacing assert_eq! by a helper function that compares to the debug representation of the expected string.
There was a problem hiding this comment.
It was pretty trivial to just add a bool toggle which avoids some of the " mess, so I did that and added tests.
Do you have other uses in mind?
I did avoid adding the quotes here originally because I figured it's not impossible that some Display impl for an error would want to print within { .. }, or the compiler may use it. But no, there's nothing concrete.
7085071 to
a68197e
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
r=me after changing the commit message to not be interpreted as reference to #123 by Github and the bot. Maybe omit the alternate flag from the example so there's no #?
The current implementation just includes the encoded flags. Add a custom
implementation that prints all the specifiers. Sample for
`"{my_item:x<+0123.456X?}"`:
FormattingOptions {
align: Some(Left),
alternate: false,
debug_as_hex: Some(Upper),
fill: 'x',
precision: Some(456),
sign: Some(Plus),
sign_aware_zero_pad: true,
width: Some(123),
specifier: "x<+0123.456X",
}
a68197e to
42804ba
Compare
|
Thanks! @bors r=hanna-kruppe |
|
📌 Commit 42804ba has been tentatively approved by It will be put into the queue for this repository once PR CI succeeds. 🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened. Reason for tree closure: |
|
@bors rollup |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
❌ Commit 42804ba has been unapproved due to PR CI failure. Reapprove it with |
Restarted CI @bors r=hanna-kruppe rollup |
|
📌 Commit 42804ba has been tentatively approved by It will be put into the queue for this repository once PR CI succeeds. 🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened. Reason for tree closure: |
…nna-kruppe
fmt: Improve `Debug` for `FormattingOptions`
The current implementation just includes the encoded flags. Add a custom implementation that prints all the specifiers. Sample for `"{my_item:x<+0123.456X?}"`:
```rust
FormattingOptions {
align: Some(Left),
alternate: false,
debug_as_hex: Some(Upper),
fill: 'x',
precision: Some(456),
sign: Some(Plus),
sign_aware_zero_pad: true,
width: Some(123),
specifier: "x<+0123.456X",
}
```
…uwer Rollup of 14 pull requests Successful merges: - #166266 (miri subtree update) - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`) - #165008 (Support `NonNull` pointers as `asm!` input operands) - #165892 (`rustc_builtin_macros` cleanup, part 10) - #165930 (Add red-green mark when dumping dep graph. Show DefIds if available) - #166280 (std: also mark doctests with skip_unsupported_miri_tests) - #166295 (use `cfg!(overflow_checks)` in `overflow_checks` intrinsic instead of `cfg!(debug_assertions)`) - #165618 (Implement RangeIntersect on core::range types) - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types) - #166142 (Lower to `hir::Param`s for extern functions) - #166259 (fmt: Improve `Debug` for `FormattingOptions`) - #166269 (libcore: Revert some comment changes) - #166287 (Use `core::ptr` in `implicit_provenance_casts` help for `no_std` crates) - #166306 (bootstrap: Stop passing `-Ztrim-paths` to cargo now that it's stable in beta)
…nna-kruppe
fmt: Improve `Debug` for `FormattingOptions`
The current implementation just includes the encoded flags. Add a custom implementation that prints all the specifiers. Sample for `"{my_item:x<+0123.456X?}"`:
```rust
FormattingOptions {
align: Some(Left),
alternate: false,
debug_as_hex: Some(Upper),
fill: 'x',
precision: Some(456),
sign: Some(Plus),
sign_aware_zero_pad: true,
width: Some(123),
specifier: "x<+0123.456X",
}
```
Rollup of 15 pull requests Successful merges: - #166266 (miri subtree update) - #165720 (windows: remove redundant prefix parsing in `PathBuf::_push`) - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`) - #165008 (Support `NonNull` pointers as `asm!` input operands) - #165892 (`rustc_builtin_macros` cleanup, part 10) - #164485 (Stabilize unchecked `nonzero_ops`) - #165618 (Implement RangeIntersect on core::range types) - #165833 (Deallocate with the right provenance on Windows) - #165834 (Implement tidy alphabetical --bless on Windows) - #165889 (Fix docs of alloc fn: Allocator API stabilized) - #165923 (Don't load/store a LLVM aggregate type when returning an array from a generic SIMD intrinsic) - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types) - #166216 (Stop using #![no_core] in profiler_builtins) - #166259 (fmt: Improve `Debug` for `FormattingOptions`) - #166263 (Show slice split results for empty and singleton slices)
…nna-kruppe
fmt: Improve `Debug` for `FormattingOptions`
The current implementation just includes the encoded flags. Add a custom implementation that prints all the specifiers. Sample for `"{my_item:x<+0123.456X?}"`:
```rust
FormattingOptions {
align: Some(Left),
alternate: false,
debug_as_hex: Some(Upper),
fill: 'x',
precision: Some(456),
sign: Some(Plus),
sign_aware_zero_pad: true,
width: Some(123),
specifier: "x<+0123.456X",
}
```
…nna-kruppe
fmt: Improve `Debug` for `FormattingOptions`
The current implementation just includes the encoded flags. Add a custom implementation that prints all the specifiers. Sample for `"{my_item:x<+0123.456X?}"`:
```rust
FormattingOptions {
align: Some(Left),
alternate: false,
debug_as_hex: Some(Upper),
fill: 'x',
precision: Some(456),
sign: Some(Plus),
sign_aware_zero_pad: true,
width: Some(123),
specifier: "x<+0123.456X",
}
```
…uwer Rollup of 19 pull requests Successful merges: - #166266 (miri subtree update) - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`) - #165008 (Support `NonNull` pointers as `asm!` input operands) - #165892 (`rustc_builtin_macros` cleanup, part 10) - #165930 (Add red-green mark when dumping dep graph. Show DefIds if available) - #166280 (std: also mark doctests with skip_unsupported_miri_tests) - #166295 (use `cfg!(overflow_checks)` in `overflow_checks` intrinsic instead of `cfg!(debug_assertions)`) - #164485 (Stabilize unchecked `nonzero_ops`) - #165618 (Implement RangeIntersect on core::range types) - #165782 (Make unsafe unknown diagnostic attributes an error again) - #165833 (Deallocate with the right provenance on Windows) - #165834 (Implement tidy alphabetical --bless on Windows) - #165889 (Fix docs of alloc fn: Allocator API stabilized) - #165923 (Don't load/store a LLVM aggregate type when returning an array from a generic SIMD intrinsic) - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types) - #166186 (OpenOptions::append: clarify docs) - #166216 (Stop using #![no_core] in profiler_builtins) - #166259 (fmt: Improve `Debug` for `FormattingOptions`) - #166263 (Show slice split results for empty and singleton slices)
…uwer Rollup of 18 pull requests Successful merges: - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`) - #165008 (Support `NonNull` pointers as `asm!` input operands) - #165892 (`rustc_builtin_macros` cleanup, part 10) - #165930 (Add red-green mark when dumping dep graph. Show DefIds if available) - #166280 (std: also mark doctests with skip_unsupported_miri_tests) - #166295 (use `cfg!(overflow_checks)` in `overflow_checks` intrinsic instead of `cfg!(debug_assertions)`) - #164485 (Stabilize unchecked `nonzero_ops`) - #165618 (Implement RangeIntersect on core::range types) - #165782 (Make unsafe unknown diagnostic attributes an error again) - #165833 (Deallocate with the right provenance on Windows) - #165834 (Implement tidy alphabetical --bless on Windows) - #165889 (Fix docs of alloc fn: Allocator API stabilized) - #165923 (Don't load/store a LLVM aggregate type when returning an array from a generic SIMD intrinsic) - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types) - #166186 (OpenOptions::append: clarify docs) - #166216 (Stop using #![no_core] in profiler_builtins) - #166259 (fmt: Improve `Debug` for `FormattingOptions`) - #166263 (Show slice split results for empty and singleton slices)
…uwer Rollup of 18 pull requests Successful merges: - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`) - #165008 (Support `NonNull` pointers as `asm!` input operands) - #165892 (`rustc_builtin_macros` cleanup, part 10) - #165930 (Add red-green mark when dumping dep graph. Show DefIds if available) - #166280 (std: also mark doctests with skip_unsupported_miri_tests) - #166295 (use `cfg!(overflow_checks)` in `overflow_checks` intrinsic instead of `cfg!(debug_assertions)`) - #164485 (Stabilize unchecked `nonzero_ops`) - #165618 (Implement RangeIntersect on core::range types) - #165782 (Make unsafe unknown diagnostic attributes an error again) - #165833 (Deallocate with the right provenance on Windows) - #165834 (Implement tidy alphabetical --bless on Windows) - #165889 (Fix docs of alloc fn: Allocator API stabilized) - #165923 (Don't load/store a LLVM aggregate type when returning an array from a generic SIMD intrinsic) - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types) - #166186 (OpenOptions::append: clarify docs) - #166216 (Stop using #![no_core] in profiler_builtins) - #166259 (fmt: Improve `Debug` for `FormattingOptions`) - #166263 (Show slice split results for empty and singleton slices)
Rollup merge of #166259 - tgross35:formatoptions-debug, r=hanna-kruppe fmt: Improve `Debug` for `FormattingOptions` The current implementation just includes the encoded flags. Add a custom implementation that prints all the specifiers. Sample for `"{my_item:x<+0123.456X?}"`: ```rust FormattingOptions { align: Some(Left), alternate: false, debug_as_hex: Some(Upper), fill: 'x', precision: Some(456), sign: Some(Plus), sign_aware_zero_pad: true, width: Some(123), specifier: "x<+0123.456X", } ```
View all comments
The current implementation just includes the encoded flags. Add a custom implementation that prints all the specifiers. Sample for
"{my_item:x<+0123.456X?}":