Skip to content

Tracking Issue for FormattingOptions #118117

Open
@EliasHolzmann

Description

@EliasHolzmann
Contributor

Feature gate: #![feature(formatting_options)]

This is a tracking issue for fmt::FormattingOptions.

FormattingOptions can be used to construct custom Formatters at runtime.

Public API

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct FormattingOptions {}

enum Sign {
    Plus, 
    Minus
}

impl FormattingOptions {
    pub fn new() -> Self;

    pub fn sign(&mut self, sign: Option<Sign>) -> &mut Self;
    pub fn zero_pad(&mut self, zero_pad: bool) -> &mut Self;
    pub fn alternate(&mut self, alternate: bool) -> &mut Self;
    pub fn fill(&mut self, fill: Option<char>) -> &mut Self;
    pub fn alignment(&mut self, alignment: Option<Alignment>) -> &mut Self;
    pub fn width(&mut self, width: Option<usize>) -> &mut Self;
    pub fn precision(&mut self, precision: Option<usize>) -> &mut Self;

    pub fn get_sign(&self) -> Option<Sign>;
    pub fn get_zero_pad(&self) -> bool;
    pub fn get_alternate(&self) -> bool;
    pub fn get_fill(&self) -> Option<char>;
    pub fn get_alignment(&self) -> Option<Alignment>;
    pub fn get_width(&self) -> Option<usize>;
    pub fn get_precision(&self) -> Option<usize>;
    
    pub fn create_formatter<'a>(self, write: &'a mut (dyn Write + 'a)) -> Formatter<'a>;
}

impl<'a> Formatter<'a> {
    pub fn new(write: &'a mut (dyn Write + 'a), options: FormattingOptions) -> Self;
    pub fn with_options(&mut self, options: FormattingOptions) -> Self;
    
    pub fn sign(&self) -> Option<Sign>;

    pub fn options(&self) -> FormattingOptions;
}

Steps / History

Unresolved Questions

  • Should this expose the "debug as hex" flags? ({:x?} and {:X?})
    Should we make the methods receiving parameters as &mut (especially the setters for FormattingOptions) const (which is currently unstable)? See Implementation of fmt::FormattingOptions #118159 (comment) for context

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/api-change-proposals.html

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

Activity

added
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Nov 21, 2023
EliasHolzmann

EliasHolzmann commented on Nov 21, 2023

@EliasHolzmann
ContributorAuthor

@rustbot claim

changed the title [-]Tracking Issue for XXX[/-] [+]Tracking Issue for `FormattingOptions`[/+] on Nov 21, 2023
added 2 commits that reference this issue on Feb 15, 2024

Auto merge of rust-lang#118159 - EliasHolzmann:formatting_options, r=…

Auto merge of rust-lang#118159 - EliasHolzmann:formatting_options, r=…

added 2 commits that reference this issue on Dec 5, 2024

Auto merge of rust-lang#118159 - EliasHolzmann:formatting_options, r=…

Auto merge of rust-lang#118159 - EliasHolzmann:formatting_options, r=…

joboet

joboet commented on Jan 22, 2025

@joboet
Member

I was wondering: could the signature for Formatter::new be changed to take a reference to the FormattingOptions instead of passing them by value? That way, LLVM wouldn't need to copy around the options as often, which seems like it could improve performance and binary size in cases where the same options are used multiple times, for instance when using the default options.

added a commit that references this issue on Jan 24, 2025

Rollup merge of rust-lang#135977 - nyurik:fix-fmt-options, r=joboet

d6eeb36
added a commit that references this issue on Jan 25, 2025

Rollup merge of rust-lang#135977 - nyurik:fix-fmt-options, r=joboet

5c821ae
added a commit that references this issue on Jan 25, 2025
EliasHolzmann

EliasHolzmann commented on Jan 25, 2025

@EliasHolzmann
ContributorAuthor

@joboet I don't think so. Formatter directly holds a FormattingOptions internally: https://doc.rust-lang.org/nightly/src/core/fmt/mod.rs.html#524

If Formatter::new takes its FormattingOptions by reference instead of by value, we'd have to copy the options in that function anyway. (Instead, you probably could have the contents of FormattingOptions behind a Rc so that copying/cloning is cheaper, which might indeed improve performance for use cases with heavy FormattingOptions reuse. However, I don't think that this will compensate the ref-counting overhead that is in most use cases probably unnecessary.)

In most cases, when the FormattingOptions can be used multiple times, the target buffer of the resulting Formatter is probably also the same – in this case, you can just use the Formatter you construct.

added a commit that references this issue on Feb 11, 2025

Auto merge of rust-lang#136862 - joboet:formatter_options_ref, r=<try>

Ternvein

Ternvein commented on Feb 27, 2025

@Ternvein

As far as I can see, &mut T in const contexts are now stable. Should const be added for functions with mutable references?

added 2 commits that reference this issue on Mar 11, 2025

Auto merge of rust-lang#118159 - EliasHolzmann:formatting_options, r=…

Rollup merge of rust-lang#135977 - nyurik:fix-fmt-options, r=joboet

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

Metadata

Metadata

Assignees

Labels

C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @EliasHolzmann@Ternvein@joboet

      Issue actions

        Tracking Issue for `FormattingOptions` · Issue #118117 · rust-lang/rust