From d4acca26b9440ff0733ae02915dbf0b1ac4bde97 Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Thu, 23 Feb 2023 21:30:13 +0100 Subject: [PATCH 1/5] format-dynamic-fill --- text/0000-format-dynamic-fill.md | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 text/0000-format-dynamic-fill.md diff --git a/text/0000-format-dynamic-fill.md b/text/0000-format-dynamic-fill.md new file mode 100644 index 00000000000..9a7bce87c40 --- /dev/null +++ b/text/0000-format-dynamic-fill.md @@ -0,0 +1,65 @@ +- Feature Name: `format_dynamic_fill` +- Start Date: 2023-02-23 +- RFC PR: [rust-lang/rfcs#0000](https://github.com/rust-lang/rfcs/pull/0000) +- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000) + +# Summary +[summary]: #summary + +Allow specifying the *fill* character of a format string, matching behavior of *width* and *precision*. + +# Motivation +[motivation]: #motivation + +To allow changing the *fill* character at run-time. Due to not being able to create or modify [`Formatter`](https://doc.rust-lang.org/std/fmt/struct.Formatter.html), this is the only way to allow this. + +# Guide-level explanation +[guide-level-explanation]: #guide-level-explanation + +Extending on [`std::fmt`#Fill/Alignment](https://doc.rust-lang.org/std/fmt/index.html#fillalignment): + +The value for the fill can also be provided as a [`char`](https://doc.rust-lang.org/std/primitive.char.html) in the list of parameters by adding a postfix `$` indicating that the second argument is a char specifying the fill. + +Referring to an argument with the dollar syntax does not affect the “next argument” counter, so it’s usually a good idea to refer to arguments by position, or use named arguments. + +```rs +assert_eq!(format!("Hello {:fill$>5}!", "x", fill='+'), "Hello x++++!"); +assert_eq!(format!("Hello {1:0$<5}!", '-', "x"), "Hello x----!"); +``` + +# Reference-level explanation +[reference-level-explanation]: #reference-level-explanation + +Implementation should be matching that of width and precision. + +The fill and alignment is the first specified format parameter, so if the `:` is followed by: +- `"{:-^}"` a single character *c* (this includes `$`) followed by an *alignment* ⇒ the fill is *c* +- `"{:ident$>}"` a valid rust identifier *ident* followed by a `$` and an *alignment* ⇒ the fill is the named argument/variable *ident* +- `"{:1$<}"` an usize *n* followed by a `$` and an *alignment* ⇒ the fill is the *n*th argument (this does not affect the "next argument" counter matching [width](https://doc.rust-lang.org/std/fmt/index.html#width)) +- `"{:width$}"` a valid rust identifier or usize followed by a `$` and not followed by an *alignment* ⇒ this is the width + +(*alignment* is one of `<^>`) + +# Drawbacks +[drawbacks]: #drawbacks + +It complicates the format spec, and it is unclear how much it might be used. + +# Rationale and alternatives +[rationale-and-alternatives]: #rationale-and-alternatives + +- If enforcing a minimum of 2 characters the `$` could be omitted, but this only makes this easier to mess up and is unnecessarily restrictive. +- This can only be implemented in rust as it is impossible to drive the `Display` family traits on stable without going through the format string. + +# Prior art +[prior-art]: #prior-art + +The implementation of width and precision. + +# Unresolved questions +[unresolved-questions]: #unresolved-questions + +# Future possibilities +[future-possibilities]: #future-possibilities + +Create a stable interface to specify all possible format arguments. From 0ff5acc9816cf8dcfb663e98a9d27f06672aa03d Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Thu, 23 Feb 2023 21:31:52 +0100 Subject: [PATCH 2/5] add rfc pr --- text/0000-format-dynamic-fill.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/0000-format-dynamic-fill.md b/text/0000-format-dynamic-fill.md index 9a7bce87c40..30f8dc02b57 100644 --- a/text/0000-format-dynamic-fill.md +++ b/text/0000-format-dynamic-fill.md @@ -1,6 +1,6 @@ - Feature Name: `format_dynamic_fill` - Start Date: 2023-02-23 -- RFC PR: [rust-lang/rfcs#0000](https://github.com/rust-lang/rfcs/pull/0000) +- RFC PR: [rust-lang/rfcs#3394](https://github.com/rust-lang/rfcs/pull/3394) - Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000) # Summary From 612399a20af86bffeee0816eb6e9d8435dfb6063 Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Thu, 23 Feb 2023 22:11:17 +0100 Subject: [PATCH 3/5] incomparate comments --- text/0000-format-dynamic-fill.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/text/0000-format-dynamic-fill.md b/text/0000-format-dynamic-fill.md index 30f8dc02b57..024d8c0673d 100644 --- a/text/0000-format-dynamic-fill.md +++ b/text/0000-format-dynamic-fill.md @@ -18,7 +18,7 @@ To allow changing the *fill* character at run-time. Due to not being able to cre Extending on [`std::fmt`#Fill/Alignment](https://doc.rust-lang.org/std/fmt/index.html#fillalignment): -The value for the fill can also be provided as a [`char`](https://doc.rust-lang.org/std/primitive.char.html) in the list of parameters by adding a postfix `$` indicating that the second argument is a char specifying the fill. +The value for the fill can also be provided as a [`char`](https://doc.rust-lang.org/std/primitive.char.html) in the list of parameters by adding a postfix `$` indicating that the *n*th or named argument is a char specifying the fill. Referring to an argument with the dollar syntax does not affect the “next argument” counter, so it’s usually a good idea to refer to arguments by position, or use named arguments. @@ -48,8 +48,8 @@ It complicates the format spec, and it is unclear how much it might be used. # Rationale and alternatives [rationale-and-alternatives]: #rationale-and-alternatives -- If enforcing a minimum of 2 characters the `$` could be omitted, but this only makes this easier to mess up and is unnecessarily restrictive. -- This can only be implemented in rust as it is impossible to drive the `Display` family traits on stable without going through the format string. +- If a minimum of 2 characters was enforced the `$` could be omitted, but this only makes this easier to mess up and is unnecessarily restrictive, as well as inconsistent with other parameters that do require `$`. +- This can only be implemented in rust as it is impossible to drive the `Display` family traits in stable rust without using a format string. # Prior art [prior-art]: #prior-art From 9a55a37720b0404b84b5df2db13bd9745b1d010f Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Thu, 23 Feb 2023 23:38:32 +0100 Subject: [PATCH 4/5] name with pr number --- text/{0000-format-dynamic-fill.md => 3394-format-dynamic-fill.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename text/{0000-format-dynamic-fill.md => 3394-format-dynamic-fill.md} (100%) diff --git a/text/0000-format-dynamic-fill.md b/text/3394-format-dynamic-fill.md similarity index 100% rename from text/0000-format-dynamic-fill.md rename to text/3394-format-dynamic-fill.md From 013e97262017e9f63cf2f56dde526a3b181174a9 Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Sun, 26 Feb 2023 13:55:23 +0100 Subject: [PATCH 5/5] clarified "not possible in library" Co-authored-by: Josh Triplett --- text/3394-format-dynamic-fill.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/3394-format-dynamic-fill.md b/text/3394-format-dynamic-fill.md index 024d8c0673d..0b71ab3214e 100644 --- a/text/3394-format-dynamic-fill.md +++ b/text/3394-format-dynamic-fill.md @@ -49,7 +49,7 @@ It complicates the format spec, and it is unclear how much it might be used. [rationale-and-alternatives]: #rationale-and-alternatives - If a minimum of 2 characters was enforced the `$` could be omitted, but this only makes this easier to mess up and is unnecessarily restrictive, as well as inconsistent with other parameters that do require `$`. -- This can only be implemented in rust as it is impossible to drive the `Display` family traits in stable rust without using a format string. +- This cannot be implemented in a library, as the `Formatter` used by the `Display` trait does not expose a mechanism to change the fill character, and any other mechanism would not allow using the `Display` implementations throughout the ecosystem. # Prior art [prior-art]: #prior-art