diff --git a/async-nats/src/error.rs b/async-nats/src/error.rs index 13cd633af..770743abb 100644 --- a/async-nats/src/error.rs +++ b/async-nats/src/error.rs @@ -13,11 +13,16 @@ use std::fmt::{Debug, Display}; +// A trait to mark enums that can be used as error kinds. +pub trait ErrorKind: Clone + Debug + PartialEq {} + +impl ErrorKind for T where T: Clone + Debug + PartialEq {} + /// The error type for the NATS client, generic by the kind of error. #[derive(Debug)] pub struct Error where - Kind: Clone + Debug + Display + PartialEq, + Kind: ErrorKind, { pub(crate) kind: Kind, pub(crate) source: Option, @@ -25,7 +30,7 @@ where impl Error where - Kind: Clone + Debug + Display + PartialEq, + Kind: ErrorKind, { pub(crate) fn new(kind: Kind) -> Self { Self { kind, source: None } @@ -49,7 +54,7 @@ where impl Display for Error where - Kind: Clone + Debug + Display + PartialEq, + Kind: ErrorKind + Display, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { if let Some(err) = &self.source { @@ -60,11 +65,16 @@ where } } -impl std::error::Error for Error where Kind: Clone + Debug + Display + PartialEq {} +impl std::error::Error for Error +where + Kind: ErrorKind, + Error: Display, +{ +} impl From for Error where - Kind: Clone + Debug + Display + PartialEq, + Kind: ErrorKind, { fn from(kind: Kind) -> Self { Self { kind, source: None } @@ -75,7 +85,7 @@ where /// by additionally specifying the kind of the target error. trait WithKind where - Kind: Clone + Debug + Display + PartialEq, + Kind: ErrorKind, Self: Into, { fn with_kind(self, kind: Kind) -> Error { @@ -88,7 +98,7 @@ where impl WithKind for E where - Kind: Clone + Debug + Display + PartialEq, + Kind: ErrorKind, E: Into, { }