Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync: implement more traits for channel errors #5666

Merged
merged 4 commits into from May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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