Skip to content

Commit

Permalink
sync: implement more traits for channel errors (#5666)
Browse files Browse the repository at this point in the history
  • Loading branch information
GilShoshan94 committed May 3, 2023
1 parent 52bc6b6 commit 61b68a8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
36 changes: 30 additions & 6 deletions tokio/src/sync/mpsc/error.rs
Expand Up @@ -4,22 +4,28 @@ use std::error::Error;
use std::fmt;

/// Error returned by the `Sender`.
#[derive(Debug)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct SendError<T>(pub T);

impl<T> fmt::Debug for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SendError").finish_non_exhaustive()
}
}

impl<T> fmt::Display for SendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "channel closed")
}
}

impl<T: fmt::Debug> std::error::Error for SendError<T> {}
impl<T> std::error::Error for SendError<T> {}

// ===== TrySendError =====

/// This enumeration is the list of the possible error outcomes for the
/// [try_send](super::Sender::try_send) method.
#[derive(Debug, Eq, PartialEq)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum TrySendError<T> {
/// The data could not be sent on the channel because the channel is
/// currently full and sending would require blocking.
Expand All @@ -30,7 +36,14 @@ pub enum TrySendError<T> {
Closed(T),
}

impl<T: fmt::Debug> Error for TrySendError<T> {}
impl<T> fmt::Debug for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
TrySendError::Full(..) => "Full(..)".fmt(f),
TrySendError::Closed(..) => "Closed(..)".fmt(f),
}
}
}

impl<T> fmt::Display for TrySendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -45,6 +58,8 @@ impl<T> fmt::Display for TrySendError<T> {
}
}

impl<T> Error for TrySendError<T> {}

impl<T> From<SendError<T>> for TrySendError<T> {
fn from(src: SendError<T>) -> TrySendError<T> {
TrySendError::Closed(src.0)
Expand Down Expand Up @@ -96,7 +111,7 @@ impl Error for RecvError {}
cfg_time! {
// ===== SendTimeoutError =====

#[derive(Debug, Eq, PartialEq)]
#[derive(PartialEq, Eq, Clone, Copy)]
/// Error returned by [`Sender::send_timeout`](super::Sender::send_timeout)].
pub enum SendTimeoutError<T> {
/// The data could not be sent on the channel because the channel is
Expand All @@ -108,7 +123,14 @@ cfg_time! {
Closed(T),
}

impl<T: fmt::Debug> Error for SendTimeoutError<T> {}
impl<T> fmt::Debug for SendTimeoutError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
SendTimeoutError::Timeout(..) => "Timeout(..)".fmt(f),
SendTimeoutError::Closed(..) => "Closed(..)".fmt(f),
}
}
}

impl<T> fmt::Display for SendTimeoutError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -122,4 +144,6 @@ cfg_time! {
)
}
}

impl<T> Error for SendTimeoutError<T> {}
}
12 changes: 9 additions & 3 deletions tokio/src/sync/watch.rs
Expand Up @@ -208,18 +208,24 @@ pub mod error {
use std::fmt;

/// Error produced when sending a value fails.
#[derive(Debug)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct SendError<T>(pub T);

// ===== impl SendError =====

impl<T: fmt::Debug> fmt::Display for SendError<T> {
impl<T> fmt::Debug for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SendError").finish_non_exhaustive()
}
}

impl<T> fmt::Display for SendError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "channel closed")
}
}

impl<T: fmt::Debug> std::error::Error for SendError<T> {}
impl<T> std::error::Error for SendError<T> {}

/// Error produced when receiving a change notification.
#[derive(Debug, Clone)]
Expand Down

0 comments on commit 61b68a8

Please sign in to comment.