]> git.lizzy.rs Git - rust.git/commit
Auto merge of #88999 - Migi:master, r=oli-obk
authorbors <bors@rust-lang.org>
Fri, 24 Sep 2021 15:22:26 +0000 (15:22 +0000)
committerbors <bors@rust-lang.org>
Fri, 24 Sep 2021 15:22:26 +0000 (15:22 +0000)
commitf06f9bbd3a2b0a2781decd6163b14f71dd59bf7f
tree50ef3e4883906b47f8823622ae54516747cc5220
parenta0648eab3686f100c7ab9b0d38472c740079cce4
parent77ceb2b5d8381be2ea0c1ef95e117d34a6bb4d10
Auto merge of #88999 - Migi:master, r=oli-obk

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. #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 #88059.
library/core/src/fmt/mod.rs