about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2020-01-21 07:32:40 +0900
committerGitHub <noreply@github.com>2020-01-21 07:32:40 +0900
commite1bd9b33963f51620980ee1450a64ed12e104934 (patch)
tree18bc3e93d227f96192ac3ebfbdaa2a469c1f75b3 /src/libcore
parentec7f209ad426208c9bc40f4e49c870d5364b47b2 (diff)
parenta804a455289cfa6d39ec92903959c4614c48d080 (diff)
downloadrust-e1bd9b33963f51620980ee1450a64ed12e104934.tar.gz
rust-e1bd9b33963f51620980ee1450a64ed12e104934.zip
Rollup merge of #67795 - Mark-Simulacrum:fmt-argument, r=dtolnay
Cleanup formatting code

This removes a few leftover positional enum variants that were no longer used.

All details that are changed are unstable (and `#[doc(hidden)]`), so this should
not impact downstream code.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/fmt/mod.rs75
-rw-r--r--src/libcore/fmt/rt/v1.rs6
2 files changed, 38 insertions, 43 deletions
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs
index e68f3c58a3e..900ef63f1df 100644
--- a/src/libcore/fmt/mod.rs
+++ b/src/libcore/fmt/mod.rs
@@ -10,7 +10,6 @@ use crate::mem;
 use crate::num::flt2dec;
 use crate::ops::Deref;
 use crate::result;
-use crate::slice;
 use crate::str;
 
 mod builders;
@@ -234,8 +233,6 @@ pub struct Formatter<'a> {
     precision: Option<usize>,
 
     buf: &'a mut (dyn Write + 'a),
-    curarg: slice::Iter<'a, ArgumentV1<'a>>,
-    args: &'a [ArgumentV1<'a>],
 }
 
 // NB. Argument is essentially an optimized partially applied formatting function,
@@ -1043,8 +1040,6 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
         buf: output,
         align: rt::v1::Alignment::Unknown,
         fill: ' ',
-        args: args.args,
-        curarg: args.args.iter(),
     };
 
     let mut idx = 0;
@@ -1063,7 +1058,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, &args.args)?;
                 idx += 1;
             }
         }
@@ -1077,6 +1072,39 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
     Ok(())
 }
 
+fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument, args: &[ArgumentV1<'_>]) -> Result {
+    fmt.fill = arg.format.fill;
+    fmt.align = arg.format.align;
+    fmt.flags = arg.format.flags;
+    fmt.width = getcount(args, &arg.format.width);
+    fmt.precision = getcount(args, &arg.format.precision);
+
+    // Extract the correct argument
+    let value = {
+        #[cfg(bootstrap)]
+        {
+            match arg.position {
+                rt::v1::Position::At(i) => args[i],
+            }
+        }
+        #[cfg(not(bootstrap))]
+        {
+            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 {
@@ -1114,41 +1142,6 @@ impl<'a> Formatter<'a> {
             align: self.align,
             width: self.width,
             precision: self.precision,
-
-            // These only exist in the struct for the `run` method,
-            // which won’t be used together with this method.
-            curarg: self.curarg.clone(),
-            args: self.args,
-        }
-    }
-
-    // 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 = match arg.position {
-            rt::v1::Position::Next => *self.curarg.next().unwrap(),
-            rt::v1::Position::At(i) => self.args[i],
-        };
-
-        // 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(),
-            rt::v1::Count::NextParam => self.curarg.next()?.as_usize(),
         }
     }
 
diff --git a/src/libcore/fmt/rt/v1.rs b/src/libcore/fmt/rt/v1.rs
index 826ae36d2d1..fd81f93242b 100644
--- a/src/libcore/fmt/rt/v1.rs
+++ b/src/libcore/fmt/rt/v1.rs
@@ -7,7 +7,10 @@
 
 #[derive(Copy, Clone)]
 pub struct Argument {
+    #[cfg(bootstrap)]
     pub position: Position,
+    #[cfg(not(bootstrap))]
+    pub position: usize,
     pub format: FormatSpec,
 }
 
@@ -37,12 +40,11 @@ pub enum Alignment {
 pub enum Count {
     Is(usize),
     Param(usize),
-    NextParam,
     Implied,
 }
 
+#[cfg(bootstrap)]
 #[derive(Copy, Clone)]
 pub enum Position {
-    Next,
     At(usize),
 }