From 0e60287b4136bcede0c5eae8ab4e5de8496a16f0 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 23 Jan 2018 17:49:43 +0100 Subject: [PATCH] Implement Display for PanicInfo and Location MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Due to being in libcore, this impl cannot access PanicInfo::payload if it’s a String. --- src/libcore/panic.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libcore/panic.rs b/src/libcore/panic.rs index 2dfd950225c..cf8ceff6cda 100644 --- a/src/libcore/panic.rs +++ b/src/libcore/panic.rs @@ -120,6 +120,23 @@ pub fn location(&self) -> Option<&Location> { } } +impl<'a> fmt::Display for PanicInfo<'a> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("panicked at ")?; + if let Some(message) = self.message { + write!(formatter, "'{}', ", message)? + } else if let Some(payload) = self.payload.downcast_ref::<&'static str>() { + write!(formatter, "'{}', ", payload)? + } + // NOTE: we cannot use downcast_ref::() here + // since String is not available in libcore! + // A String payload and no message is what we’d get from `std::panic!` + // called with multiple arguments. + + self.location.fmt(formatter) + } +} + /// A struct containing information about the location of a panic. /// /// This structure is created by the [`location`] method of [`PanicInfo`]. @@ -226,3 +243,9 @@ pub fn column(&self) -> u32 { self.col } } + +impl<'a> fmt::Display for Location<'a> { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "{}:{}:{}", self.file, self.line, self.col) + } +} -- 2.44.0