diff --git a/src/as_error.rs b/src/as_error.rs index c775b2b..2262af8 100644 --- a/src/as_error.rs +++ b/src/as_error.rs @@ -8,7 +8,10 @@ pub trait AsError { fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static); } -impl AsError for T { +impl AsError for T +where + T: StdError + Send + Sync + 'static, +{ fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) { self } diff --git a/src/context.rs b/src/context.rs index 88e5406..8581171 100644 --- a/src/context.rs +++ b/src/context.rs @@ -7,7 +7,9 @@ use crate::Error; /// Provides the `context` method for `Result`. pub trait Context { /// Wrap the error value with additional context. - fn context(self, context: C) -> Result; + fn context(self, context: C) -> Result + where + C: Display + Send + Sync + 'static; /// Wrap the error value with additional context lazily. fn with_context(self, f: F) -> Result @@ -16,8 +18,14 @@ pub trait Context { F: FnOnce(&E) -> C; } -impl Context for Result { - fn context(self, context: C) -> Result { +impl Context for Result +where + E: StdError + Send + Sync + 'static, +{ + fn context(self, context: C) -> Result + where + C: Display + Send + Sync + 'static, + { self.map_err(|error| Error::from(ContextError { error, context })) } @@ -36,7 +44,10 @@ impl Context for Result { } impl Context for Result { - fn context(self, context: C) -> Result { + fn context(self, context: C) -> Result + where + C: Display + Send + Sync + 'static, + { self.map_err(|error| Error::from(ContextError { error, context })) } @@ -59,19 +70,30 @@ struct ContextError { context: C, } -impl Debug for ContextError { +impl Debug for ContextError +where + E: Debug, + C: Display, +{ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}\n\n{}", self.error, self.context) } } -impl Display for ContextError { +impl Display for ContextError +where + C: Display, +{ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Display::fmt(&self.context, f) } } -impl StdError for ContextError { +impl StdError for ContextError +where + E: StdError + 'static, + C: Display, +{ fn backtrace(&self) -> Option<&Backtrace> { self.error.backtrace() } @@ -85,7 +107,10 @@ impl StdError for ContextError { } } -impl StdError for ContextError { +impl StdError for ContextError +where + C: Display, +{ fn backtrace(&self) -> Option<&Backtrace> { Some(self.error.backtrace()) } diff --git a/src/error.rs b/src/error.rs index 4775417..8b6b85a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -100,12 +100,18 @@ impl Error { } /// Returns `true` if `E` is the type wrapped by this error object. - pub fn is(&self) -> bool { + pub fn is(&self) -> bool + where + E: Display + Debug + Send + Sync + 'static, + { TypeId::of::() == self.inner.type_id } /// Attempt to downcast the error object to a concrete type. - pub fn downcast(self) -> Result { + pub fn downcast(self) -> Result + where + E: Display + Debug + Send + Sync + 'static, + { if let Some(error) = self.downcast_ref::() { unsafe { let error = ptr::read(error); @@ -119,7 +125,10 @@ impl Error { } /// Downcast this error object by reference. - pub fn downcast_ref(&self) -> Option<&E> { + pub fn downcast_ref(&self) -> Option<&E> + where + E: Display + Debug + Send + Sync + 'static, + { if self.is::() { unsafe { Some(&*(self.inner.error() as *const dyn StdError as *const E)) } } else { @@ -128,7 +137,10 @@ impl Error { } /// Downcast this error object by mutable reference. - pub fn downcast_mut(&mut self) -> Option<&mut E> { + pub fn downcast_mut(&mut self) -> Option<&mut E> + where + E: Display + Debug + Send + Sync + 'static, + { if self.is::() { unsafe { Some(&mut *(self.inner.error_mut() as *mut dyn StdError as *mut E)) } } else { @@ -137,7 +149,10 @@ impl Error { } } -impl From for Error { +impl From for Error +where + E: StdError + Send + Sync + 'static, +{ fn from(error: E) -> Error { Error::new(error) } @@ -221,21 +236,29 @@ struct TraitObject { } #[repr(transparent)] -struct MessageError(M); +struct MessageError(M) +where + M: Display + Debug; -impl Debug for MessageError { +impl Debug for MessageError +where + M: Display + Debug, +{ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Debug::fmt(&self.0, f) } } -impl Display for MessageError { +impl Display for MessageError +where + M: Display + Debug, +{ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Display::fmt(&self.0, f) } } -impl StdError for MessageError {} +impl StdError for MessageError where M: Display + Debug + 'static {} impl ErrorImpl<()> { fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {