Skip to content

Commit 5d95ec0

Browse files
Rollup merge of #145863 - EliasHolzmann:formatting_options_20250825, r=m-ou-se
formatting_options: Make all methods `const` Related to #118117. Having `const fn`s that take a `mut &` was unstable until Rust 1.83 (see #129195). Because of this, not all methods on `FormattingOptions` were implemented as `const`. As this has been stabilized now, there is no reason not to have all methods `const`. Thanks to `@Ternvein` for bringing this to my attention (see [1]). r? `@m-ou-se` (As you were the reviewer for the original implementation – feel free to reroll if you are busy or if you aren't interested) [1]: #118117 (comment)
2 parents 20a3e45 + 575a90e commit 5d95ec0

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

library/core/src/fmt/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ impl FormattingOptions {
359359
/// always be printed.
360360
/// - `-`: Currently not used
361361
#[unstable(feature = "formatting_options", issue = "118117")]
362-
pub fn sign(&mut self, sign: Option<Sign>) -> &mut Self {
362+
pub const fn sign(&mut self, sign: Option<Sign>) -> &mut Self {
363363
let sign = match sign {
364364
None => 0,
365365
Some(Sign::Plus) => flags::SIGN_PLUS_FLAG,
@@ -372,7 +372,7 @@ impl FormattingOptions {
372372
///
373373
/// This is used to indicate for integer formats that the padding to width should both be done with a 0 character as well as be sign-aware
374374
#[unstable(feature = "formatting_options", issue = "118117")]
375-
pub fn sign_aware_zero_pad(&mut self, sign_aware_zero_pad: bool) -> &mut Self {
375+
pub const fn sign_aware_zero_pad(&mut self, sign_aware_zero_pad: bool) -> &mut Self {
376376
if sign_aware_zero_pad {
377377
self.flags |= flags::SIGN_AWARE_ZERO_PAD_FLAG;
378378
} else {
@@ -389,7 +389,7 @@ impl FormattingOptions {
389389
/// - [`Octal`] - precedes the argument with a `0b`
390390
/// - [`Binary`] - precedes the argument with a `0o`
391391
#[unstable(feature = "formatting_options", issue = "118117")]
392-
pub fn alternate(&mut self, alternate: bool) -> &mut Self {
392+
pub const fn alternate(&mut self, alternate: bool) -> &mut Self {
393393
if alternate {
394394
self.flags |= flags::ALTERNATE_FLAG;
395395
} else {
@@ -404,7 +404,7 @@ impl FormattingOptions {
404404
/// being formatted is smaller than width some extra characters will be
405405
/// printed around it.
406406
#[unstable(feature = "formatting_options", issue = "118117")]
407-
pub fn fill(&mut self, fill: char) -> &mut Self {
407+
pub const fn fill(&mut self, fill: char) -> &mut Self {
408408
self.flags = self.flags & (u32::MAX << 21) | fill as u32;
409409
self
410410
}
@@ -413,7 +413,7 @@ impl FormattingOptions {
413413
/// The alignment specifies how the value being formatted should be
414414
/// positioned if it is smaller than the width of the formatter.
415415
#[unstable(feature = "formatting_options", issue = "118117")]
416-
pub fn align(&mut self, align: Option<Alignment>) -> &mut Self {
416+
pub const fn align(&mut self, align: Option<Alignment>) -> &mut Self {
417417
let align: u32 = match align {
418418
Some(Alignment::Left) => flags::ALIGN_LEFT,
419419
Some(Alignment::Right) => flags::ALIGN_RIGHT,
@@ -430,7 +430,7 @@ impl FormattingOptions {
430430
/// the padding specified by [`FormattingOptions::fill`]/[`FormattingOptions::align`]
431431
/// will be used to take up the required space.
432432
#[unstable(feature = "formatting_options", issue = "118117")]
433-
pub fn width(&mut self, width: Option<u16>) -> &mut Self {
433+
pub const fn width(&mut self, width: Option<u16>) -> &mut Self {
434434
if let Some(width) = width {
435435
self.flags |= flags::WIDTH_FLAG;
436436
self.width = width;
@@ -450,7 +450,7 @@ impl FormattingOptions {
450450
/// - For floating-point types, this indicates how many digits after the
451451
/// decimal point should be printed.
452452
#[unstable(feature = "formatting_options", issue = "118117")]
453-
pub fn precision(&mut self, precision: Option<u16>) -> &mut Self {
453+
pub const fn precision(&mut self, precision: Option<u16>) -> &mut Self {
454454
if let Some(precision) = precision {
455455
self.flags |= flags::PRECISION_FLAG;
456456
self.precision = precision;
@@ -463,7 +463,7 @@ impl FormattingOptions {
463463
/// Specifies whether the [`Debug`] trait should use lower-/upper-case
464464
/// hexadecimal or normal integers
465465
#[unstable(feature = "formatting_options", issue = "118117")]
466-
pub fn debug_as_hex(&mut self, debug_as_hex: Option<DebugAsHex>) -> &mut Self {
466+
pub const fn debug_as_hex(&mut self, debug_as_hex: Option<DebugAsHex>) -> &mut Self {
467467
let debug_as_hex = match debug_as_hex {
468468
None => 0,
469469
Some(DebugAsHex::Lower) => flags::DEBUG_LOWER_HEX_FLAG,
@@ -537,7 +537,7 @@ impl FormattingOptions {
537537
///
538538
/// You may alternatively use [`Formatter::new()`].
539539
#[unstable(feature = "formatting_options", issue = "118117")]
540-
pub fn create_formatter<'a>(self, write: &'a mut (dyn Write + 'a)) -> Formatter<'a> {
540+
pub const fn create_formatter<'a>(self, write: &'a mut (dyn Write + 'a)) -> Formatter<'a> {
541541
Formatter { options: self, buf: write }
542542
}
543543
}
@@ -578,13 +578,13 @@ impl<'a> Formatter<'a> {
578578
///
579579
/// You may alternatively use [`FormattingOptions::create_formatter()`].
580580
#[unstable(feature = "formatting_options", issue = "118117")]
581-
pub fn new(write: &'a mut (dyn Write + 'a), options: FormattingOptions) -> Self {
581+
pub const fn new(write: &'a mut (dyn Write + 'a), options: FormattingOptions) -> Self {
582582
Formatter { options, buf: write }
583583
}
584584

585585
/// Creates a new formatter based on this one with given [`FormattingOptions`].
586586
#[unstable(feature = "formatting_options", issue = "118117")]
587-
pub fn with_options<'b>(&'b mut self, options: FormattingOptions) -> Formatter<'b> {
587+
pub const fn with_options<'b>(&'b mut self, options: FormattingOptions) -> Formatter<'b> {
588588
Formatter { options, buf: self.buf }
589589
}
590590
}

0 commit comments

Comments
 (0)