about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2018-01-23 17:49:43 +0100
committerSimon Sapin <simon.sapin@exyr.org>2018-01-23 18:04:50 +0100
commit0e60287b4136bcede0c5eae8ab4e5de8496a16f0 (patch)
tree82d0e66ba935501db40b1ed806341a4dfdc2509d /src/libcore
parent9e96c1ef7fcac0ac85b3c9160f5486e91dd27dd2 (diff)
downloadrust-0e60287b4136bcede0c5eae8ab4e5de8496a16f0.tar.gz
rust-0e60287b4136bcede0c5eae8ab4e5de8496a16f0.zip
Implement Display for PanicInfo and Location
Due to being in libcore,
this impl cannot access PanicInfo::payload if it’s a String.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/panic.rs23
1 files changed, 23 insertions, 0 deletions
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 @@ impl<'a> PanicInfo<'a> {
     }
 }
 
+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::<String>() 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 @@ impl<'a> Location<'a> {
         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)
+    }
+}