2023-06-29 00:00:42 -04:00
|
|
|
use core::panic::Location;
|
|
|
|
|
2022-11-09 19:48:43 -05:00
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
use crate::report::report_write;
|
|
|
|
|
2023-06-29 01:51:08 -04:00
|
|
|
/// The error type.
|
2023-06-29 00:00:42 -04:00
|
|
|
///
|
|
|
|
/// By default, does not implement [`Clone`] because [`std::backtrace::Backtrace`] does not
|
|
|
|
/// implement [`Clone`]. However, the `clone-with-caveats` feature may be used to enable a
|
|
|
|
/// [`Clone`] impl that sets the cloned `backtrace` to [`std::backtrace::Backtrace::disabled`].
|
2023-07-01 12:58:07 -04:00
|
|
|
#[cfg_attr(any(feature = "arc-backtrace", feature = "clone-with-caveats"), derive(Clone))]
|
2022-11-09 19:48:43 -05:00
|
|
|
pub struct How(Box<HowInner>);
|
|
|
|
|
|
|
|
struct HowInner {
|
2023-06-29 00:00:42 -04:00
|
|
|
location: &'static Location<'static>,
|
2022-11-09 19:48:43 -05:00
|
|
|
// TODO: consider storing this vec inline (sharing the allocation with rest of the struct.
|
|
|
|
// Probably move after `backtrace`)
|
|
|
|
context: Vec<Context>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl How {
|
|
|
|
#[must_use]
|
|
|
|
#[inline(never)]
|
2023-01-23 19:06:23 -05:00
|
|
|
#[track_caller]
|
2022-11-09 19:48:43 -05:00
|
|
|
pub fn new(context: impl IntoContext) -> Self {
|
2023-06-29 00:00:42 -04:00
|
|
|
let location = Location::caller();
|
2023-07-01 12:35:14 -04:00
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut how = Self(Box::new(HowInner {
|
2023-06-29 00:00:42 -04:00
|
|
|
location,
|
|
|
|
context: Vec::with_capacity(4),
|
2022-11-09 19:48:43 -05:00
|
|
|
}))
|
2023-07-02 15:19:30 -04:00
|
|
|
.frame(context);
|
2023-07-01 12:35:14 -04:00
|
|
|
#[cfg(all(feature = "backtrace", not(feature = "extra-backtrace")))]
|
|
|
|
how.top_mut().extra.push(Detail::backtrace());
|
|
|
|
how
|
2022-11-09 19:48:43 -05:00
|
|
|
}
|
|
|
|
|
2023-07-01 12:35:14 -04:00
|
|
|
#[inline]
|
2023-06-29 00:00:42 -04:00
|
|
|
pub fn location(&self) -> &'static Location {
|
|
|
|
self.0.location
|
2023-02-06 09:09:01 -05:00
|
|
|
}
|
|
|
|
|
2023-07-01 12:35:14 -04:00
|
|
|
#[inline]
|
2023-02-06 09:09:01 -05:00
|
|
|
pub fn top(&self) -> &Context {
|
|
|
|
// SAFETY: we only ever push values into context, and the constructor ensures that there
|
2023-06-29 00:00:42 -04:00
|
|
|
// is at least 1 value in context.
|
|
|
|
let o = self.0.context.iter().next();
|
2023-02-06 09:09:01 -05:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
o.unwrap()
|
|
|
|
} else {
|
|
|
|
#[allow(unsafe_code)]
|
2023-06-29 00:00:42 -04:00
|
|
|
unsafe {
|
|
|
|
o.unwrap_unchecked()
|
|
|
|
}
|
2023-02-06 09:09:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-01 12:35:14 -04:00
|
|
|
#[inline]
|
2023-02-06 09:09:01 -05:00
|
|
|
pub fn bottom(&self) -> &Context {
|
|
|
|
// SAFETY: we only ever push values into context, and the constructor ensures that there
|
2023-06-29 00:00:42 -04:00
|
|
|
// is at least 1 value in context.
|
2023-02-06 09:09:01 -05:00
|
|
|
let o = self.0.context.iter().next_back();
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
o.unwrap()
|
|
|
|
} else {
|
|
|
|
#[allow(unsafe_code)]
|
2023-06-29 00:00:42 -04:00
|
|
|
unsafe {
|
|
|
|
o.unwrap_unchecked()
|
|
|
|
}
|
2023-02-06 09:09:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-01 12:35:14 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn top_mut(&mut self) -> &mut Context {
|
|
|
|
// SAFETY: we only ever push values into context, and the constructor ensures that there
|
|
|
|
// is at least 1 value in context.
|
|
|
|
let o = self.0.context.iter_mut().next();
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
o.unwrap()
|
|
|
|
} else {
|
|
|
|
#[allow(unsafe_code)]
|
|
|
|
unsafe {
|
|
|
|
o.unwrap_unchecked()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn bottom_mut(&mut self) -> &mut Context {
|
|
|
|
// SAFETY: we only ever push values into context, and the constructor ensures that there
|
|
|
|
// is at least 1 value in context.
|
|
|
|
let o = self.0.context.iter_mut().next_back();
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
o.unwrap()
|
|
|
|
} else {
|
|
|
|
#[allow(unsafe_code)]
|
|
|
|
unsafe {
|
|
|
|
o.unwrap_unchecked()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-02-06 09:09:01 -05:00
|
|
|
pub fn into_context(self) -> impl Iterator<Item = Context> {
|
|
|
|
self.0.context.into_iter()
|
|
|
|
}
|
|
|
|
|
2023-01-17 07:29:58 -05:00
|
|
|
fn fmt_debug_alternate(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
let mut b = f.debug_struct(std::any::type_name::<Self>());
|
2023-06-29 00:00:42 -04:00
|
|
|
b.field("location", &(&self.0.location));
|
|
|
|
b.field("context", &(&self.0.context));
|
2023-01-17 07:29:58 -05:00
|
|
|
b.finish()
|
|
|
|
}
|
2023-06-29 01:51:08 -04:00
|
|
|
|
2023-07-01 12:35:14 -04:00
|
|
|
#[inline]
|
2023-06-29 01:51:08 -04:00
|
|
|
pub(crate) fn push_context(&mut self, context: Context) {
|
|
|
|
self.0.context.push(context);
|
|
|
|
}
|
2022-11-09 19:48:43 -05:00
|
|
|
}
|
|
|
|
|
2023-07-01 12:58:07 -04:00
|
|
|
#[cfg(any(feature = "arc-backtrace", feature = "clone-with-caveats"))]
|
2023-06-29 02:14:03 -04:00
|
|
|
impl Clone for HowInner {
|
2023-06-29 00:00:42 -04:00
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> Self {
|
2023-06-29 02:14:03 -04:00
|
|
|
Self {
|
|
|
|
location: self.location,
|
|
|
|
context: self.context.clone(),
|
|
|
|
}
|
2023-06-29 00:00:42 -04:00
|
|
|
}
|
|
|
|
|
2023-07-01 12:35:14 -04:00
|
|
|
#[inline]
|
2023-06-29 00:00:42 -04:00
|
|
|
fn clone_from(&mut self, source: &Self) {
|
2023-06-29 02:14:03 -04:00
|
|
|
self.location = source.location;
|
|
|
|
self.context.clone_from(&source.context);
|
2023-06-29 00:00:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 01:51:08 -04:00
|
|
|
impl std::error::Error for How {}
|
2022-11-09 19:48:43 -05:00
|
|
|
|
|
|
|
impl std::fmt::Display for How {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2023-06-29 00:00:42 -04:00
|
|
|
for (i, ctx) in self.0.context.iter().enumerate() {
|
|
|
|
if i != 0 {
|
|
|
|
f.write_str("\n")?;
|
|
|
|
}
|
2023-07-03 18:44:28 -04:00
|
|
|
std::fmt::Display::fmt(ctx, f)?;
|
2022-11-09 19:48:43 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 07:29:58 -05:00
|
|
|
|
|
|
|
impl std::fmt::Debug for How {
|
|
|
|
#[inline(always)]
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
if f.alternate() {
|
|
|
|
self.fmt_debug_alternate(f)
|
|
|
|
} else {
|
|
|
|
std::fmt::Display::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|