fix: honor precision when a width is set in ByteSize Display#178
Open
sueun-dev wants to merge 1 commit into
Open
fix: honor precision when a width is set in ByteSize Display#178sueun-dev wants to merge 1 commit into
sueun-dev wants to merge 1 commit into
Conversation
With a width, the Display impl used f.pad(display.to_string()). That
renders at the default precision and then f.pad treats the precision as
a maximum width, so a value like ByteSize::mib(1908) formatted with
{:>12.5} came out as " 1.9 G" - the precision ignored and the
unit truncated. Render with the requested precision first, then apply
only the width, fill, and alignment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a
ByteSizeis formatted with a width, precision is dropped and the value can be truncated mid-unit:The width branch of
Displaydoesf.pad(&self.display().to_string()).to_string()renders at the default precision (sof.precision()is ignored), andf.padthen interprets the precision as a maximum width and truncates the result — here"1.9 GiB"becomes"1.9 G". The no-width fast path forwards the formatter and honors precision, and the existingprecision()test pins precision to mean decimal places, so the width path is inconsistent with the rest of the crate.This renders the value with the requested precision first, then applies only the width, fill, and alignment. Added
test_display_width_with_precision; the existingtest_display_alignment(including custom fill) still passes.Checked:
cargo test,cargo test --no-default-features,cargo test --all-features,cargo fmt --check,cargo clippy --all-featuresall clean.