Implement Display for PanicInfo and Location
Due to being in libcore, this impl cannot access PanicInfo::payload if it’s a String.
This commit is contained in:
parent
9e96c1ef7f
commit
0e60287b41
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user