Skip to content

Commit 8c96149

Browse files
committed
Formatter: Access members via getter methods wherever possible
The idea behind this is to make implementing `fmt::FormattingOptions` (as well as any future changes to `std::Formatter`) easier. In theory, this might have a negative performance impact because of the additional function calls. However, I strongly believe that those will be inlined anyway, thereby producing assembly code that has comparable performance.
1 parent e45a937 commit 8c96149

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

library/core/src/fmt/float.rs

Lines changed: 3 additions & 3 deletions
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.precision() {
9090
float_to_decimal_common_exact(fmt, num, sign, precision)
9191
} else {
9292
let min_precision = 0;
@@ -161,7 +161,7 @@ where
161161
true => flt2dec::Sign::MinusPlus,
162162
};
163163

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

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

library/core/src/fmt/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ impl<'a> Formatter<'a> {
12981298
}
12991299

13001300
// The `width` field is more of a `min-width` parameter at this point.
1301-
match self.width {
1301+
match self.width() {
13021302
// If there's no minimum length requirements then we can just
13031303
// write the bytes.
13041304
None => {
@@ -1365,12 +1365,12 @@ impl<'a> Formatter<'a> {
13651365
#[stable(feature = "rust1", since = "1.0.0")]
13661366
pub fn pad(&mut self, s: &str) -> Result {
13671367
// Make sure there's a fast path up front
1368-
if self.width.is_none() && self.precision.is_none() {
1368+
if self.width().is_none() && self.precision().is_none() {
13691369
return self.buf.write_str(s);
13701370
}
13711371
// The `precision` field can be interpreted as a `max-width` for the
13721372
// string being formatted.
1373-
let s = if let Some(max) = self.precision {
1373+
let s = if let Some(max) = self.precision() {
13741374
// If our string is longer that the precision, then we must have
13751375
// truncation. However other flags like `fill`, `width` and `align`
13761376
// must act as always.
@@ -1387,7 +1387,7 @@ impl<'a> Formatter<'a> {
13871387
&s
13881388
};
13891389
// The `width` field is more of a `min-width` parameter at this point.
1390-
match self.width {
1390+
match self.width() {
13911391
// If we're under the maximum length, and there's no minimum length
13921392
// requirements, then we can just emit the string
13931393
None => self.buf.write_str(s),
@@ -1432,10 +1432,10 @@ impl<'a> Formatter<'a> {
14321432
};
14331433

14341434
for _ in 0..pre_pad {
1435-
self.buf.write_char(self.fill)?;
1435+
self.buf.write_char(self.fill())?;
14361436
}
14371437

1438-
Ok(PostPadding::new(self.fill, post_pad))
1438+
Ok(PostPadding::new(self.fill(), post_pad))
14391439
}
14401440

14411441
/// Takes the formatted parts and applies the padding.
@@ -1446,12 +1446,12 @@ impl<'a> Formatter<'a> {
14461446
///
14471447
/// Any `numfmt::Part::Copy` parts in `formatted` must contain valid UTF-8.
14481448
unsafe fn pad_formatted_parts(&mut self, formatted: &numfmt::Formatted<'_>) -> Result {
1449-
if let Some(mut width) = self.width {
1449+
if let Some(mut width) = self.width() {
14501450
// for the sign-aware zero padding, we render the sign first and
14511451
// behave as if we had no sign from the beginning.
14521452
let mut formatted = formatted.clone();
1453-
let old_fill = self.fill;
1454-
let old_align = self.align;
1453+
let old_fill = self.fill();
1454+
let old_align = self.align();
14551455
if self.sign_aware_zero_pad() {
14561456
// a sign always goes first
14571457
let sign = formatted.sign;
@@ -2384,7 +2384,7 @@ impl Debug for char {
23842384
#[stable(feature = "rust1", since = "1.0.0")]
23852385
impl Display for char {
23862386
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2387-
if f.width.is_none() && f.precision.is_none() {
2387+
if f.width().is_none() && f.precision().is_none() {
23882388
f.write_char(*self)
23892389
} else {
23902390
f.pad(self.encode_utf8(&mut [0; 4]))
@@ -2408,8 +2408,8 @@ impl<T: ?Sized> Pointer for *const T {
24082408
///
24092409
/// [problematic]: https://github.com/rust-lang/rust/issues/95489
24102410
pub(crate) fn pointer_fmt_inner(ptr_addr: usize, f: &mut Formatter<'_>) -> Result {
2411-
let old_width = f.width;
2412-
let old_flags = f.flags;
2411+
let old_width = f.width();
2412+
let old_flags = f.flags();
24132413

24142414
// The alternate flag is already treated by LowerHex as being special-
24152415
// it denotes whether to prefix with 0x. We use it to work out whether
@@ -2418,7 +2418,7 @@ pub(crate) fn pointer_fmt_inner(ptr_addr: usize, f: &mut Formatter<'_>) -> Resul
24182418
if f.alternate() {
24192419
f.flags |= 1 << (rt::Flag::SignAwareZeroPad as u32);
24202420

2421-
if f.width.is_none() {
2421+
if f.width().is_none() {
24222422
f.width = Some((usize::BITS / 4) as usize + 2);
24232423
}
24242424
}

0 commit comments

Comments
 (0)