Skip to content

Commit

Permalink
refactor(error): remove PartialEq derives for error kind enums
Browse files Browse the repository at this point in the history
Replaced the comparisons with `matches!`. This should reduce a bit of
code generation that isn't really needed.
  • Loading branch information
seanmonstar committed Jun 8, 2021
1 parent 8b71a67 commit ea8b0cd
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ struct ErrorImpl {
cause: Option<Cause>,
}

#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub(super) enum Kind {
Parse(Parse),
User(User),
/// A message reached EOF, but is not complete.
#[allow(unused)]
IncompleteMessage,
/// A connection received a message (or bytes) when not waiting for one.
#[cfg(feature = "http1")]
Expand All @@ -34,6 +35,7 @@ pub(super) enum Kind {
#[cfg(any(feature = "http1", feature = "http2"))]
Io,
/// Error occurred while connecting.
#[allow(unused)]
Connect,
/// Error creating a TcpListener.
#[cfg(all(
Expand Down Expand Up @@ -63,7 +65,7 @@ pub(super) enum Kind {
Http2,
}

#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub(super) enum Parse {
Method,
Version,
Expand All @@ -77,7 +79,7 @@ pub(super) enum Parse {
Internal,
}

#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub(super) enum User {
/// Error calling user's HttpBody::poll_data().
#[cfg(any(feature = "http1", feature = "http2"))]
Expand Down Expand Up @@ -152,27 +154,27 @@ impl Error {

/// Returns true if this was about a `Request` that was canceled.
pub fn is_canceled(&self) -> bool {
self.inner.kind == Kind::Canceled
matches!(self.inner.kind, Kind::Canceled)
}

/// Returns true if a sender's channel is closed.
pub fn is_closed(&self) -> bool {
self.inner.kind == Kind::ChannelClosed
matches!(self.inner.kind, Kind::ChannelClosed)
}

/// Returns true if this was an error from `Connect`.
pub fn is_connect(&self) -> bool {
self.inner.kind == Kind::Connect
matches!(self.inner.kind, Kind::Connect)
}

/// Returns true if the connection closed before a message could complete.
pub fn is_incomplete_message(&self) -> bool {
self.inner.kind == Kind::IncompleteMessage
matches!(self.inner.kind, Kind::IncompleteMessage)
}

/// Returns true if the body write was aborted.
pub fn is_body_write_aborted(&self) -> bool {
self.inner.kind == Kind::BodyWriteAborted
matches!(self.inner.kind, Kind::BodyWriteAborted)
}

/// Returns true if the error was caused by a timeout.
Expand Down

0 comments on commit ea8b0cd

Please sign in to comment.