-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Allow modifying Formatter members #19207
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
Comments
This was specifically left out of |
No -- my code looks like this, so it is not overwriting. if sz > 0 && f.width.is_none() {
f.width = Some(4)
} If I want to override width, but I don't want to override all the other parameters.. precision and formatter type (Default or e or E). Maybe there could be a way to copy a formatter with different attributes? Of course the impl could be made smarter, for example adjusting the width so that the elements always line up. |
Triage: no change that I'm aware of, formatter is still private. @rust-lang/libs, is this a use-case that you intend to support, or is this not a bug? |
There are now stable accessors for all of the relevant fields except for alignment. EDIT: Oh, misread, didn't realize this was for modifiers. |
The general question is how to allow passing down formatting options for a container to the elements in a convenient way. Preferably while allowing to set defaults, like my use case indicated. Imagine a simple numerical container, like a mathematical vector. Since the numerics are the main feature, it makes sense we want rich formatting options for how the components of the vector are formatted.
The numerical container will want to support |
@steveklabnik I believe this is still a legitimate bug in terms of "this could be implemented but isn't". My personal opinion is that I'd like to push as hard as we can on read-only members without providing writable variants. |
Could we have writable variants actually, please? Currently, since |
This seems like a fine feature to me to add! PRs would be most welcome to review an implementation here. |
Here's another example of where this would be useful. In our book, @jorendorff and I show how to implement the
This is okay, but ideally, alignment and padding would apply to the complex number's representation as a whole, while numeric precision would apply to each component individually, such that:
The example could certainly use |
What way do you want to address this though?
I'd be happy to have a go at a PR, if you let me know. |
Closing now that there is a libs-team ACP for this: rust-lang/libs-team#280. |
std::fmt::Formatter's members are now private.
In my usecase, I was formatting an array of elements by passing down the formatter and using it for each element, so for example {:.2} would print each element to two decimals.
In this usage, it was useful to modify the
width
field of the Formatter so that the array elements were more padded out by default.The text was updated successfully, but these errors were encountered: