Skip to content
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

Fix Debug implementations that hardcode the representation of a struct #44771

Closed
dtolnay opened this issue Sep 22, 2017 · 1 comment
Closed
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Sep 22, 2017

The Debug implementation for Mutex looks like this:

write!(f, "Mutex {{ data: {:?} }}", &*guard)

This is bad because it does not honor the # pretty-print modifier.

println!("{:#?}", std::sync::Mutex::new(5));

It prints

Mutex { data: 5 }

instead of

Mutex {
    data: 5
}

A correct implementation would use debug_struct.

f.debug_struct("Mutex").field("data", &*guard).finish()

There seem to be lots of instances of this in the standard library. The following search is a good place to start.

grep -r ' {{ ' libcore libstd
@MaloJaffre
Copy link
Contributor

I'll try to tackle and fix this issue.

@TimNN TimNN added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Sep 24, 2017
MaloJaffre added a commit to MaloJaffre/rust that referenced this issue Oct 9, 2017
kennytm added a commit to kennytm/rust that referenced this issue Oct 10, 2017
Refactor to use `debug_struct` in several Debug impls

Also use `pad` and derive `Debug` for `Edge`.

Fixes rust-lang#44771.
@dtolnay dtolnay added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants