diff --git a/src/as_error.rs b/src/as_error.rs index 625c493..c775b2b 100644 --- a/src/as_error.rs +++ b/src/as_error.rs @@ -1,6 +1,6 @@ use std::error::Error as StdError; -use crate::Exception; +use crate::Error; /// View type as an error object. pub trait AsError { @@ -20,7 +20,7 @@ impl AsError for dyn StdError + Send + Sync + 'static { } } -impl AsError for Exception { +impl AsError for Error { fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) { &**self } diff --git a/src/context.rs b/src/context.rs index 95d116f..88e5406 100644 --- a/src/context.rs +++ b/src/context.rs @@ -2,32 +2,32 @@ use std::backtrace::Backtrace; use std::error::Error as StdError; use std::fmt::{self, Debug, Display}; -use crate::Exception; +use crate::Error; /// Provides the `context` method for `Result`. pub trait Context { /// Wrap the error value with additional context. - fn context(self, context: C) -> Result; + fn context(self, context: C) -> Result; /// Wrap the error value with additional context lazily. - fn with_context(self, f: F) -> Result + fn with_context(self, f: F) -> Result where C: Display + Send + Sync + 'static, F: FnOnce(&E) -> C; } impl Context for Result { - fn context(self, context: C) -> Result { - self.map_err(|error| Exception::from(ContextError { error, context })) + fn context(self, context: C) -> Result { + self.map_err(|error| Error::from(ContextError { error, context })) } - fn with_context(self, f: F) -> Result + fn with_context(self, f: F) -> Result where C: Display + Send + Sync + 'static, F: FnOnce(&E) -> C, { self.map_err(|error| { - Exception::from(ContextError { + Error::from(ContextError { context: f(&error), error, }) @@ -35,18 +35,18 @@ impl Context for Result { } } -impl Context for Result { - fn context(self, context: C) -> Result { - self.map_err(|error| Exception::from(ContextError { error, context })) +impl Context for Result { + fn context(self, context: C) -> Result { + self.map_err(|error| Error::from(ContextError { error, context })) } - fn with_context(self, f: F) -> Result + fn with_context(self, f: F) -> Result where C: Display + Send + Sync + 'static, - F: FnOnce(&Exception) -> C, + F: FnOnce(&Error) -> C, { self.map_err(|error| { - Exception::from(ContextError { + Error::from(ContextError { context: f(&error), error, }) @@ -85,7 +85,7 @@ impl StdError for ContextError { } } -impl StdError for ContextError { +impl StdError for ContextError { fn backtrace(&self) -> Option<&Backtrace> { Some(self.error.backtrace()) } diff --git a/src/error.rs b/src/error.rs index 0966d7d..bf5ba90 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,42 +6,42 @@ use std::mem; use std::ops::{Deref, DerefMut}; use std::ptr; -/// The `Exception type, a wrapper around a dynamic error type. +/// The `Error` type, a wrapper around a dynamic error type. /// -/// `Exception` functions a lot like `Box`, with these differences: +/// `Error` functions a lot like `Box`, with these differences: /// -/// - `Exception` requires that the error is `Send`, `Sync`, and `'static` -/// - `Exception` guarantees that a backtrace will exist, even if the error type +/// - `Error` requires that the error is `Send`, `Sync`, and `'static` +/// - `Error` guarantees that a backtrace will exist, even if the error type /// did not provide one -/// - `Exception` is represented as a narrow pointer - exactly one word in size, +/// - `Error` is represented as a narrow pointer - exactly one word in size, /// instead of two. -pub struct Exception { +pub struct Error { inner: Box>, } -impl Exception { +impl Error { /// Create a new exception from any error type. /// - /// The error type must be threadsafe and `'static`, so that the `Exception` will be as well. + /// The error type must be threadsafe and `'static`, so that the `Error` will be as well. /// /// If the error type does not provide a backtrace, a backtrace will be created here to ensure /// that a backtrace exists. - pub fn new(error: E) -> Exception + pub fn new(error: E) -> Error where E: StdError + Send + Sync + 'static, { - Exception::construct(error, TypeId::of::()) + Error::construct(error, TypeId::of::()) } #[doc(hidden)] - pub fn new_adhoc(message: M) -> Exception + pub fn new_adhoc(message: M) -> Error where M: Display + Debug + Send + Sync + 'static, { - Exception::construct(MessageError(message), TypeId::of::()) + Error::construct(MessageError(message), TypeId::of::()) } - fn construct(error: E, type_id: TypeId) -> Exception + fn construct(error: E, type_id: TypeId) -> Error where E: StdError + Send + Sync + 'static, { @@ -58,7 +58,7 @@ impl Exception { backtrace, error, }; - Exception { + Error { inner: mem::transmute(Box::new(inner)), } } @@ -74,7 +74,7 @@ impl Exception { &mut **self } - /// Get the backtrace for this Exception. + /// Get the backtrace for this Error. pub fn backtrace(&self) -> &Backtrace { // NB: this unwrap can only fail if the underlying error's backtrace method is // nondeterministic, which would only happen in maliciously constructed code @@ -85,7 +85,7 @@ impl Exception { .expect("exception backtrace capture failed") } - /// An iterator of errors contained by this Exception. + /// An iterator of errors contained by this Error. /// /// This iterator will visit every error in the "cause chain" of this exception, beginning with /// the error that this exception was created from. @@ -101,7 +101,7 @@ impl Exception { } /// Attempt to downcast the exception to a concrete type. - pub fn downcast(self) -> Result { + pub fn downcast(self) -> Result { if let Some(error) = self.downcast_ref::() { unsafe { let error = ptr::read(error); @@ -133,26 +133,26 @@ impl Exception { } } -impl From for Exception { - fn from(error: E) -> Exception { - Exception::new(error) +impl From for Error { + fn from(error: E) -> Error { + Error::new(error) } } -impl Deref for Exception { +impl Deref for Error { type Target = dyn StdError + Send + Sync + 'static; fn deref(&self) -> &Self::Target { self.inner.error() } } -impl DerefMut for Exception { +impl DerefMut for Error { fn deref_mut(&mut self) -> &mut Self::Target { self.inner.error_mut() } } -impl Debug for Exception { +impl Debug for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!(f, "{}", self.inner.error())?; @@ -186,16 +186,16 @@ impl Debug for Exception { } } -impl Display for Exception { +impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.inner.error()) } } -unsafe impl Send for Exception {} -unsafe impl Sync for Exception {} +unsafe impl Send for Error {} +unsafe impl Sync for Error {} -impl Drop for Exception { +impl Drop for Error { fn drop(&mut self) { unsafe { ptr::drop_in_place(self.inner.error_mut()) } } @@ -254,7 +254,7 @@ impl InnerException<()> { } } -/// Iterator of errors in an `Exception`. +/// Iterator of errors in an `Error`. pub struct Errors<'a> { next: Option<&'a (dyn StdError + 'static)>, } @@ -278,13 +278,13 @@ mod repr_correctness { #[test] fn size_of_exception() { - assert_eq!(mem::size_of::(), mem::size_of::()); + assert_eq!(mem::size_of::(), mem::size_of::()); } #[allow(dead_code)] fn assert_exception_autotraits() where - Exception: Unpin + Send + Sync + 'static, + Error: Unpin + Send + Sync + 'static, { } @@ -310,7 +310,7 @@ mod repr_correctness { let has_dropped = Arc::new(Mutex::new(false)); - drop(Exception::from(HasDrop(Box::new(has_dropped.clone())))); + drop(Error::from(HasDrop(Box::new(has_dropped.clone())))); assert!(*has_dropped.lock().unwrap()); } diff --git a/src/lib.rs b/src/lib.rs index fb254c2..416eaec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ mod error; pub use crate::as_error::AsError; pub use crate::context::Context; -pub use crate::error::{Errors, Exception}; +pub use crate::error::{Error, Errors}; /// Return early with an error. /// @@ -20,10 +20,10 @@ macro_rules! bail { /// Construct an ad-hoc exception from a string. /// -/// This evaluates to an `Exception`. It can take either just a string, or a format string with +/// This evaluates to an `Error`. It can take either just a string, or a format string with /// arguments. It also can take any custom type which implements `Debug` and `Display`. #[macro_export] macro_rules! error { - ($e:expr) => { $crate::Exception::new_adhoc($e) }; - ($($arg:tt)*) => { $crate::Exception::new_adhoc(format!($($arg)*)) }; + ($e:expr) => { $crate::Error::new_adhoc($e) }; + ($($arg:tt)*) => { $crate::Error::new_adhoc(format!($($arg)*)) }; }