Open jmillikin opened 10 months ago
Taking inspiration from iter::from_fn
, I suggest a module level helper function:
core::fmt::from_fn<F>(f: F) -> FormatterFn<F>
where
F: Fn(&mut Formatter<'_>) -> Result
{
FormatterFn(f)
}
Plus perhaps also
struct
to fmt::FromFn
struct
definitionIs there any particular reason it doesn't implement Binary
, LowerExp
, etc? (Why both Debug
and Display
but not all formats?)
Is there any particular reason it doesn't implement
Binary
,LowerExp
, etc? (Why bothDebug
andDisplay
but not all formats?)
With “just” Debug
and Display
it would kind-of follow the precedent of fmt::Arguments<'_>
.
these are really nice btw. I think QuineDot's suggestion could be a cool refactoring of this, but it's very nice to be able to basically randomly throw in a simple closure that provides debug formatting, without having to roll the internals yourself, since it's just... like, the case you're most likely doing this, you're already implementing some kind of explicit Debug or "Debug adapter" and you don't wanna have to do another one for every custom field.
QuineDot's suggestion is in fact very close to one of the listed alternatives in the original idea! The main difference is the use of a free higher-order function instead of a constructor, which might be better for type inference reasons.
I would love to see just fmt::from_fn
stabilised, as suggested by @QuineDot. The symmetry with iter::from_fn
is apparent, and this seems relatively uncontroversial. What is the status of stabilising this?
someone would need to PR fmt::from_fn
, to start.
Is there anything remaining for stabilization? These are super useful.
The change suggested above has just been merged into master, the description of the issue should be changed to reflect the new API
Thanks for your work, @its-the-shrimp!
Feature gate:
#![feature(debug_closure_helpers)]
This is a tracking issue for adding helpers to
core::fmt
that can use closures for formatting values.ACP: https://github.com/rust-lang/libs-team/issues/288
Public API
Steps / History
Unresolved Questions
{orig_method}_with
, for exampleDebugStruct::field_with()
is likeDebugStruct::field()
.DebugMap
struct gotkey_with()
andvalue_with()
, but notentry_with()
-- is it worth adding that?FormatterFn<F>
place a trait bound onF
so that call sites can be tidier?