mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-09-29 22:11:55 +00:00
Also move Error and Chain to get usable paths in diagnostics
This commit is contained in:
parent
fc44da4836
commit
27fcdd42a0
40
src/error.rs
40
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<dyn std::error::Error>`, 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<Box<ErrorImpl<()>>>,
|
||||
}
|
||||
|
||||
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<E> {
|
||||
pub(crate) struct ErrorImpl<E> {
|
||||
vtable: &'static ErrorVTable,
|
||||
type_id: TypeId,
|
||||
backtrace: Option<Backtrace>,
|
||||
@ -578,29 +565,6 @@ impl From<Error> for Box<dyn StdError + 'static> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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<io::ErrorKind> {
|
||||
/// for cause in error.chain() {
|
||||
/// if let Some(io_error) = cause.downcast_ref::<io::Error>() {
|
||||
/// 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);
|
||||
|
||||
|
40
src/lib.rs
40
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<dyn std::error::Error>`, 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<Box<ErrorImpl<()>>>,
|
||||
}
|
||||
|
||||
/// 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<io::ErrorKind> {
|
||||
/// for cause in error.chain() {
|
||||
/// if let Some(io_error) = cause.downcast_ref::<io::Error>() {
|
||||
/// return Some(io_error.kind());
|
||||
/// }
|
||||
/// }
|
||||
/// None
|
||||
/// }
|
||||
/// ```
|
||||
pub struct Chain<'a> {
|
||||
next: Option<&'a (dyn StdError + 'static)>,
|
||||
}
|
||||
|
||||
/// `Result<T, Error>`
|
||||
///
|
||||
|
Loading…
x
Reference in New Issue
Block a user