From 491867fbbb672d9cb5044e9357ae43b980160126 Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 11 Apr 2024 16:08:26 +0100 Subject: [PATCH] sync::oneshot::{Try,}{Send,Recv}Error{,}: Added send + recv + generic error (with consuming variants for re-triable operations.) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for reverse's current commit: Small blessing − 小吉 --- src/sync/mod.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/src/sync/mod.rs b/src/sync/mod.rs index d023457..ba74bb9 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -58,19 +58,25 @@ pub mod oneshot { Cancelled, } + //TODO: Add impl Send/RecvError: `fn can_retry(&self) -> bool` + impl fmt::Display for SendError { + #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - todo!("`Sender` Error message") + f.write_str("send error") + // TODO: Should we `match self` for detailed error messages here? } } impl fmt::Display for RecvError { + #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - todo!("`Receiver` Error message") + f.write_str("recv error") + // TODO: Should we `match self` for detailed error messages here? } } @@ -93,10 +99,7 @@ pub mod oneshot { #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Recv(_) => f.write_str("recv error"), - Self::Send(_) => f.write_str("send error"), - } + f.write_str("oneshot error") } } @@ -129,6 +132,55 @@ pub mod oneshot { } //TODO: impl fmt::Display error::Error for Try*Error[]... + impl fmt::Display for TrySendError + { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result + { + write!(f, "send error (T = {})", std::any::type_name::()) + } + } + impl fmt::Display for TryRecvError + { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result + { + write!(f, "recv error (T = {})", std::any::type_name::()) + } + } + impl error::Error for TryRecvError + where T: fmt::Debug + { + #[inline] + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + Some(&self.0) + } + } + impl error::Error for TrySendError + where T: fmt::Debug + { + #[inline] + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + Some(&self.0) + } + } + impl fmt::Display for TryError + { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result + { + write!(f, "oneshot error (T = {})", std::any::type_name::()) + } + } + impl error::Error for TryError + where T: fmt::Debug + { + #[inline] + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + Some(&self.0) + } + } + //TODO: XXX: We might also want explicit `Debug` impls for all `Try*Error`s, since `T` is irrelevent to the `Error` part. /// Error when attempting to send a value using a `try_` function.