Skip to content
Myr

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

2 files+41 added−39 removed

modified:src/error.rs+2 added-38 removed
1use crate::backtrace::Backtrace;2use crate::context::ContextError;3use crate::{Chain, Error};4use std::any::TypeId;5use std::error::Error as StdError;6use std::fmt::{self, Debug, Display};7use std::mem::{self, ManuallyDrop};8use std::ops::{Deref, DerefMut};9use std::ptr;1010/// 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 &mdash; exactly one word in19/// size instead of two.20pub struct Error {21 inner: ManuallyDrop<Box<ErrorImpl<()>>>,22}2311impl 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 lines
391}392393// repr C to ensure that `E` remains in the final position394#[repr(C)]408struct ErrorImpl<E> {395pub(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 lines
564 Box::<dyn StdError + Send + Sync>::from(error)565 }566}567581/// 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/// ```600pub struct Chain<'a> {601 next: Option<&'a (dyn StdError + 'static)>,602}603568impl<'a> Iterator for Chain<'a> {569 type Item = &'a (dyn StdError + 'static);570571 fn next(&mut self) -> Option<Self::Item> {
5 unmodified lines
modified:src/lib.rs+39 added-1 removed
181 unmodified lines
182183#[cfg(not(feature = "std"))]184compile_error!("no_std support is not implemented yet");185186use crate::error::ErrorImpl;187use std::error::Error as StdError;188use std::fmt::Display;189use std::mem::ManuallyDrop;190188pub 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 &mdash; exactly one word in200/// size instead of two.201pub struct Error {202 inner: ManuallyDrop<Box<ErrorImpl<()>>>,203}204205/// 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/// ```224pub struct Chain<'a> {225 next: Option<&'a (dyn StdError + 'static)>,226}227228/// `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 any
313 unmodified lines