Skip to content

Commit 9d392bc

Browse files
EliasHolzmanngitbot
authored and
gitbot
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 0702ebd commit 9d392bc

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

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.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.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.precision() {
184184
// this behavior of {:.PREC?} predates exponential formatting for {:?}
185185
float_to_decimal_common_exact(fmt, num, sign, precision)
186186
} else {

core/src/fmt/mod.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ impl<'a> Formatter<'a> {
13651365
}
13661366

13671367
// The `width` field is more of a `min-width` parameter at this point.
1368-
match self.width {
1368+
match self.width() {
13691369
// If there's no minimum length requirements then we can just
13701370
// write the bytes.
13711371
None => {
@@ -1433,12 +1433,12 @@ impl<'a> Formatter<'a> {
14331433
#[stable(feature = "rust1", since = "1.0.0")]
14341434
pub fn pad(&mut self, s: &str) -> Result {
14351435
// Make sure there's a fast path up front
1436-
if self.width.is_none() && self.precision.is_none() {
1436+
if self.width().is_none() && self.precision().is_none() {
14371437
return self.buf.write_str(s);
14381438
}
14391439
// The `precision` field can be interpreted as a `max-width` for the
14401440
// string being formatted.
1441-
let s = if let Some(max) = self.precision {
1441+
let s = if let Some(max) = self.precision() {
14421442
// If our string is longer that the precision, then we must have
14431443
// truncation. However other flags like `fill`, `width` and `align`
14441444
// must act as always.
@@ -1455,7 +1455,7 @@ impl<'a> Formatter<'a> {
14551455
&s
14561456
};
14571457
// The `width` field is more of a `min-width` parameter at this point.
1458-
match self.width {
1458+
match self.width() {
14591459
// If we're under the maximum length, and there's no minimum length
14601460
// requirements, then we can just emit the string
14611461
None => self.buf.write_str(s),
@@ -1501,10 +1501,10 @@ impl<'a> Formatter<'a> {
15011501
};
15021502

15031503
for _ in 0..pre_pad {
1504-
self.buf.write_char(self.fill)?;
1504+
self.buf.write_char(self.fill())?;
15051505
}
15061506

1507-
Ok(PostPadding::new(self.fill, post_pad))
1507+
Ok(PostPadding::new(self.fill(), post_pad))
15081508
}
15091509

15101510
/// Takes the formatted parts and applies the padding.
@@ -1516,12 +1516,12 @@ impl<'a> Formatter<'a> {
15161516
///
15171517
/// Any `numfmt::Part::Copy` parts in `formatted` must contain valid UTF-8.
15181518
unsafe fn pad_formatted_parts(&mut self, formatted: &numfmt::Formatted<'_>) -> Result {
1519-
if let Some(mut width) = self.width {
1519+
if let Some(mut width) = self.width() {
15201520
// for the sign-aware zero padding, we render the sign first and
15211521
// behave as if we had no sign from the beginning.
15221522
let mut formatted = formatted.clone();
1523-
let old_fill = self.fill;
1524-
let old_align = self.align;
1523+
let old_fill = self.fill();
1524+
let old_align = self.align();
15251525
if self.sign_aware_zero_pad() {
15261526
// a sign always goes first
15271527
let sign = formatted.sign;
@@ -2502,7 +2502,7 @@ impl Debug for char {
25022502
#[stable(feature = "rust1", since = "1.0.0")]
25032503
impl Display for char {
25042504
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2505-
if f.width.is_none() && f.precision.is_none() {
2505+
if f.width().is_none() && f.precision().is_none() {
25062506
f.write_char(*self)
25072507
} else {
25082508
f.pad(self.encode_utf8(&mut [0; 4]))
@@ -2526,8 +2526,8 @@ impl<T: ?Sized> Pointer for *const T {
25262526
///
25272527
/// [problematic]: https://github.com/rust-lang/rust/issues/95489
25282528
pub(crate) fn pointer_fmt_inner(ptr_addr: usize, f: &mut Formatter<'_>) -> Result {
2529-
let old_width = f.width;
2530-
let old_flags = f.flags;
2529+
let old_width = f.width();
2530+
let old_flags = f.flags();
25312531

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

2539-
if f.width.is_none() {
2539+
if f.width().is_none() {
25402540
f.width = Some((usize::BITS / 4) as usize + 2);
25412541
}
25422542
}

0 commit comments

Comments
 (0)