From 61b68a8abc7e6824f31eaa51e4b41e00a5471d20 Mon Sep 17 00:00:00 2001 From: Gil Shoshan Date: Wed, 3 May 2023 10:59:07 +0300 Subject: [PATCH] sync: implement more traits for channel errors (#5666) --- tokio/src/sync/mpsc/error.rs | 36 ++++++++++++++++++++++++++++++------ tokio/src/sync/watch.rs | 12 +++++++++--- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/tokio/src/sync/mpsc/error.rs b/tokio/src/sync/mpsc/error.rs index 1c789da0cdd..25b4455bee7 100644 --- a/tokio/src/sync/mpsc/error.rs +++ b/tokio/src/sync/mpsc/error.rs @@ -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(pub T); +impl fmt::Debug for SendError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("SendError").finish_non_exhaustive() + } +} + impl fmt::Display for SendError { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { write!(fmt, "channel closed") } } -impl std::error::Error for SendError {} +impl std::error::Error for SendError {} // ===== 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 { /// The data could not be sent on the channel because the channel is /// currently full and sending would require blocking. @@ -30,7 +36,14 @@ pub enum TrySendError { Closed(T), } -impl Error for TrySendError {} +impl fmt::Debug for TrySendError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + TrySendError::Full(..) => "Full(..)".fmt(f), + TrySendError::Closed(..) => "Closed(..)".fmt(f), + } + } +} impl fmt::Display for TrySendError { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -45,6 +58,8 @@ impl fmt::Display for TrySendError { } } +impl Error for TrySendError {} + impl From> for TrySendError { fn from(src: SendError) -> TrySendError { TrySendError::Closed(src.0) @@ -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 { /// The data could not be sent on the channel because the channel is @@ -108,7 +123,14 @@ cfg_time! { Closed(T), } - impl Error for SendTimeoutError {} + impl fmt::Debug for SendTimeoutError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + SendTimeoutError::Timeout(..) => "Timeout(..)".fmt(f), + SendTimeoutError::Closed(..) => "Closed(..)".fmt(f), + } + } + } impl fmt::Display for SendTimeoutError { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -122,4 +144,6 @@ cfg_time! { ) } } + + impl Error for SendTimeoutError {} } diff --git a/tokio/src/sync/watch.rs b/tokio/src/sync/watch.rs index 449711ad75c..8213da6dea4 100644 --- a/tokio/src/sync/watch.rs +++ b/tokio/src/sync/watch.rs @@ -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(pub T); // ===== impl SendError ===== - impl fmt::Display for SendError { + impl fmt::Debug for SendError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("SendError").finish_non_exhaustive() + } + } + + impl fmt::Display for SendError { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { write!(fmt, "channel closed") } } - impl std::error::Error for SendError {} + impl std::error::Error for SendError {} /// Error produced when receiving a change notification. #[derive(Debug, Clone)]