Also move Error and Chain to get usable paths in diagnostics
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Tolnay · committed by David Tolnay · parent fc44da4 · Browse files · 2 files·+41 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+41 added−39 removed
- other:+41 added−39 removed
Provenance
- Commit
27fcdd4- Tree
35cf9fd- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
fc44da4
2 files changed+41 added−39 removed
modified:src/error.rs+2 added-38 removed
1 use crate::backtrace::Backtrace;2 use crate::context::ContextError;3+use crate::{Chain, Error};4 use std::any::TypeId;5 use std::error::Error as StdError;6 use std::fmt::{self, Debug, Display};7 use std::mem::{self, ManuallyDrop};8 use std::ops::{Deref, DerefMut};9 use std::ptr;10 10-/// The `Error` type, a wrapper around a dynamic error type.11-///12-/// `Error` works a lot like `Box<dyn std::error::Error>`, but with these13-/// differences:14-///15-/// - `Error` requires that the error is `Send`, `Sync`, and `'static`.16-/// - `Error` guarantees that a backtrace is available, even if the underlying17-/// error type does not provide one.18-/// - `Error` is represented as a narrow pointer — exactly one word in19-/// size instead of two.20-pub struct Error {21- inner: ManuallyDrop<Box<ErrorImpl<()>>>,22-}23-11 impl Error {12 /// Create a new error object from any error type.13 ///14 /// The error type must be threadsafe and `'static`, so that the `Error`376 unmodified lines391 }392 393 // repr C to ensure that `E` remains in the final position394 #[repr(C)]408-struct ErrorImpl<E> {395+pub(crate) struct ErrorImpl<E> {396 vtable: &'static ErrorVTable,397 type_id: TypeId,398 backtrace: Option<Backtrace>,399 // NOTE: Don't use directly. Use only through vtable. Erased type may have different alignment.164 unmodified lines564 Box::<dyn StdError + Send + Sync>::from(error)565 }566 }567 581-/// Iterator of a chain of source errors.582-///583-/// This type is the iterator returned by [`Error::chain`].584-///585-/// # Example586-///587-/// ```588-/// use anyhow::Error;589-/// use std::io;590-///591-/// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {592-/// for cause in error.chain() {593-/// if let Some(io_error) = cause.downcast_ref::<io::Error>() {594-/// return Some(io_error.kind());595-/// }596-/// }597-/// None598-/// }599-/// ```600-pub struct Chain<'a> {601- next: Option<&'a (dyn StdError + 'static)>,602-}603-568 impl<'a> Iterator for Chain<'a> {569 type Item = &'a (dyn StdError + 'static);570 571 fn next(&mut self) -> Option<Self::Item> {5 unmodified linesmodified:src/lib.rs+39 added-1 removed
181 unmodified lines182 183 #[cfg(not(feature = "std"))]184 compile_error!("no_std support is not implemented yet");185 186+use crate::error::ErrorImpl;187+use std::error::Error as StdError;188 use std::fmt::Display;189+use std::mem::ManuallyDrop;190 188-pub use crate::error::{Chain, Error};191+/// The `Error` type, a wrapper around a dynamic error type.192+///193+/// `Error` works a lot like `Box<dyn std::error::Error>`, but with these194+/// differences:195+///196+/// - `Error` requires that the error is `Send`, `Sync`, and `'static`.197+/// - `Error` guarantees that a backtrace is available, even if the underlying198+/// error type does not provide one.199+/// - `Error` is represented as a narrow pointer — exactly one word in200+/// size instead of two.201+pub struct Error {202+ inner: ManuallyDrop<Box<ErrorImpl<()>>>,203+}204 205+/// Iterator of a chain of source errors.206+///207+/// This type is the iterator returned by [`Error::chain`].208+///209+/// # Example210+///211+/// ```212+/// use anyhow::Error;213+/// use std::io;214+///215+/// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {216+/// for cause in error.chain() {217+/// if let Some(io_error) = cause.downcast_ref::<io::Error>() {218+/// return Some(io_error.kind());219+/// }220+/// }221+/// None222+/// }223+/// ```224+pub struct Chain<'a> {225+ next: Option<&'a (dyn StdError + 'static)>,226+}227+228 /// `Result<T, Error>`229 ///230 /// This is a reasonable return type to use throughout your application but also231 /// for `fn main`; if you do, failures will be printed along with any313 unmodified lines