Skip to content

fmt: Implement Debug for Formatter - #166245

Open
tgross35 wants to merge 1 commit into
rust-lang:mainfrom
tgross35:formatter-debug
Open

tgross35 wants to merge 1 commit into
rust-lang:mainfrom
tgross35:formatter-debug

Conversation

@tgross35

@tgross35 tgross35 commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

This is a public type that doesn't currently have a debug implementation, but that would be helpful for peeking at format specifiers. Add one that shows specifiers via FormattingOptions.

As this is a new trait implementation, it is inst-stable under a new formatter_debug feature.

This makes the most sense with #166259.

@rustbot rustbot added A-fmt Area: `core::fmt` 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 Nov 20, 2026
@rustbot

This comment has been minimized.

@tgross35

Copy link
Copy Markdown
Member Author

^ it's picking up # in a code block

New Debug implementation is insta-stable, I expect most will be on board with some kind of implementation here.

@rfcbot merge libs

@rust-rfcbot

rust-rfcbot commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

@tgross35 has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/libs-ping: FCP proposed for libs, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Nov 20, 2026
@hanna-kruppe

Copy link
Copy Markdown
Contributor

When writing specifier-aware formatters, it would be useful to peek at all configured specifiers, but there isn't an easy way to do this.

On nightly you can just grab the options with Formatter::options() and debug-print that, right? If that method and FormattingOptions get stabilized, would the impl for Formatter still be useful? For this use case I'd probably prefer to see the options directly, without the Formatter { options: ..., buf: &dyn mut Write } fluff around it.

I'm not necessarily opposed to adding the impl, we generally add Debug impls even if they seem pointless because any public type could end up in a larger type someone wants to debug-print. But it seems like the main change here (better FormattingOption debug impl) doesn't need FCP and can be landed separately, while the part that needs FCP isn't really required for this use case.

@rustbot

This comment has been minimized.

@tgross35

Copy link
Copy Markdown
Member Author

Agreed that just using .options() will be nicer once available, though I still don't think there is any downside of having the impl. The rest is also reasonable, I split the FormattingOptions changes to #166259.

This is a public type that doesn't currently have a debug
implementation, but that would be helpful for peeking at format
specifiers. Using `.options()` may be nicer for this usecase once it is
stable, but it does not hurt to have an implementation anyway; thus, add
one here.

As this is a new trait implementation, it is inst-stable under a new
`formatter_debug` feature.
@clarfonthey

Copy link
Copy Markdown
Contributor

My only apprehension is a worry that people might start accidentally swapping the formatter with other arguments when writing debug impls. But that feels like… not really a big concern, since the formatter will be required to make those things work, and the mutable reference means multiple concurrent borrows would be detected.

Speaking of which… outside dbg!, would it even be possible to debug-print this?

@tgross35

Copy link
Copy Markdown
Member Author

Speaking of which… outside dbg!, would it even be possible to debug-print this?

Not easily :) if you want it written back to itself within an impl, you pretty much need to format! it to a string first. I think this is fine since, at least in my usecase, logging it somehow is more useful than writing it back and messing up the trait output.

Once Formatter::new is stable there will be more of these floating around outside of the fmt traits so it will be a bit more relevant.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-fmt Area: `core::fmt` disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. 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.

5 participants