diff --git a/src/error.rs b/src/error.rs index 1723028..ffe39d5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,6 @@ use crate::backtrace::Backtrace; use crate::context::ContextError; +use crate::{Chain, Error}; use std::any::TypeId; use std::error::Error as StdError; use std::fmt::{self, Debug, Display}; @@ -7,20 +8,6 @@ use std::mem::{self, ManuallyDrop}; use std::ops::{Deref, DerefMut}; use std::ptr; -/// The `Error` type, a wrapper around a dynamic error type. -/// -/// `Error` works a lot like `Box`, but with these -/// differences: -/// -/// - `Error` requires that the error is `Send`, `Sync`, and `'static`. -/// - `Error` guarantees that a backtrace is available, even if the underlying -/// error type does not provide one. -/// - `Error` is represented as a narrow pointer — exactly one word in -/// size instead of two. -pub struct Error { - inner: ManuallyDrop>>, -} - impl Error { /// Create a new error object from any error type. /// @@ -405,7 +392,7 @@ where // repr C to ensure that `E` remains in the final position #[repr(C)] -struct ErrorImpl { +pub(crate) struct ErrorImpl { vtable: &'static ErrorVTable, type_id: TypeId, backtrace: Option, @@ -578,29 +565,6 @@ impl From for Box { } } -/// Iterator of a chain of source errors. -/// -/// This type is the iterator returned by [`Error::chain`]. -/// -/// # Example -/// -/// ``` -/// use anyhow::Error; -/// use std::io; -/// -/// pub fn underlying_io_error_kind(error: &Error) -> Option { -/// for cause in error.chain() { -/// if let Some(io_error) = cause.downcast_ref::() { -/// return Some(io_error.kind()); -/// } -/// } -/// None -/// } -/// ``` -pub struct Chain<'a> { - next: Option<&'a (dyn StdError + 'static)>, -} - impl<'a> Iterator for Chain<'a> { type Item = &'a (dyn StdError + 'static); diff --git a/src/lib.rs b/src/lib.rs index b65e9cb..25d03e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,9 +183,47 @@ mod kind; #[cfg(not(feature = "std"))] compile_error!("no_std support is not implemented yet"); +use crate::error::ErrorImpl; +use std::error::Error as StdError; use std::fmt::Display; +use std::mem::ManuallyDrop; -pub use crate::error::{Chain, Error}; +/// The `Error` type, a wrapper around a dynamic error type. +/// +/// `Error` works a lot like `Box`, but with these +/// differences: +/// +/// - `Error` requires that the error is `Send`, `Sync`, and `'static`. +/// - `Error` guarantees that a backtrace is available, even if the underlying +/// error type does not provide one. +/// - `Error` is represented as a narrow pointer — exactly one word in +/// size instead of two. +pub struct Error { + inner: ManuallyDrop>>, +} + +/// Iterator of a chain of source errors. +/// +/// This type is the iterator returned by [`Error::chain`]. +/// +/// # Example +/// +/// ``` +/// use anyhow::Error; +/// use std::io; +/// +/// pub fn underlying_io_error_kind(error: &Error) -> Option { +/// for cause in error.chain() { +/// if let Some(io_error) = cause.downcast_ref::() { +/// return Some(io_error.kind()); +/// } +/// } +/// None +/// } +/// ``` +pub struct Chain<'a> { + next: Option<&'a (dyn StdError + 'static)>, +} /// `Result` ///