Skip to content

Commit 4919b96

Browse files
Move run/getcount to functions
These are only called from one place and don't generally support being called from other places; furthermore, they're the only formatter functions that look at the `args` field (which a future commit will remove).
1 parent fdef4f1 commit 4919b96

File tree

1 file changed

+35
-38
lines changed

1 file changed

+35
-38
lines changed

src/libcore/fmt/mod.rs

+35-38
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
10601060
// a string piece.
10611061
for (arg, piece) in fmt.iter().zip(args.pieces.iter()) {
10621062
formatter.buf.write_str(*piece)?;
1063-
formatter.run(arg)?;
1063+
run(&mut formatter, arg)?;
10641064
idx += 1;
10651065
}
10661066
}
@@ -1074,6 +1074,40 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
10741074
Ok(())
10751075
}
10761076

1077+
fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument) -> Result {
1078+
// Fill in the format parameters into the formatter
1079+
fmt.fill = arg.format.fill;
1080+
fmt.align = arg.format.align;
1081+
fmt.flags = arg.format.flags;
1082+
fmt.width = getcount(&fmt.args, &arg.format.width);
1083+
fmt.precision = getcount(&fmt.args, &arg.format.precision);
1084+
1085+
// Extract the correct argument
1086+
let value = {
1087+
#[cfg(bootstrap)]
1088+
{
1089+
match arg.position {
1090+
rt::v1::Position::At(i) => fmt.args[i],
1091+
}
1092+
}
1093+
#[cfg(not(bootstrap))]
1094+
{
1095+
fmt.args[arg.position]
1096+
}
1097+
};
1098+
1099+
// Then actually do some printing
1100+
(value.formatter)(value.value, fmt)
1101+
}
1102+
1103+
fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::v1::Count) -> Option<usize> {
1104+
match *cnt {
1105+
rt::v1::Count::Is(n) => Some(n),
1106+
rt::v1::Count::Implied => None,
1107+
rt::v1::Count::Param(i) => args[i].as_usize(),
1108+
}
1109+
}
1110+
10771111
/// Padding after the end of something. Returned by `Formatter::padding`.
10781112
#[must_use = "don't forget to write the post padding"]
10791113
struct PostPadding {
@@ -1118,43 +1152,6 @@ impl<'a> Formatter<'a> {
11181152
}
11191153
}
11201154

1121-
// First up is the collection of functions used to execute a format string
1122-
// at runtime. This consumes all of the compile-time statics generated by
1123-
// the format! syntax extension.
1124-
fn run(&mut self, arg: &rt::v1::Argument) -> Result {
1125-
// Fill in the format parameters into the formatter
1126-
self.fill = arg.format.fill;
1127-
self.align = arg.format.align;
1128-
self.flags = arg.format.flags;
1129-
self.width = self.getcount(&arg.format.width);
1130-
self.precision = self.getcount(&arg.format.precision);
1131-
1132-
// Extract the correct argument
1133-
let value = {
1134-
#[cfg(bootstrap)]
1135-
{
1136-
match arg.position {
1137-
rt::v1::Position::At(i) => self.args[i],
1138-
}
1139-
}
1140-
#[cfg(not(bootstrap))]
1141-
{
1142-
self.args[arg.position]
1143-
}
1144-
};
1145-
1146-
// Then actually do some printing
1147-
(value.formatter)(value.value, self)
1148-
}
1149-
1150-
fn getcount(&mut self, cnt: &rt::v1::Count) -> Option<usize> {
1151-
match *cnt {
1152-
rt::v1::Count::Is(n) => Some(n),
1153-
rt::v1::Count::Implied => None,
1154-
rt::v1::Count::Param(i) => self.args[i].as_usize(),
1155-
}
1156-
}
1157-
11581155
// Helper methods used for padding and processing formatting arguments that
11591156
// all formatting traits can use.
11601157

0 commit comments

Comments
 (0)