Skip to content

Commit 31a5657

Browse files
committed
Access members of FormattingOptions directly instead of via getters/setters
1 parent 2f9e0c9 commit 31a5657

4 files changed

+187
-187
lines changed

library/core/src/fmt/float.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ where
8686
true => flt2dec::Sign::MinusPlus,
8787
};
8888

89-
if let Some(precision) = fmt.precision() {
89+
if let Some(precision) = fmt.options.precision {
9090
float_to_decimal_common_exact(fmt, num, sign, precision)
9191
} else {
9292
let min_precision = 0;
@@ -162,7 +162,7 @@ where
162162
true => flt2dec::Sign::MinusPlus,
163163
};
164164

165-
if let Some(precision) = fmt.precision() {
165+
if let Some(precision) = fmt.options.precision {
166166
// 1 integral digit + `precision` fractional digits = `precision + 1` total digits
167167
float_to_exponential_common_exact(fmt, num, sign, precision + 1, upper)
168168
} else {
@@ -180,7 +180,7 @@ where
180180
true => flt2dec::Sign::MinusPlus,
181181
};
182182

183-
if let Some(precision) = fmt.precision() {
183+
if let Some(precision) = fmt.options.precision {
184184
// this behavior of {:.PREC?} predates exponential formatting for {:?}
185185
float_to_decimal_common_exact(fmt, num, sign, precision)
186186
} else {

library/core/src/fmt/mod.rs

+36-38
Original file line numberDiff line numberDiff line change
@@ -1466,14 +1466,14 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
14661466
}
14671467

14681468
unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::Placeholder, args: &[rt::Argument<'_>]) -> Result {
1469-
fmt.options.fill(arg.fill);
1470-
fmt.options.align(arg.align.into());
1471-
fmt.options.flags(arg.flags);
1469+
fmt.options.fill = arg.fill;
1470+
fmt.options.align = arg.align.into();
1471+
fmt.options.flags = arg.flags;
14721472
// SAFETY: arg and args come from the same Arguments,
14731473
// which guarantees the indexes are always within bounds.
14741474
unsafe {
1475-
fmt.options.width(getcount(args, &arg.width));
1476-
fmt.options.precision(getcount(args, &arg.precision));
1475+
fmt.options.width = getcount(args, &arg.width);
1476+
fmt.options.precision = getcount(args, &arg.precision);
14771477
}
14781478

14791479
// Extract the correct argument
@@ -1613,7 +1613,7 @@ impl<'a> Formatter<'a> {
16131613
}
16141614

16151615
// The `width` field is more of a `min-width` parameter at this point.
1616-
match self.width() {
1616+
match self.options.width {
16171617
// If there's no minimum length requirements then we can just
16181618
// write the bytes.
16191619
None => {
@@ -1682,12 +1682,12 @@ impl<'a> Formatter<'a> {
16821682
#[stable(feature = "rust1", since = "1.0.0")]
16831683
pub fn pad(&mut self, s: &str) -> Result {
16841684
// Make sure there's a fast path up front
1685-
if self.width().is_none() && self.precision().is_none() {
1685+
if self.options.width.is_none() && self.options.precision.is_none() {
16861686
return self.buf.write_str(s);
16871687
}
16881688
// The `precision` field can be interpreted as a `max-width` for the
16891689
// string being formatted.
1690-
let s = if let Some(max) = self.precision() {
1690+
let s = if let Some(max) = self.options.precision {
16911691
// If our string is longer that the precision, then we must have
16921692
// truncation. However other flags like `fill`, `width` and `align`
16931693
// must act as always.
@@ -1704,7 +1704,7 @@ impl<'a> Formatter<'a> {
17041704
&s
17051705
};
17061706
// The `width` field is more of a `min-width` parameter at this point.
1707-
match self.width() {
1707+
match self.options.width {
17081708
// If we're under the maximum length, and there's no minimum length
17091709
// requirements, then we can just emit the string
17101710
None => self.buf.write_str(s),
@@ -1745,10 +1745,10 @@ impl<'a> Formatter<'a> {
17451745
};
17461746

17471747
for _ in 0..pre_pad {
1748-
self.buf.write_char(self.fill())?;
1748+
self.buf.write_char(self.options.fill)?;
17491749
}
17501750

1751-
Ok(PostPadding::new(self.fill(), post_pad))
1751+
Ok(PostPadding::new(self.options.fill, post_pad))
17521752
}
17531753

17541754
/// Takes the formatted parts and applies the padding.
@@ -1760,12 +1760,12 @@ impl<'a> Formatter<'a> {
17601760
///
17611761
/// Any `numfmt::Part::Copy` parts in `formatted` must contain valid UTF-8.
17621762
unsafe fn pad_formatted_parts(&mut self, formatted: &numfmt::Formatted<'_>) -> Result {
1763-
if let Some(mut width) = self.width() {
1763+
if let Some(mut width) = self.options.width {
17641764
// for the sign-aware zero padding, we render the sign first and
17651765
// behave as if we had no sign from the beginning.
17661766
let mut formatted = formatted.clone();
1767-
let old_fill = self.fill();
1768-
let old_align = self.align();
1767+
let old_fill = self.options.fill;
1768+
let old_align = self.options.align;
17691769
if self.sign_aware_zero_pad() {
17701770
// a sign always goes first
17711771
let sign = formatted.sign;
@@ -1774,8 +1774,8 @@ impl<'a> Formatter<'a> {
17741774
// remove the sign from the formatted parts
17751775
formatted.sign = "";
17761776
width = width.saturating_sub(sign.len());
1777-
self.options.fill('0');
1778-
self.options.align(Some(Alignment::Right));
1777+
self.options.fill = '0';
1778+
self.options.align = Some(Alignment::Right);
17791779
}
17801780

17811781
// remaining parts go through the ordinary padding process.
@@ -1792,8 +1792,8 @@ impl<'a> Formatter<'a> {
17921792
}
17931793
post_padding.write(self)
17941794
};
1795-
self.options.fill(old_fill);
1796-
self.options.align(old_align);
1795+
self.options.fill = old_fill;
1796+
self.options.align = old_align;
17971797
ret
17981798
} else {
17991799
// this is the common case and we take a shortcut
@@ -1919,7 +1919,7 @@ impl<'a> Formatter<'a> {
19191919
or `sign_aware_zero_pad` methods instead"
19201920
)]
19211921
pub fn flags(&self) -> u32 {
1922-
self.options.get_flags()
1922+
self.options.flags
19231923
}
19241924

19251925
/// Returns the character used as 'fill' whenever there is alignment.
@@ -1952,7 +1952,7 @@ impl<'a> Formatter<'a> {
19521952
#[must_use]
19531953
#[stable(feature = "fmt_flags", since = "1.5.0")]
19541954
pub fn fill(&self) -> char {
1955-
self.options.get_fill()
1955+
self.options.fill
19561956
}
19571957

19581958
/// Returns a flag indicating what form of alignment was requested.
@@ -1987,7 +1987,7 @@ impl<'a> Formatter<'a> {
19871987
#[must_use]
19881988
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
19891989
pub fn align(&self) -> Option<Alignment> {
1990-
self.options.get_align()
1990+
self.options.align
19911991
}
19921992

19931993
/// Returns the optionally specified integer width that the output should be.
@@ -2017,7 +2017,7 @@ impl<'a> Formatter<'a> {
20172017
#[must_use]
20182018
#[stable(feature = "fmt_flags", since = "1.5.0")]
20192019
pub fn width(&self) -> Option<usize> {
2020-
self.options.get_width()
2020+
self.options.width
20212021
}
20222022

20232023
/// Returns the optionally specified precision for numeric types.
@@ -2048,7 +2048,7 @@ impl<'a> Formatter<'a> {
20482048
#[must_use]
20492049
#[stable(feature = "fmt_flags", since = "1.5.0")]
20502050
pub fn precision(&self) -> Option<usize> {
2051-
self.options.get_precision()
2051+
self.options.precision
20522052
}
20532053

20542054
/// Determines if the `+` flag was specified.
@@ -2080,7 +2080,7 @@ impl<'a> Formatter<'a> {
20802080
#[must_use]
20812081
#[stable(feature = "fmt_flags", since = "1.5.0")]
20822082
pub fn sign_plus(&self) -> bool {
2083-
self.options.get_sign() == Some(Sign::Plus)
2083+
self.options.flags & (1 << rt::Flag::SignPlus as u32) != 0
20842084
}
20852085

20862086
/// Determines if the `-` flag was specified.
@@ -2109,7 +2109,7 @@ impl<'a> Formatter<'a> {
21092109
#[must_use]
21102110
#[stable(feature = "fmt_flags", since = "1.5.0")]
21112111
pub fn sign_minus(&self) -> bool {
2112-
self.options.get_sign() == Some(Sign::Minus)
2112+
self.options.flags & (1 << rt::Flag::SignMinus as u32) != 0
21132113
}
21142114

21152115
/// Determines if the `#` flag was specified.
@@ -2137,7 +2137,7 @@ impl<'a> Formatter<'a> {
21372137
#[must_use]
21382138
#[stable(feature = "fmt_flags", since = "1.5.0")]
21392139
pub fn alternate(&self) -> bool {
2140-
self.options.get_alternate()
2140+
self.options.flags & (1 << rt::Flag::Alternate as u32) != 0
21412141
}
21422142

21432143
/// Determines if the `0` flag was specified.
@@ -2163,7 +2163,7 @@ impl<'a> Formatter<'a> {
21632163
#[must_use]
21642164
#[stable(feature = "fmt_flags", since = "1.5.0")]
21652165
pub fn sign_aware_zero_pad(&self) -> bool {
2166-
self.options.get_sign_aware_zero_pad()
2166+
self.options.flags & (1 << rt::Flag::SignAwareZeroPad as u32) != 0
21672167
}
21682168

21692169
// FIXME: Decide what public API we want for these two flags.
@@ -2753,7 +2753,7 @@ impl Debug for char {
27532753
#[stable(feature = "rust1", since = "1.0.0")]
27542754
impl Display for char {
27552755
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2756-
if f.width().is_none() && f.precision().is_none() {
2756+
if f.options.width.is_none() && f.options.precision.is_none() {
27572757
f.write_char(*self)
27582758
} else {
27592759
f.pad(self.encode_utf8(&mut [0; 4]))
@@ -2777,28 +2777,26 @@ impl<T: ?Sized> Pointer for *const T {
27772777
///
27782778
/// [problematic]: https://github.com/rust-lang/rust/issues/95489
27792779
pub(crate) fn pointer_fmt_inner(ptr_addr: usize, f: &mut Formatter<'_>) -> Result {
2780-
let old_width = f.width();
2781-
let old_alternate = f.alternate();
2782-
let old_zero_pad = f.sign_aware_zero_pad();
2780+
let old_width = f.options.width;
2781+
let old_flags = f.options.flags;
27832782

27842783
// The alternate flag is already treated by LowerHex as being special-
27852784
// it denotes whether to prefix with 0x. We use it to work out whether
27862785
// or not to zero extend, and then unconditionally set it to get the
27872786
// prefix.
27882787
if f.alternate() {
2789-
f.options.sign_aware_zero_pad(true);
2788+
f.options.flags |= 1 << (rt::Flag::SignAwareZeroPad as u32);
27902789

2791-
if f.width().is_none() {
2792-
f.options.width(Some((usize::BITS / 4) as usize + 2));
2790+
if f.options.width.is_none() {
2791+
f.options.width = Some((usize::BITS / 4) as usize + 2);
27932792
}
27942793
}
2795-
f.options.alternate(true);
2794+
f.options.flags |= 1 << (rt::Flag::Alternate as u32);
27962795

27972796
let ret = LowerHex::fmt(&ptr_addr, f);
27982797

2799-
f.options.width(old_width);
2800-
f.options.alternate(old_alternate);
2801-
f.options.sign_aware_zero_pad(old_zero_pad);
2798+
f.options.width = old_width;
2799+
f.options.flags = old_flags;
28022800

28032801
ret
28042802
}

0 commit comments

Comments
 (0)