Skip to content

Commit caaf5da

Browse files
committed
Stop requiring error kinds to impl Display
All kinds of `error::Error` now implements the `Display` trait, and the `Display` for the `Error` itself is inherited. The binding between the kind and `Display` is not necessary though. In this commit I dropped the restriction of error kinds by `Display`, and only implemented the trait for errors with displayable kinds. As a result, the developer got 2 options: - either to implement the `Display` for the kind (preferred), - or implement it for the error only (but not for the kind) (edge case).
1 parent 73e0aa2 commit caaf5da

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

async-nats/src/error.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@
1313

1414
use std::fmt::{Debug, Display};
1515

16+
// A trait to mark enums that can be used as error kinds.
17+
pub trait ErrorKind: Clone + Debug + PartialEq {}
18+
19+
impl<T> ErrorKind for T where T: Clone + Debug + PartialEq {}
20+
1621
/// The error type for the NATS client, generic by the kind of error.
1722
#[derive(Debug)]
1823
pub struct Error<Kind>
1924
where
20-
Kind: Clone + Debug + Display + PartialEq,
25+
Kind: ErrorKind,
2126
{
2227
pub(crate) kind: Kind,
2328
pub(crate) source: Option<crate::Error>,
2429
}
2530

2631
impl<Kind> Error<Kind>
2732
where
28-
Kind: Clone + Debug + Display + PartialEq,
33+
Kind: ErrorKind,
2934
{
3035
pub(crate) fn new(kind: Kind) -> Self {
3136
Self { kind, source: None }
@@ -49,7 +54,7 @@ where
4954

5055
impl<Kind> Display for Error<Kind>
5156
where
52-
Kind: Clone + Debug + Display + PartialEq,
57+
Kind: ErrorKind + Display,
5358
{
5459
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5560
if let Some(err) = &self.source {
@@ -60,11 +65,16 @@ where
6065
}
6166
}
6267

63-
impl<Kind> std::error::Error for Error<Kind> where Kind: Clone + Debug + Display + PartialEq {}
68+
impl<Kind> std::error::Error for Error<Kind>
69+
where
70+
Kind: ErrorKind,
71+
Error<Kind>: Display,
72+
{
73+
}
6474

6575
impl<Kind> From<Kind> for Error<Kind>
6676
where
67-
Kind: Clone + Debug + Display + PartialEq,
77+
Kind: ErrorKind,
6878
{
6979
fn from(kind: Kind) -> Self {
7080
Self { kind, source: None }
@@ -75,7 +85,7 @@ where
7585
/// by additionally specifying the kind of the target error.
7686
trait WithKind<Kind>
7787
where
78-
Kind: Clone + Debug + Display + PartialEq,
88+
Kind: ErrorKind,
7989
Self: Into<crate::Error>,
8090
{
8191
fn with_kind(self, kind: Kind) -> Error<Kind> {
@@ -88,7 +98,7 @@ where
8898

8999
impl<E, Kind> WithKind<Kind> for E
90100
where
91-
Kind: Clone + Debug + Display + PartialEq,
101+
Kind: ErrorKind,
92102
E: Into<crate::Error>,
93103
{
94104
}

0 commit comments

Comments
 (0)