Skip to content
Myr

Tagged dispatch for anyhow!($expr)

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 89df7af · Browse files · 2 files+55 added−3 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+55 added−3 removed

  • other:+55 added−3 removed

2 files+55 added−3 removed

added:src/kind.rs+46 added
1use crate::Error;2use std::error::Error as StdError;3use std::fmt::{Debug, Display};45#[cfg(backtrace)]6use std::backtrace::Backtrace;78pub struct Adhoc;910pub trait AdhocKind: Sized {11 #[inline]12 fn anyhow_kind(&self) -> Adhoc {13 Adhoc14 }15}1617impl<T> AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'static {}1819impl Adhoc {20 pub fn new<M>(self, message: M) -> Error21 where22 M: Display + Debug + Send + Sync + 'static,23 {24 Error::from_adhoc(message, backtrace!())25 }26}2728pub struct Trait;2930pub trait TraitKind: Sized {31 #[inline]32 fn anyhow_kind(&self) -> Trait {33 Trait34 }35}3637impl<T> TraitKind for T where T: StdError + Send + Sync + 'static {}3839impl Trait {40 pub fn new<E>(self, error: E) -> Error41 where42 E: StdError + Send + Sync + 'static,43 {44 Error::from_std(error, backtrace!())45 }46}
modified:src/lib.rs+9 added-3 removed
175 unmodified lines
176#[macro_use]177mod backtrace;178mod context;179mod error;180mod kind;181182#[cfg(not(feature = "std"))]183compile_error!("no_std support is not implemented yet");184
201 unmodified lines
386/// }387/// ```388#[macro_export]389macro_rules! anyhow {389 ($msg:expr $(,)?) => {390 $crate::private::new_adhoc($msg)391 };390 ($msg:expr $(,)?) => ({391 #[allow(unused_imports)]392 use $crate::private::{AdhocKind, TraitKind};393 let error = $msg;394 error.anyhow_kind().new(error)395 });396 ($fmt:expr, $($arg:tt)*) => {397 $crate::private::new_adhoc(format!($fmt, $($arg)*))398 };399}
6 unmodified lines
406407 #[cfg(backtrace)]408 use std::backtrace::Backtrace;409410 pub use crate::kind::{AdhocKind, TraitKind};411412 pub fn new_adhoc<M>(message: M) -> Error413 where414 M: Display + Debug + Send + Sync + 'static,415 {
3 unmodified lines