| 1 | // 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, we |
| 4 | // 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 nothing |
| 6 | // 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 T |
| 15 | // where |
| 16 | // 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 T |
| 24 | // where |
| 25 | // 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 behavior |
| 33 | // of method resolution to perform tagged dispatch. Here we have two traits |
| 34 | // AdhocKind and TraitKind that both have an anyhow_kind() method. AdhocKind is |
| 35 | // implemented whether or not the caller's type has a std error impl, while |
| 36 | // TraitKind is implemented only when a std error impl does exist. The ambiguity |
| 37 | // is resolved by AdhocKind requiring an extra autoref so that it has lower |
| 38 | // 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) |
| 46 | |
| 47 | use craterust-analyzer cargo anyhow 1.0.103 crate/::Errorrust-analyzer cargo anyhow 1.0.103 Error#; |
| 48 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::{Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#}; |
| 49 | |
| 50 | #[cfg(any(feature = "std", not(anyhow_no_core_error)))] |
| 51 | use craterust-analyzer cargo anyhow 1.0.103 crate/::StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#; |
| 52 | #[cfg(any(feature = "std", not(anyhow_no_core_error)))] |
| 53 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::boxedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/::Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#; |
| 54 | |
| 55 | pub struct Adhocrust-analyzer cargo anyhow 1.0.103 kind/Adhoc#; |
| 56 | |
| 57 | #[doc(hidden)] |
| 58 | pub trait AdhocKindrust-analyzer cargo anyhow 1.0.103 kind/AdhocKind#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# { |
| 59 | #[inline] |
| 60 | fn anyhow_kindrust-analyzer cargo anyhow 1.0.103 kind/AdhocKind#anyhow_kind().(&self) -> Adhocrust-analyzer cargo anyhow 1.0.103 kind/Adhoc# { |
| 61 | Adhocrust-analyzer cargo anyhow 1.0.103 kind/Adhoc# |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl<T> AdhocKindrust-analyzer cargo anyhow 1.0.103 kind/AdhocKind# for &T where T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# + Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# + 'static {} |
| 66 | |
| 67 | impl Adhocrust-analyzer cargo anyhow 1.0.103 kind/Adhoc# { |
| 68 | #[cold] |
| 69 | pub fn newrust-analyzer cargo anyhow 1.0.103 kind/impl#[Adhoc]new().<M>(self, message: M) -> Errorrust-analyzer cargo anyhow 1.0.103 Error# |
| 70 | where |
| 71 | M: Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# + Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# + 'static, |
| 72 | { |
| 73 | Errorrust-analyzer cargo anyhow 1.0.103 Error#::construct_from_adhocrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]construct_from_adhoc().(message, backtracerust-analyzer cargo anyhow 1.0.103 backtrace/backtrace!!()) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | pub struct Traitrust-analyzer cargo anyhow 1.0.103 kind/Trait#; |
| 78 | |
| 79 | #[doc(hidden)] |
| 80 | pub trait TraitKindrust-analyzer cargo anyhow 1.0.103 kind/TraitKind#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# { |
| 81 | #[inline] |
| 82 | fn anyhow_kindrust-analyzer cargo anyhow 1.0.103 kind/TraitKind#anyhow_kind().(&self) -> Traitrust-analyzer cargo anyhow 1.0.103 kind/Trait# { |
| 83 | Traitrust-analyzer cargo anyhow 1.0.103 kind/Trait# |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | impl<E> TraitKindrust-analyzer cargo anyhow 1.0.103 kind/TraitKind# for E where E: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Errorrust-analyzer cargo anyhow 1.0.103 Error#> {} |
| 88 | |
| 89 | impl Traitrust-analyzer cargo anyhow 1.0.103 kind/Trait# { |
| 90 | #[cold] |
| 91 | pub fn newrust-analyzer cargo anyhow 1.0.103 kind/impl#[Trait]new().<E>(self, error: E) -> Errorrust-analyzer cargo anyhow 1.0.103 Error# |
| 92 | where |
| 93 | E: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Errorrust-analyzer cargo anyhow 1.0.103 Error#>, |
| 94 | { |
| 95 | error.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().() |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #[cfg(any(feature = "std", not(anyhow_no_core_error)))] |
| 100 | pub struct Boxedrust-analyzer cargo anyhow 1.0.103 kind/Boxed#; |
| 101 | |
| 102 | #[cfg(any(feature = "std", not(anyhow_no_core_error)))] |
| 103 | #[doc(hidden)] |
| 104 | pub trait BoxedKindrust-analyzer cargo anyhow 1.0.103 kind/BoxedKind#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# { |
| 105 | #[inline] |
| 106 | fn anyhow_kindrust-analyzer cargo anyhow 1.0.103 kind/BoxedKind#anyhow_kind().(&self) -> Boxedrust-analyzer cargo anyhow 1.0.103 kind/Boxed# { |
| 107 | Boxedrust-analyzer cargo anyhow 1.0.103 kind/Boxed# |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | #[cfg(any(feature = "std", not(anyhow_no_core_error)))] |
| 112 | impl BoxedKindrust-analyzer cargo anyhow 1.0.103 kind/BoxedKind# for Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync#> {} |
| 113 | |
| 114 | #[cfg(any(feature = "std", not(anyhow_no_core_error)))] |
| 115 | impl Boxedrust-analyzer cargo anyhow 1.0.103 kind/Boxed# { |
| 116 | #[cold] |
| 117 | pub fn newrust-analyzer cargo anyhow 1.0.103 kind/impl#[Boxed]new().(self, error: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn StdErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync#>) -> Errorrust-analyzer cargo anyhow 1.0.103 Error# { |
| 118 | let backtrace = backtrace_if_absentrust-analyzer cargo anyhow 1.0.103 backtrace/backtrace_if_absent!!(&*error); |
| 119 | Errorrust-analyzer cargo anyhow 1.0.103 Error#::construct_from_boxedrust-analyzer cargo anyhow 1.0.103 error/impl#[Error]construct_from_boxed().(error, backtrace) |
| 120 | } |
| 121 | } |