about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMark Rousskov <mark.simulacrum@gmail.com>2020-01-01 13:58:57 -0500
committerMark Rousskov <mark.simulacrum@gmail.com>2020-01-20 11:57:27 -0500
commit4919b96f81d9df0a7c0fe83ad5d66cef18ddfcfb (patch)
treef5249e9b6b3a33d81e549abd7407e51eb0a1e595
parentfdef4f185ea6a1b560c1370c10ee561135af483d (diff)
downloadrust-4919b96f81d9df0a7c0fe83ad5d66cef18ddfcfb.tar.gz
rust-4919b96f81d9df0a7c0fe83ad5d66cef18ddfcfb.zip
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).
-rw-r--r--src/libcore/fmt/mod.rs73
1 files changed, 35 insertions, 38 deletions
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs
index 64c270522d3..e76a8490f51 100644
--- a/src/libcore/fmt/mod.rs
+++ b/src/libcore/fmt/mod.rs
@@ -1060,7 +1060,7 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
             // a string piece.
             for (arg, piece) in fmt.iter().zip(args.pieces.iter()) {
                 formatter.buf.write_str(*piece)?;
-                formatter.run(arg)?;
+                run(&mut formatter, arg)?;
                 idx += 1;
             }
         }
@@ -1074,6 +1074,40 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
     Ok(())
 }
 
+fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument) -> Result {
+    // Fill in the format parameters into the formatter
+    fmt.fill = arg.format.fill;
+    fmt.align = arg.format.align;
+    fmt.flags = arg.format.flags;
+    fmt.width = getcount(&fmt.args, &arg.format.width);
+    fmt.precision = getcount(&fmt.args, &arg.format.precision);
+
+    // Extract the correct argument
+    let value = {
+        #[cfg(bootstrap)]
+        {
+            match arg.position {
+                rt::v1::Position::At(i) => fmt.args[i],
+            }
+        }
+        #[cfg(not(bootstrap))]
+        {
+            fmt.args[arg.position]
+        }
+    };
+
+    // Then actually do some printing
+    (value.formatter)(value.value, fmt)
+}
+
+fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::v1::Count) -> Option<usize> {
+    match *cnt {
+        rt::v1::Count::Is(n) => Some(n),
+        rt::v1::Count::Implied => None,
+        rt::v1::Count::Param(i) => args[i].as_usize(),
+    }
+}
+
 /// Padding after the end of something. Returned by `Formatter::padding`.
 #[must_use = "don't forget to write the post padding"]
 struct PostPadding {
@@ -1118,43 +1152,6 @@ impl<'a> Formatter<'a> {
         }
     }
 
-    // First up is the collection of functions used to execute a format string
-    // at runtime. This consumes all of the compile-time statics generated by
-    // the format! syntax extension.
-    fn run(&mut self, arg: &rt::v1::Argument) -> Result {
-        // Fill in the format parameters into the formatter
-        self.fill = arg.format.fill;
-        self.align = arg.format.align;
-        self.flags = arg.format.flags;
-        self.width = self.getcount(&arg.format.width);
-        self.precision = self.getcount(&arg.format.precision);
-
-        // Extract the correct argument
-        let value = {
-            #[cfg(bootstrap)]
-            {
-                match arg.position {
-                    rt::v1::Position::At(i) => self.args[i],
-                }
-            }
-            #[cfg(not(bootstrap))]
-            {
-                self.args[arg.position]
-            }
-        };
-
-        // Then actually do some printing
-        (value.formatter)(value.value, self)
-    }
-
-    fn getcount(&mut self, cnt: &rt::v1::Count) -> Option<usize> {
-        match *cnt {
-            rt::v1::Count::Is(n) => Some(n),
-            rt::v1::Count::Implied => None,
-            rt::v1::Count::Param(i) => self.args[i].as_usize(),
-        }
-    }
-
     // Helper methods used for padding and processing formatting arguments that
     // all formatting traits can use.