Skip to content

Commit c9ae0bb

Browse files
committed
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`
1 parent 1c9837d commit c9ae0bb

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

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)