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
Conversation
r? @cuviper (rustbot has picked a reviewer for you, use r? to override) |
A quick note regarding 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 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? |
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. |
a3ac892
to
82a9f94
Compare
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 However, the actual way I'd implemented that code is with fn hex_u32(value: u32) -> impl fmt::Debug {
FormatterFn(move |f| write!(f, "{:#010X}", value))
} |
Sounds good! @bors r+ |
…viper Closure-consuming helper functions for `fmt::Debug` helpers ACP: rust-lang/libs-team#288 Tracking issue: rust-lang#117729
💔 Test failed - checks-actions |
Connection failure in remote-test-client... @bors retry |
…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
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
ACP: rust-lang/libs-team#288
Tracking issue: #117729