Skip to content

Commit ed2157a

Browse files
committed
De-duplicate write_prefix lambda in pad_integral
For smaller code size.
1 parent da5a0cd commit ed2157a

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/libcore/fmt/mod.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,46 +1153,55 @@ impl<'a> Formatter<'a> {
11531153
sign = Some('+'); width += 1;
11541154
}
11551155

1156-
let prefixed = self.alternate();
1157-
if prefixed {
1156+
let prefix = if self.alternate() {
11581157
width += prefix.chars().count();
1159-
}
1158+
Some(prefix)
1159+
} else {
1160+
None
1161+
};
11601162

11611163
// Writes the sign if it exists, and then the prefix if it was requested
1162-
let write_prefix = |f: &mut Formatter| {
1164+
#[inline(never)]
1165+
fn write_prefix(f: &mut Formatter, sign: Option<char>, prefix: Option<&str>) -> Result {
11631166
if let Some(c) = sign {
11641167
f.buf.write_char(c)?;
11651168
}
1166-
if prefixed { f.buf.write_str(prefix) }
1167-
else { Ok(()) }
1168-
};
1169+
if let Some(prefix) = prefix {
1170+
f.buf.write_str(prefix)
1171+
} else {
1172+
Ok(())
1173+
}
1174+
}
11691175

11701176
// The `width` field is more of a `min-width` parameter at this point.
11711177
match self.width {
11721178
// If there's no minimum length requirements then we can just
11731179
// write the bytes.
11741180
None => {
1175-
write_prefix(self)?; self.buf.write_str(buf)
1181+
write_prefix(self, sign, prefix)?;
1182+
self.buf.write_str(buf)
11761183
}
11771184
// Check if we're over the minimum width, if so then we can also
11781185
// just write the bytes.
11791186
Some(min) if width >= min => {
1180-
write_prefix(self)?; self.buf.write_str(buf)
1187+
write_prefix(self, sign, prefix)?;
1188+
self.buf.write_str(buf)
11811189
}
11821190
// The sign and prefix goes before the padding if the fill character
11831191
// is zero
11841192
Some(min) if self.sign_aware_zero_pad() => {
11851193
self.fill = '0';
11861194
self.align = rt::v1::Alignment::Right;
1187-
write_prefix(self)?;
1195+
write_prefix(self, sign, prefix)?;
11881196
self.with_padding(min - width, rt::v1::Alignment::Right, |f| {
11891197
f.buf.write_str(buf)
11901198
})
11911199
}
11921200
// Otherwise, the sign and prefix goes after the padding
11931201
Some(min) => {
11941202
self.with_padding(min - width, rt::v1::Alignment::Right, |f| {
1195-
write_prefix(f)?; f.buf.write_str(buf)
1203+
write_prefix(f, sign, prefix)?;
1204+
f.buf.write_str(buf)
11961205
})
11971206
}
11981207
}

0 commit comments

Comments
 (0)