mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-10-02 15:26:08 +00:00
Format with rustfmt 2019-09-08
This commit is contained in:
parent
b47a6fde74
commit
61cf951a14
@ -1,6 +1,6 @@
|
|||||||
use std::backtrace::Backtrace;
|
use std::backtrace::Backtrace;
|
||||||
use std::fmt::{self, Debug, Display};
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::fmt::{self, Debug, Display};
|
||||||
|
|
||||||
use crate::Exception;
|
use crate::Exception;
|
||||||
|
|
||||||
@ -10,7 +10,8 @@ pub trait Context<T, E> {
|
|||||||
fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Exception>;
|
fn context<C: Display + Send + Sync + 'static>(self, context: C) -> Result<T, Exception>;
|
||||||
|
|
||||||
/// Wrap the error value with additional context lazily.
|
/// Wrap the error value with additional context lazily.
|
||||||
fn with_context<C, F>(self, f: F) -> Result<T, Exception> where
|
fn with_context<C, F>(self, f: F) -> Result<T, Exception>
|
||||||
|
where
|
||||||
C: Display + Send + Sync + 'static,
|
C: Display + Send + Sync + 'static,
|
||||||
F: FnOnce(&E) -> C;
|
F: FnOnce(&E) -> C;
|
||||||
}
|
}
|
||||||
@ -20,11 +21,17 @@ impl<T, E: Error + Send + Sync + 'static> Context<T, E> for Result<T, E> {
|
|||||||
self.map_err(|error| Exception::from(ContextError { error, context }))
|
self.map_err(|error| Exception::from(ContextError { error, context }))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_context<C, F>(self, f: F) -> Result<T, Exception> where
|
fn with_context<C, F>(self, f: F) -> Result<T, Exception>
|
||||||
|
where
|
||||||
C: Display + Send + Sync + 'static,
|
C: Display + Send + Sync + 'static,
|
||||||
F: FnOnce(&E) -> C
|
F: FnOnce(&E) -> C,
|
||||||
{
|
{
|
||||||
self.map_err(|error| Exception::from(ContextError { context: f(&error), error }))
|
self.map_err(|error| {
|
||||||
|
Exception::from(ContextError {
|
||||||
|
context: f(&error),
|
||||||
|
error,
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,11 +40,17 @@ impl<T> Context<T, Exception> for Result<T, Exception> {
|
|||||||
self.map_err(|error| Exception::from(ContextError { error, context }))
|
self.map_err(|error| Exception::from(ContextError { error, context }))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_context<C, F>(self, f: F) -> Result<T, Exception> where
|
fn with_context<C, F>(self, f: F) -> Result<T, Exception>
|
||||||
|
where
|
||||||
C: Display + Send + Sync + 'static,
|
C: Display + Send + Sync + 'static,
|
||||||
F: FnOnce(&Exception) -> C
|
F: FnOnce(&Exception) -> C,
|
||||||
{
|
{
|
||||||
self.map_err(|error| Exception::from(ContextError { context: f(&error), error }))
|
self.map_err(|error| {
|
||||||
|
Exception::from(ContextError {
|
||||||
|
context: f(&error),
|
||||||
|
error,
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,32 +26,40 @@ impl Exception {
|
|||||||
///
|
///
|
||||||
/// If the error type does not provide a backtrace, a backtrace will be created here to ensure
|
/// If the error type does not provide a backtrace, a backtrace will be created here to ensure
|
||||||
/// that a backtrace exists.
|
/// that a backtrace exists.
|
||||||
pub fn new<E>(error: E) -> Exception where
|
pub fn new<E>(error: E) -> Exception
|
||||||
E: Error + Send + Sync + 'static
|
where
|
||||||
|
E: Error + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
Exception::construct(error, TypeId::of::<E>())
|
Exception::construct(error, TypeId::of::<E>())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn new_adhoc<M>(message: M) -> Exception where
|
pub fn new_adhoc<M>(message: M) -> Exception
|
||||||
M: Display + Debug + Send + Sync + 'static
|
where
|
||||||
|
M: Display + Debug + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
Exception::construct(MessageError(message), TypeId::of::<M>())
|
Exception::construct(MessageError(message), TypeId::of::<M>())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn construct<E>(error: E, type_id: TypeId) -> Exception where
|
fn construct<E>(error: E, type_id: TypeId) -> Exception
|
||||||
|
where
|
||||||
E: Error + Send + Sync + 'static,
|
E: Error + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
let backtrace = match error.backtrace() {
|
let backtrace = match error.backtrace() {
|
||||||
Some(_) => None,
|
Some(_) => None,
|
||||||
None => Some(Backtrace::capture()),
|
None => Some(Backtrace::capture()),
|
||||||
};
|
};
|
||||||
let obj: TraitObject = mem::transmute(&error as &dyn Error);
|
let obj: TraitObject = mem::transmute(&error as &dyn Error);
|
||||||
let vtable = obj.vtable;
|
let vtable = obj.vtable;
|
||||||
let inner = InnerException { vtable, type_id, backtrace, error };
|
let inner = InnerException {
|
||||||
|
vtable,
|
||||||
|
type_id,
|
||||||
|
backtrace,
|
||||||
|
error,
|
||||||
|
};
|
||||||
Exception {
|
Exception {
|
||||||
inner: mem::transmute(Box::new(inner))
|
inner: mem::transmute(Box::new(inner)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,7 +78,10 @@ impl Exception {
|
|||||||
pub fn backtrace(&self) -> &Backtrace {
|
pub fn backtrace(&self) -> &Backtrace {
|
||||||
// NB: this unwrap can only fail if the underlying error's backtrace method is
|
// NB: this unwrap can only fail if the underlying error's backtrace method is
|
||||||
// nondeterministic, which would only happen in maliciously constructed code
|
// nondeterministic, which would only happen in maliciously constructed code
|
||||||
self.inner.backtrace.as_ref().or_else(|| self.inner.error().backtrace())
|
self.inner
|
||||||
|
.backtrace
|
||||||
|
.as_ref()
|
||||||
|
.or_else(|| self.inner.error().backtrace())
|
||||||
.expect("exception backtrace capture failed")
|
.expect("exception backtrace capture failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +90,9 @@ impl Exception {
|
|||||||
/// This iterator will visit every error in the "cause chain" of this exception, beginning with
|
/// This iterator will visit every error in the "cause chain" of this exception, beginning with
|
||||||
/// the error that this exception was created from.
|
/// the error that this exception was created from.
|
||||||
pub fn errors(&self) -> Errors<'_> {
|
pub fn errors(&self) -> Errors<'_> {
|
||||||
Errors { next: Some(self.inner.error()) }
|
Errors {
|
||||||
|
next: Some(self.inner.error()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if `E` is the type wrapped by this exception.
|
/// Returns `true` if `E` is the type wrapped by this exception.
|
||||||
@ -105,14 +118,18 @@ impl Exception {
|
|||||||
pub fn downcast_ref<E: Display + Debug + Send + Sync + 'static>(&self) -> Option<&E> {
|
pub fn downcast_ref<E: Display + Debug + Send + Sync + 'static>(&self) -> Option<&E> {
|
||||||
if self.is::<E>() {
|
if self.is::<E>() {
|
||||||
unsafe { Some(&*(self.inner.error() as *const dyn Error as *const E)) }
|
unsafe { Some(&*(self.inner.error() as *const dyn Error as *const E)) }
|
||||||
} else { None }
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Downcast this exception by mutable reference.
|
/// Downcast this exception by mutable reference.
|
||||||
pub fn downcast_mut<E: Display + Debug + Send + Sync + 'static>(&mut self) -> Option<&mut E> {
|
pub fn downcast_mut<E: Display + Debug + Send + Sync + 'static>(&mut self) -> Option<&mut E> {
|
||||||
if self.is::<E>() {
|
if self.is::<E>() {
|
||||||
unsafe { Some(&mut *(self.inner.error_mut() as *mut dyn Error as *mut E)) }
|
unsafe { Some(&mut *(self.inner.error_mut() as *mut dyn Error as *mut E)) }
|
||||||
} else { None }
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,14 +169,17 @@ impl Debug for Exception {
|
|||||||
let backtrace = self.backtrace();
|
let backtrace = self.backtrace();
|
||||||
|
|
||||||
match backtrace.status() {
|
match backtrace.status() {
|
||||||
BacktraceStatus::Captured => {
|
BacktraceStatus::Captured => {
|
||||||
writeln!(f, "\n{}", backtrace)?;
|
writeln!(f, "\n{}", backtrace)?;
|
||||||
}
|
}
|
||||||
BacktraceStatus::Disabled => {
|
BacktraceStatus::Disabled => {
|
||||||
writeln!(f, "\nbacktrace disabled; run with RUST_BACKTRACE=1 environment variable \
|
writeln!(
|
||||||
to display a backtrace")?;
|
f,
|
||||||
|
"\nbacktrace disabled; run with RUST_BACKTRACE=1 environment variable \
|
||||||
|
to display a backtrace"
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
_ => { }
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -172,8 +192,8 @@ impl Display for Exception {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for Exception { }
|
unsafe impl Send for Exception {}
|
||||||
unsafe impl Sync for Exception { }
|
unsafe impl Sync for Exception {}
|
||||||
|
|
||||||
impl Drop for Exception {
|
impl Drop for Exception {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
@ -212,7 +232,7 @@ impl<M: Display + Debug> Display for MessageError<M> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<M: Display + Debug + 'static> Error for MessageError<M> { }
|
impl<M: Display + Debug + 'static> Error for MessageError<M> {}
|
||||||
|
|
||||||
impl InnerException<()> {
|
impl InnerException<()> {
|
||||||
fn error(&self) -> &(dyn Error + Send + Sync + 'static) {
|
fn error(&self) -> &(dyn Error + Send + Sync + 'static) {
|
||||||
@ -241,6 +261,7 @@ pub struct Errors<'a> {
|
|||||||
|
|
||||||
impl<'a> Iterator for Errors<'a> {
|
impl<'a> Iterator for Errors<'a> {
|
||||||
type Item = &'a (dyn Error + 'static);
|
type Item = &'a (dyn Error + 'static);
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let next = self.next.take()?;
|
let next = self.next.take()?;
|
||||||
self.next = next.source();
|
self.next = next.source();
|
||||||
@ -252,24 +273,28 @@ impl<'a> Iterator for Errors<'a> {
|
|||||||
mod repr_correctness {
|
mod repr_correctness {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
use std::mem;
|
|
||||||
use std::marker::Unpin;
|
use std::marker::Unpin;
|
||||||
|
use std::mem;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn size_of_exception() {
|
fn size_of_exception() {
|
||||||
assert_eq!(mem::size_of::<Exception>(), mem::size_of::<usize>());
|
assert_eq!(mem::size_of::<Exception>(), mem::size_of::<usize>());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)] fn assert_exception_autotraits() where
|
#[allow(dead_code)]
|
||||||
Exception: Unpin + Send + Sync + 'static
|
fn assert_exception_autotraits()
|
||||||
{ }
|
where
|
||||||
|
Exception: Unpin + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn destructors_work() {
|
fn destructors_work() {
|
||||||
use std::sync::*;
|
use std::sync::*;
|
||||||
|
|
||||||
#[derive(Debug)] struct HasDrop(Box<Arc<Mutex<bool>>>);
|
#[derive(Debug)]
|
||||||
impl Error for HasDrop { }
|
struct HasDrop(Box<Arc<Mutex<bool>>>);
|
||||||
|
impl Error for HasDrop {}
|
||||||
impl Display for HasDrop {
|
impl Display for HasDrop {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "does something")
|
write!(f, "does something")
|
||||||
@ -288,6 +313,5 @@ mod repr_correctness {
|
|||||||
drop(Exception::from(HasDrop(Box::new(has_dropped.clone()))));
|
drop(Exception::from(HasDrop(Box::new(has_dropped.clone()))));
|
||||||
|
|
||||||
assert!(*has_dropped.lock().unwrap());
|
assert!(*has_dropped.lock().unwrap());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,21 @@
|
|||||||
#![feature(backtrace)]
|
#![feature(backtrace)]
|
||||||
|
|
||||||
mod as_error;
|
mod as_error;
|
||||||
mod exception;
|
|
||||||
mod context;
|
mod context;
|
||||||
|
mod exception;
|
||||||
|
|
||||||
pub use crate::as_error::AsError;
|
pub use crate::as_error::AsError;
|
||||||
pub use crate::exception::{Exception, Errors};
|
|
||||||
pub use crate::context::Context;
|
pub use crate::context::Context;
|
||||||
|
pub use crate::exception::{Errors, Exception};
|
||||||
|
|
||||||
/// Throw an error.
|
/// Throw an error.
|
||||||
///
|
///
|
||||||
/// This macro is equivalent to `Err($err)?`.
|
/// This macro is equivalent to `Err($err)?`.
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! throw {
|
macro_rules! throw {
|
||||||
($err:expr) => (return std::result::Result::Err(std::convert::From::from($err)))
|
($err:expr) => {
|
||||||
|
return std::result::Result::Err(std::convert::From::from($err));
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct an ad-hoc exception from a string.
|
/// Construct an ad-hoc exception from a string.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user