From e33f0cb4cd6de020329e475ca930cc242b8d9ccb Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Fri, 16 Mar 2018 01:16:44 +0100 Subject: [PATCH 1/2] add Formatter::debug --- src/libcore/fmt/mod.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 67126b496e211..425368ff536d6 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -1537,6 +1537,33 @@ impl<'a> Formatter<'a> { self.flags & (1 << FlagV1::SignAwareZeroPad as u32) != 0 } + /// Calls the `Debug` implementation of `D` on this `Formatter`. + /// This is equivalent to `{ ::fmt(&d, fmt); drop(d); }` + /// but reads better when you are calling other methods on the `Formatter`. + /// + /// # Examples + /// + /// ```rust + /// #![feature(formatter_debug)] + /// use std::fmt; + /// + /// struct Arm(L, R); + /// + /// impl fmt::Debug for Arm { + /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + /// fmt.debug(&self.0)?; + /// fmt.write_str(" => ")?; + /// fmt.debug(&self.1) + /// } + /// } + /// + /// assert_eq!(format!("{:?}", Arm(0, 1)), "0 => 1"); + /// ``` + #[unstable(feature = "formatter_debug", issue = "0")] + fn debug(&mut self, d: D) -> Result { + ::fmt(&d, self) + } + /// Creates a [`DebugStruct`] builder designed to assist with creation of /// [`fmt::Debug`] implementations for structs. /// From a2141551b5bef342e7f017520278649f790201cb Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Fri, 16 Mar 2018 01:55:16 +0100 Subject: [PATCH 2/2] add Formatter::debug - better example + fix visibility --- src/libcore/fmt/mod.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 425368ff536d6..99a08e954f8fa 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -1551,16 +1551,19 @@ impl<'a> Formatter<'a> { /// /// impl fmt::Debug for Arm { /// fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - /// fmt.debug(&self.0)?; - /// fmt.write_str(" => ")?; + /// write!(fmt, "{:?} => ", self.0)?; /// fmt.debug(&self.1) /// } /// } /// - /// assert_eq!(format!("{:?}", Arm(0, 1)), "0 => 1"); + /// // `fmt.debug(..)` respects formatting on the RHS of the arrow: + /// assert_eq!(format!("{:?}", Arm(0, vec![2, 3])), + /// "0 => [2, 3]"); + /// assert_eq!(format!("{:#?}", Arm(0, vec![2, 3])), + /// "0 => [\n 2,\n 3\n]"); /// ``` #[unstable(feature = "formatter_debug", issue = "0")] - fn debug(&mut self, d: D) -> Result { + pub fn debug(&mut self, d: D) -> Result { ::fmt(&d, self) }