-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Duration
fmt::Debug
impl doesn't honor alignment
#88059
Labels
C-bug
Category: This is a bug.
Comments
I'd be happy to work on a PR to change this behavior, provided it's agreed that we would want to honor fill/width/alignment here? |
hawkw
added a commit
to tokio-rs/console
that referenced
this issue
Aug 15, 2021
This looks somewhat nicer, IMO --- and, `top` and `htop` do this. It would be nice to also left-align other numeric values (a la `top`/`htop`), but we can't do that for `Duration`s currently, as the `fmt::Debug` impl for `std::time::Duration` simply ignores alignment specifiers (see rust-lang/rust#88059). We could write our own custom formatter for durations, I guess...
Is this related to #87905? |
hawkw
added a commit
to tokio-rs/console
that referenced
this issue
Aug 17, 2021
This looks somewhat nicer, IMO --- and, `top` and `htop` do this. It would be nice to also left-align other numeric values (a la `top`/`htop`), but we can't do that for `Duration`s currently, as the `fmt::Debug` impl for `std::time::Duration` simply ignores alignment specifiers (see rust-lang/rust#88059). We could write our own custom formatter for durations, I guess...
This was referenced Sep 14, 2021
Fixed by #88999. |
the8472
pushed a commit
to the8472/rust
that referenced
this issue
Sep 22, 2021
Duration's Debug formatting previously ignored the width parameter. This commit fixes that. Fixes issue rust-lang#88059.
the8472
added a commit
to the8472/rust
that referenced
this issue
Sep 22, 2021
Make `Duration` respect `width` when formatting using `Debug` When printing or writing a `std::time::Duration` using `Debug` formatting, it previously completely ignored any specified `width`. This is unlike types like integers and floats, which do pad to `width`, for both `Display` and `Debug`, though not all types consider `width` in their `Debug` output (see e.g. rust-lang#30164). Curiously, `Duration`'s `Debug` formatting *did* consider `precision`. This PR makes `Duration` pad to `width` just like integers and floats, so that ```rust format!("|{:8?}|", Duration::from_millis(1234)) ``` returns ``` |1.234s | ``` Before you ask "who formats `Debug` output?", note that `Duration` doesn't actually implement `Display`, so `Debug` is currently the only way to format `Duration`s. I think that's wrong, and `Duration` should get a `Display` implementation, but in the meantime there's no harm in making the `Debug` formatting respect `width` rather than ignore it. I chose the default alignment to be left-aligned. The general rule Rust uses is: numeric types are right-aligned by default, non-numeric types left-aligned. It wasn't clear to me whether `Duration` is a numeric type or not. The fact that a formatted `Duration` can end with suffixes of variable length (`"s"`, `"ms"`, `"µs"`, etc.) made me lean towards left-alignment, but it would be trivial to change it. Fixes issue rust-lang#88059.
bors
added a commit
to rust-lang-ci/rust
that referenced
this issue
Sep 24, 2021
Make `Duration` respect `width` when formatting using `Debug` When printing or writing a `std::time::Duration` using `Debug` formatting, it previously completely ignored any specified `width`. This is unlike types like integers and floats, which do pad to `width`, for both `Display` and `Debug`, though not all types consider `width` in their `Debug` output (see e.g. rust-lang#30164). Curiously, `Duration`'s `Debug` formatting *did* consider `precision`. This PR makes `Duration` pad to `width` just like integers and floats, so that ```rust format!("|{:8?}|", Duration::from_millis(1234)) ``` returns ``` |1.234s | ``` Before you ask "who formats `Debug` output?", note that `Duration` doesn't actually implement `Display`, so `Debug` is currently the only way to format `Duration`s. I think that's wrong, and `Duration` should get a `Display` implementation, but in the meantime there's no harm in making the `Debug` formatting respect `width` rather than ignore it. I chose the default alignment to be left-aligned. The general rule Rust uses is: numeric types are right-aligned by default, non-numeric types left-aligned. It wasn't clear to me whether `Duration` is a numeric type or not. The fact that a formatted `Duration` can end with suffixes of variable length (`"s"`, `"ms"`, `"µs"`, etc.) made me lean towards left-alignment, but it would be trivial to change it. Fixes issue rust-lang#88059.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It appears that the
fmt::Debug
implementation forstd::time::Duration
ignores alignment/width specifiers.For example, see this playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d5daccbe4b2e510f4dcee8d85e421b47
I tried this code:
Formatting a
Duration
with alignment/width specifiers (with af64
for comparison).I expected to see this happen:
The durations should be padded to fill the specified width, and aligned based on the alignment specifier (identically to the
f64
value. I would expect to see output that looks like this:Instead, this happened:
The
Duration
s are not padded to the specified width, and are not aligned based on the format specifier. The output looks like this:Meta
rustc --version --verbose
:On my machine, the version is:
In the playground this also occurs on stable 1.54.0, and on
1.56.0-nightly (2021-08-14 8007b506ac5da629f223)
. Looking at thegit blame
, though, it looks likeDuration
'sDebug
impl has had this behavior since 9eeb13f (prior to that commit, it simply derivedDebug
).The text was updated successfully, but these errors were encountered: