]> git.lizzy.rs Git - rust.git/blobdiff - src/libcore/fmt/builders.rs
std: Clean out deprecated APIs
[rust.git] / src / libcore / fmt / builders.rs
index 39a067c16083f94da301192c2f4eb1284d59e280..b562e658b62db8c1c619cfe55d76d87619550e4d 100644 (file)
@@ -61,7 +61,8 @@ pub struct DebugStruct<'a, 'b: 'a> {
     has_fields: bool,
 }
 
-pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str)
+pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>,
+                                name: &str)
                                 -> DebugStruct<'a, 'b> {
     let result = fmt.write_str(name);
     DebugStruct {
@@ -84,7 +85,8 @@ pub fn field(&mut self, name: &str, value: &fmt::Debug) -> &mut DebugStruct<'a,
 
             if self.is_pretty() {
                 let mut writer = PadAdapter::new(self.fmt);
-                fmt::write(&mut writer, format_args!("{}\n{}: {:#?}", prefix, name, value))
+                fmt::write(&mut writer,
+                           format_args!("{}\n{}: {:#?}", prefix, name, value))
             } else {
                 write!(self.fmt, "{} {}: {:?}", prefix, name, value)
             }
@@ -122,7 +124,8 @@ fn is_pretty(&self) -> bool {
 pub struct DebugTuple<'a, 'b: 'a> {
     fmt: &'a mut fmt::Formatter<'b>,
     result: fmt::Result,
-    has_fields: bool,
+    fields: usize,
+    empty_name: bool,
 }
 
 pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> DebugTuple<'a, 'b> {
@@ -130,7 +133,8 @@ pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> D
     DebugTuple {
         fmt: fmt,
         result: result,
-        has_fields: false,
+        fields: 0,
+        empty_name: name.is_empty(),
     }
 }
 
@@ -139,7 +143,7 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> {
     #[stable(feature = "debug_builders", since = "1.2.0")]
     pub fn field(&mut self, value: &fmt::Debug) -> &mut DebugTuple<'a, 'b> {
         self.result = self.result.and_then(|_| {
-            let (prefix, space) = if self.has_fields {
+            let (prefix, space) = if self.fields > 0 {
                 (",", " ")
             } else {
                 ("(", "")
@@ -153,20 +157,22 @@ pub fn field(&mut self, value: &fmt::Debug) -> &mut DebugTuple<'a, 'b> {
             }
         });
 
-        self.has_fields = true;
+        self.fields += 1;
         self
     }
 
     /// Finishes output and returns any error encountered.
     #[stable(feature = "debug_builders", since = "1.2.0")]
     pub fn finish(&mut self) -> fmt::Result {
-        if self.has_fields {
+        if self.fields > 0 {
             self.result = self.result.and_then(|_| {
                 if self.is_pretty() {
-                    self.fmt.write_str("\n)")
-                } else {
-                    self.fmt.write_str(")")
+                    try!(self.fmt.write_str("\n"));
+                }
+                if self.fields == 1 && self.empty_name {
+                    try!(self.fmt.write_str(","));
                 }
+                self.fmt.write_str(")")
             });
         }
         self.result
@@ -175,12 +181,6 @@ pub fn finish(&mut self) -> fmt::Result {
     fn is_pretty(&self) -> bool {
         self.fmt.flags() & (1 << (FlagV1::Alternate as usize)) != 0
     }
-
-    /// Returns the wrapped `Formatter`.
-    #[unstable(feature = "debug_builder_formatter", reason = "recently added")]
-    pub fn formatter(&mut self) -> &mut fmt::Formatter<'b> {
-        &mut self.fmt
-    }
 }
 
 struct DebugInner<'a, 'b: 'a> {
@@ -194,10 +194,18 @@ fn entry(&mut self, entry: &fmt::Debug) {
         self.result = self.result.and_then(|_| {
             if self.is_pretty() {
                 let mut writer = PadAdapter::new(self.fmt);
-                let prefix = if self.has_fields { "," } else { "" };
+                let prefix = if self.has_fields {
+                    ","
+                } else {
+                    ""
+                };
                 fmt::write(&mut writer, format_args!("{}\n{:#?}", prefix, entry))
             } else {
-                let prefix = if self.has_fields { ", " } else { "" };
+                let prefix = if self.has_fields {
+                    ", "
+                } else {
+                    ""
+                };
                 write!(self.fmt, "{}{:?}", prefix, entry)
             }
         });
@@ -206,7 +214,11 @@ fn entry(&mut self, entry: &fmt::Debug) {
     }
 
     pub fn finish(&mut self) {
-        let prefix = if self.is_pretty() && self.has_fields { "\n" } else { "" };
+        let prefix = if self.is_pretty() && self.has_fields {
+            "\n"
+        } else {
+            ""
+        };
         self.result = self.result.and_then(|_| self.fmt.write_str(prefix));
     }
 
@@ -231,7 +243,7 @@ pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b
             fmt: fmt,
             result: result,
             has_fields: false,
-        }
+        },
     }
 }
 
@@ -246,7 +258,9 @@ pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugSet<'a, 'b> {
     /// Adds the contents of an iterator of entries to the set output.
     #[stable(feature = "debug_builders", since = "1.2.0")]
     pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugSet<'a, 'b>
-            where D: fmt::Debug, I: IntoIterator<Item=D> {
+        where D: fmt::Debug,
+              I: IntoIterator<Item = D>
+    {
         for entry in entries {
             self.entry(&entry);
         }
@@ -277,7 +291,7 @@ pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a,
             fmt: fmt,
             result: result,
             has_fields: false,
-        }
+        },
     }
 }
 
@@ -292,7 +306,9 @@ pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugList<'a, 'b> {
     /// Adds the contents of an iterator of entries to the list output.
     #[stable(feature = "debug_builders", since = "1.2.0")]
     pub fn entries<D, I>(&mut self, entries: I) -> &mut DebugList<'a, 'b>
-            where D: fmt::Debug, I: IntoIterator<Item=D> {
+        where D: fmt::Debug,
+              I: IntoIterator<Item = D>
+    {
         for entry in entries {
             self.entry(&entry);
         }
@@ -334,10 +350,19 @@ pub fn entry(&mut self, key: &fmt::Debug, value: &fmt::Debug) -> &mut DebugMap<'
         self.result = self.result.and_then(|_| {
             if self.is_pretty() {
                 let mut writer = PadAdapter::new(self.fmt);
-                let prefix = if self.has_fields { "," } else { "" };
-                fmt::write(&mut writer, format_args!("{}\n{:#?}: {:#?}", prefix, key, value))
+                let prefix = if self.has_fields {
+                    ","
+                } else {
+                    ""
+                };
+                fmt::write(&mut writer,
+                           format_args!("{}\n{:#?}: {:#?}", prefix, key, value))
             } else {
-                let prefix = if self.has_fields { ", " } else { "" };
+                let prefix = if self.has_fields {
+                    ", "
+                } else {
+                    ""
+                };
                 write!(self.fmt, "{}{:?}: {:?}", prefix, key, value)
             }
         });
@@ -349,7 +374,10 @@ pub fn entry(&mut self, key: &fmt::Debug, value: &fmt::Debug) -> &mut DebugMap<'
     /// Adds the contents of an iterator of entries to the map output.
     #[stable(feature = "debug_builders", since = "1.2.0")]
     pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b>
-            where K: fmt::Debug, V: fmt::Debug, I: IntoIterator<Item=(K, V)> {
+        where K: fmt::Debug,
+              V: fmt::Debug,
+              I: IntoIterator<Item = (K, V)>
+    {
         for (k, v) in entries {
             self.entry(&k, &v);
         }
@@ -359,7 +387,11 @@ pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b>
     /// Finishes output and returns any error encountered.
     #[stable(feature = "debug_builders", since = "1.2.0")]
     pub fn finish(&mut self) -> fmt::Result {
-        let prefix = if self.is_pretty() && self.has_fields { "\n" } else { "" };
+        let prefix = if self.is_pretty() && self.has_fields {
+            "\n"
+        } else {
+            ""
+        };
         self.result.and_then(|_| write!(self.fmt, "{}}}", prefix))
     }