kind.rs
This file
Path
src/kind.rs
121 lines · 3.2 KiB · Rust
Last changed by
Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc
History of this fileTake it
The walk stopped after 200 commits: lines older than 553ed38 point at it.
Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 1–49why, one step back1// Tagged dispatch mechanism for resolving the behavior of `anyhow!($expr)`.2//3// When anyhow! is given a single expr argument to turn into anyhow::Error, we4// want the resulting Error to pick up the input's implementation of source()5// and backtrace() if it has a std::error::Error impl, otherwise require nothing6// more than Display and Debug.7//8// Expressed in terms of specialization, we want something like:9//10// trait AnyhowNew {11// fn new(self) -> Error;12// }13//14// impl<T> AnyhowNew for T15// where16// T: Display + Debug + Send + Sync + 'static,17// {18// default fn new(self) -> Error {19// /* no std error impl */20// }21// }22//23// impl<T> AnyhowNew for T24// where25// T: std::error::Error + Send + Sync + 'static,26// {27// fn new(self) -> Error {28// /* use std error's source() and backtrace() */29// }30// }31//32// Since specialization is not stable yet, instead we rely on autoref behavior33// of method resolution to perform tagged dispatch. Here we have two traits34// AdhocKind and TraitKind that both have an anyhow_kind() method. AdhocKind is35// implemented whether or not the caller's type has a std error impl, while36// TraitKind is implemented only when a std error impl does exist. The ambiguity37// is resolved by AdhocKind requiring an extra autoref so that it has lower38// precedence.39//40// The anyhow! macro will set up the call in this form:41//42// #[allow(unused_imports)]43// use $crate::__private::{AdhocKind, TraitKind};44// let error = $msg;45// (&error).anyhow_kind().new(error)4647use crate::Error;48use core::fmt::{Debug, Display};49Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf
line 50why, one step back50#[cfg(any(feature = "std", not(anyhow_no_core_error)))]Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
line 51why, one step back51use crate::StdError;Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf
line 52why, one step back52#[cfg(any(feature = "std", not(anyhow_no_core_error)))]Work around prelude redundant import warningsDavid Tolnaybefore adoptiondfc7bc0
line 53why, one step back53use alloc::boxed::Box;Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 54–56why, one step back5455pub struct Adhoc;56Work around ridiculous rust-analyzer behaviorDavid Tolnaybefore adoption238223a
line 57why, one step back57#[doc(hidden)]Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 58–72why, one step back58pub trait AdhocKind: Sized {59 #[inline]60 fn anyhow_kind(&self) -> Adhoc {61 Adhoc62 }63}6465impl<T> AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'static {}6667impl Adhoc {68 #[cold]69 pub fn new<M>(self, message: M) -> Error70 where71 M: Display + Debug + Send + Sync + 'static,72 {Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc
line 73why, one step back73 Error::construct_from_adhoc(message, backtrace!())Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 74–78why, one step back74 }75}7677pub struct Trait;78Work around ridiculous rust-analyzer behaviorDavid Tolnaybefore adoption238223a
line 79why, one step back79#[doc(hidden)]Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 80–98why, one step back80pub trait TraitKind: Sized {81 #[inline]82 fn anyhow_kind(&self) -> Trait {83 Trait84 }85}8687impl<E> TraitKind for E where E: Into<Error> {}8889impl Trait {90 #[cold]91 pub fn new<E>(self, error: E) -> Error92 where93 E: Into<Error>,94 {95 error.into()96 }97}98Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf
line 99why, one step back99#[cfg(any(feature = "std", not(anyhow_no_core_error)))]Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 100–101why, one step back100pub struct Boxed;101Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf
line 102why, one step back102#[cfg(any(feature = "std", not(anyhow_no_core_error)))]Work around ridiculous rust-analyzer behaviorDavid Tolnaybefore adoption238223a
line 103why, one step back103#[doc(hidden)]Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 104–110why, one step back104pub trait BoxedKind: Sized {105 #[inline]106 fn anyhow_kind(&self) -> Boxed {107 Boxed108 }109}110Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf
line 111why, one step back111#[cfg(any(feature = "std", not(anyhow_no_core_error)))]Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 112–113why, one step back112impl BoxedKind for Box<dyn StdError + Send + Sync> {}113Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf
line 114why, one step back114#[cfg(any(feature = "std", not(anyhow_no_core_error)))]Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 115–118why, one step back115impl Boxed {116 #[cold]117 pub fn new(self, error: Box<dyn StdError + Send + Sync>) -> Error {118 let backtrace = backtrace_if_absent!(&*error);Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc
line 119why, one step back119 Error::construct_from_boxed(error, backtrace)Update clippy.toml msrv to match rust-version in Cargo.tomlDavid Tolnaybefore adoption553ed38
lines 120–121why, one step back120 }121}