Skip to content

Commit 5c821ae

Browse files
authored
Rollup merge of rust-lang#135977 - nyurik:fix-fmt-options, r=joboet
Fix `FormattingOptions` instantiation with `Default` The `fill` value by default should be set to `' '` (space), but the current implementation uses `#[derive(Default)]` which sets it to `\0`. Note that `FormattingOptions` is being released as part of 1.85 (unstable) - so this might warrant a backport to that branch. Tracking issue: rust-lang#118117 Follow up from rust-lang#118159 CC: ``@EliasHolzmann`` ``@programmerjake`` r? ``@m-ou-se``
2 parents 9ffe558 + c9ae0bb commit 5c821ae

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Diff for: library/core/src/fmt/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ pub enum DebugAsHex {
288288
///
289289
/// `FormattingOptions` is a [`Formatter`] without an attached [`Write`] trait.
290290
/// It is mainly used to construct `Formatter` instances.
291-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
291+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
292292
#[unstable(feature = "formatting_options", issue = "118117")]
293293
pub struct FormattingOptions {
294294
flags: u32,
@@ -508,6 +508,15 @@ impl FormattingOptions {
508508
}
509509
}
510510

511+
#[unstable(feature = "formatting_options", issue = "118117")]
512+
impl Default for FormattingOptions {
513+
/// Same as [`FormattingOptions::new()`].
514+
fn default() -> Self {
515+
// The `#[derive(Default)]` implementation would set `fill` to `\0` instead of space.
516+
Self::new()
517+
}
518+
}
519+
511520
/// Configuration for formatting.
512521
///
513522
/// A `Formatter` represents various options related to formatting. Users do not

Diff for: library/core/tests/fmt/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ fn test_maybe_uninit_short() {
5151
assert_eq!(format!("{x:?}"), "MaybeUninit<u32>");
5252
}
5353

54+
#[test]
55+
fn formatting_options_ctor() {
56+
use core::fmt::FormattingOptions;
57+
assert_eq!(FormattingOptions::new(), FormattingOptions::default());
58+
}
59+
5460
#[test]
5561
fn formatting_options_flags() {
5662
use core::fmt::*;

0 commit comments

Comments
 (0)