Skip to content

Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned#152865

Open
asder8215 wants to merge 1 commit intorust-lang:mainfrom
asder8215:path_display
Open

Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned#152865
asder8215 wants to merge 1 commit intorust-lang:mainfrom
asder8215:path_display

Conversation

@asder8215
Copy link
Contributor

@asder8215 asder8215 commented Feb 19, 2026

Fixes #152804. Path's Display uses ByteStr's Display, which is where the problem was occurring.

The issue was coming from ByteStr implementation of fmt() in this particular area:

        let Some(align) = f.align() else {
            return fmt_nopad(self, f);
        };
        let nchars: usize = self
            .utf8_chunks()
            .map(|chunk| {
                chunk.valid().chars().count() + if chunk.invalid().is_empty() { 0 } else { 1 }
            })
            .sum();
        let padding = f.width().unwrap_or(0).saturating_sub(nchars);
        let fill = f.fill();
        let (lpad, rpad) = match align {
            fmt::Alignment::Left => (0, padding),
            fmt::Alignment::Right => (padding, 0),
            fmt::Alignment::Center => {
                let half = padding / 2;
                (half, half + padding % 2)
            }
        };

The docs for the align implies that Alignment::Left, Alignment::Right, Alignment::Center comes from :<, :>, and :^ respectively with align() returning None if neither of those symbols are used in the formatted string. However, while padding is taken care of in the aligned cases, we could still have padding for things that don't use alignment like:

assert_eq!(format!("{:10}", Path::new("/foo/bar").display()), "/foo/bar  ");

We shouldn't write to f and return from there when there's no alignment; we should also include any potential padding/filling bytes here.

r? @joboet

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 19, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 19, 2026

joboet is currently at their maximum review capacity.
They may take a while to respond.

@asder8215 asder8215 changed the title Fixed ByteStr not padding within its Display trait when no specific a… Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned Feb 19, 2026
…lignment is not mentioned (e.g. ':10' instead of ':<10', ':>10', or ':^1')
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 Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

std::path::Display regressed in 1.95.0 Nightly

3 participants

Comments