Skip to content

Tracking Issue for debug_closure_helpers #117729

Closed

Description

@jmillikin

View all comments

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: rust-lang/libs-team#288

Public API

// core::fmt

impl DebugList<'_, '_> {
    fn entry_with<F>(&mut self, entry_fmt: F) -> &mut Self
    where
        F: FnOnce(&mut Formatter) -> fmt::Result;
}

impl DebugMap<'_, '_> {
    fn key_with<F>(&mut self, key_fmt: F) -> &mut Self
    where
        F: FnOnce(&mut Formatter) -> fmt::Result;
    
    fn value_with<F>(&mut self, value_fmt: F) -> &mut Self
    where
        F: FnOnce(&mut Formatter) -> fmt::Result;
}

impl DebugSet<'_, '_> {
    fn entry_with<F>(&mut self, entry_fmt: F) -> &mut Self
    where
        F: FnOnce(&mut Formatter) -> fmt::Result;
}

impl DebugStruct<'_, '_> {
    fn field_with<F>(&mut self, name: &str, field_fmt: F) -> &mut Self
    where
        F: FnOnce(&mut Formatter) -> fmt::Result;
}

impl DebugTuple<'_, '_> { // also DebugSet
    fn field_with<F>(&mut self, field_fmt: F) -> &mut Self
    where
        F: FnOnce(&mut Formatter) -> fmt::Result;
}

Steps / History

Unresolved Questions

  • Naming: Do any strong preferences exist regarding the new method names?
    • For now I've named them {orig_method}_with, for example DebugStruct::field_with() is like DebugStruct::field().
    • Resolved: The FormatterFn type was renamed to FromFn (and wrapped in from_fn(), the helper methods kept their original names.
  • The DebugMap struct got key_with() and value_with(), but not entry_with() -- is it worth adding that?
  • Should FormatterFn<F> place a trait bound on F so that call sites can be tidier?

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    T-libs-api[DEPRECATED; DO NOT USE]
    on Nov 8, 2023
  2. added a commit that references this issue on Nov 10, 2023
  3. added a commit that references this issue on Nov 10, 2023
  4. added a commit that references this issue on Nov 10, 2023
  5. QuineDot commented on Dec 3, 2023

    @QuineDot

    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

    • Renaming the struct to fmt::FromFn
    • Making the field private
    • Removing the bound from the struct definition

    Is there any particular reason it doesn't implement Binary, LowerExp, etc? (Why both Debug and Display but not all formats?)

  6. steffahn commented on Mar 3, 2024

    @steffahn
    Member

    Is there any particular reason it doesn't implement Binary, LowerExp, etc? (Why both Debug and Display but not all formats?)

    With “just” Debug and Display it would kind-of follow the precedent of fmt::Arguments<'_>.

  7. workingjubilee commented on May 5, 2024

    @workingjubilee
    Member

    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.

  8. workingjubilee commented on May 6, 2024

    @workingjubilee
    Member

    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.

  9. 01mf02 commented on Jun 18, 2024

    @01mf02

    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?

  10. workingjubilee commented on Jun 29, 2024

    @workingjubilee
    Member

    someone would need to PR fmt::from_fn, to start.

  11. added 2 commits that reference this issue on Aug 12, 2024
  12. added a commit that references this issue on Aug 13, 2024
  13. SUPERCILEX commented on Aug 13, 2024

    @SUPERCILEX
    Contributor

    Is there anything remaining for stabilization? These are super useful.

  14. 80 remaining items

  15. jendrikw commented on Apr 18, 2026

    @jendrikw
    Contributor

    This feature was stabilized and released in Rust 1.93 (https://blog.rust-lang.org/2026/01/22/Rust-1.93.0/). Should this issue (and the PR?) be closed?

  16. tgross35 commented on Apr 18, 2026

    @tgross35
    Member

    Are you referring to fmt_from_fn? That was split off from this one #146705, the remaining API here is still unstable https://doc.rust-lang.org/std/fmt/struct.DebugList.html#method.entry_with.

  17. jendrikw commented on Apr 19, 2026

    @jendrikw
    Contributor

    I missed that the feature was split. Sorry to bother you.

  18. added a commit that references this issue on Jul 17, 2026
  19. added 3 commits that reference this issue on Jul 17, 2026
  20. added a commit that references this issue on Jul 17, 2026
  21. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 12, 2026
  22. added a commit that references this issue on Oct 5, 2026
    952bbd0
  23. added a commit that references this issue on Oct 5, 2026
    f077e60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libsRelevant to the library team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions