From d3d3049f3a37687981eb48559470f59337694bf6 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Mon, 1 Apr 2019 02:36:20 +0200 Subject: [PATCH] Forward formatter settings to bounds of `Range` in `fmt::Debug` impl Before this change, formatter settings were lost when printing a `Range`. For example, printing a `Range` with `{:.2?}` would not apply the precision modifier when printing the floats. Now the `Debug` impls look a bit more verbose, but modifier are not lost. --- src/libcore/ops/range.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/libcore/ops/range.rs b/src/libcore/ops/range.rs index 4f71c8e794954..5b6023f2e2cbb 100644 --- a/src/libcore/ops/range.rs +++ b/src/libcore/ops/range.rs @@ -85,7 +85,10 @@ pub struct Range { #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for Range { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{:?}..{:?}", self.start, self.end) + self.start.fmt(fmt)?; + write!(fmt, "..")?; + self.end.fmt(fmt)?; + Ok(()) } } @@ -184,7 +187,9 @@ pub struct RangeFrom { #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for RangeFrom { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{:?}..", self.start) + self.start.fmt(fmt)?; + write!(fmt, "..")?; + Ok(()) } } @@ -266,7 +271,9 @@ pub struct RangeTo { #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for RangeTo { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "..{:?}", self.end) + write!(fmt, "..")?; + self.end.fmt(fmt)?; + Ok(()) } } @@ -467,7 +474,10 @@ impl RangeInclusive { #[stable(feature = "inclusive_range", since = "1.26.0")] impl fmt::Debug for RangeInclusive { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{:?}..={:?}", self.start, self.end) + self.start.fmt(fmt)?; + write!(fmt, "..=")?; + self.end.fmt(fmt)?; + Ok(()) } } @@ -602,7 +612,9 @@ pub struct RangeToInclusive { #[stable(feature = "inclusive_range", since = "1.26.0")] impl fmt::Debug for RangeToInclusive { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "..={:?}", self.end) + write!(fmt, "..=")?; + self.end.fmt(fmt)?; + Ok(()) } }