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

Closure-consuming helper functions for fmt::Debug helpers #117730

Merged
merged 1 commit into from Nov 10, 2023

Conversation

jmillikin
Copy link
Contributor

ACP: rust-lang/libs-team#288

Tracking issue: #117729

@rustbot
Copy link
Collaborator

rustbot commented Nov 8, 2023

r? @cuviper

(rustbot has picked a reviewer for you, use r? to override)

@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 Nov 8, 2023
@jmillikin
Copy link
Contributor Author

A quick note regarding FormatterFn:

With its current definition, the call site must provide a signature for the closure:

pub struct FormatterFn<F>(pub F);

let value = 'a';
let wrapped = fmt::FormatterFn(|f: &mut fmt::Formatter<'_>| {
    write!(f, "{:?}", &value)
});
assert_eq!(format!("{}", wrapped), "'a'");

If the type definition were changed to include a bound on F, then the call site can be simplified but attempts to reference FormatterFn itself as a type might become more verbose:

pub struct FormatterFn<F>(pub F)
where
    F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result;

let value = 'a';
let wrapped = fmt::FormatterFn(|f| write!(f, "{:?}", &value));
assert_eq!(format!("{}", wrapped), "'a'");

Does anyone have thoughts on this?

@cuviper
Copy link
Member

cuviper commented Nov 9, 2023

If the type definition were changed to include a bound on F, then the call site can be simplified but attempts to reference FormatterFn itself as a type might become more verbose:

Can you give an example of the latter kind of use that would become more verbose? It seems to me that either way, such uses are going to need to spell out the constraints in order to do anything interesting.

The local call site is probably the more important use to optimize for anyway.

@jmillikin
Copy link
Contributor Author

Can you give an example of the latter kind of use that would become more verbose? It seems to me that either way, such uses are going to need to spell out the constraints in order to do anything interesting.

The local call site is probably the more important use to optimize for anyway.

After sleeping on it, I think my worry here was misplaced. You're correct that optimizing for call site readability is more important, and also there's an easy solution to what I was thinking of.

PR updated (diff)


Some time ago I implemented code that looked somewhat like this:

pub(crate) struct DebugHexU32(u32);

pub(crate) fn debug_hex_u32(value: u32) -> DebugAsHexU32 {
	DebugHexU32(value)
}

impl fmt::Debug for DebugHexU32 {
	fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
		// 8 hex digits + 2 for leading "0x".
		write!(fmt, "{:#010X}", self.0)
	}
}

I was imagining that the debug_hex_u32() return value might become complex, have a type bound, etc.

However, the actual way I'd implemented that code is with -> impl fmt::Debug, and the following snippet works just fine, so I have no concerns:

fn hex_u32(value: u32) -> impl fmt::Debug {
	FormatterFn(move |f| write!(f, "{:#010X}", value))
}

@cuviper
Copy link
Member

cuviper commented Nov 9, 2023

Sounds good!

@bors r+

@bors
Copy link
Contributor

bors commented Nov 9, 2023

📌 Commit 82a9f94 has been approved by cuviper

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 9, 2023
@bors
Copy link
Contributor

bors commented Nov 10, 2023

⌛ Testing commit 82a9f94 with merge cbc679c...

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 10, 2023
…viper

Closure-consuming helper functions for `fmt::Debug` helpers

ACP: rust-lang/libs-team#288

Tracking issue: rust-lang#117729
@rust-log-analyzer
Copy link
Collaborator

The job armhf-gnu failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@bors
Copy link
Contributor

bors commented Nov 10, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 10, 2023
@cuviper
Copy link
Member

cuviper commented Nov 10, 2023

Connection failure in remote-test-client...

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 10, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 10, 2023
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#114191 (Update exploit mitigations documentation)
 - rust-lang#117039 (Clarify UB in `get_unchecked(_mut)`)
 - rust-lang#117730 (Closure-consuming helper functions for `fmt::Debug` helpers)
 - rust-lang#117741 (Fix typo in internal.rs)
 - rust-lang#117743 (Suggest removing `;` for `;` within let-chains)
 - rust-lang#117751 (rustdoc-json: Fix test so it actually checks things)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 0f1da7e into rust-lang:master Nov 10, 2023
11 of 12 checks passed
@rustbot rustbot added this to the 1.75.0 milestone Nov 10, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Nov 10, 2023
Rollup merge of rust-lang#117730 - jmillikin:fmt-debug-helper-fns, r=cuviper

Closure-consuming helper functions for `fmt::Debug` helpers

ACP: rust-lang/libs-team#288

Tracking issue: rust-lang#117729
@jmillikin jmillikin deleted the fmt-debug-helper-fns branch November 10, 2023 05:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

None yet

5 participants