From 676824988e12d21878ede2d91b773354b6b0981c Mon Sep 17 00:00:00 2001 From: Thomas Lacroix Date: Tue, 12 Mar 2019 16:51:23 +0100 Subject: sync: impl `Error` for oneshot and watch error types (#967) Refs: #937 --- tokio-sync/src/oneshot.rs | 32 ++++++++++++++++++++++++++++++++ tokio-sync/src/watch.rs | 32 ++++++++++++++++++++++++++++++++ tokio-sync/tests/errors.rs | 22 +++++++++++++++++++--- 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/tokio-sync/src/oneshot.rs b/tokio-sync/src/oneshot.rs index f8e82567..bb11d4e6 100644 --- a/tokio-sync/src/oneshot.rs +++ b/tokio-sync/src/oneshot.rs @@ -32,6 +32,8 @@ pub struct Receiver { pub mod error { //! Oneshot error types + use std::fmt; + /// Error returned by the `Future` implementation for `Receiver`. #[derive(Debug)] pub struct RecvError(pub(super) ()); @@ -39,6 +41,36 @@ pub mod error { /// Error returned by the `try_recv` function on `Receiver`. #[derive(Debug)] pub struct TryRecvError(pub(super) ()); + + // ===== impl RecvError ===== + + impl fmt::Display for RecvError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + use std::error::Error; + write!(fmt, "{}", self.description()) + } + } + + impl ::std::error::Error for RecvError { + fn description(&self) -> &str { + "channel closed" + } + } + + // ===== impl TryRecvError ===== + + impl fmt::Display for TryRecvError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + use std::error::Error; + write!(fmt, "{}", self.description()) + } + } + + impl ::std::error::Error for TryRecvError { + fn description(&self) -> &str { + "channel closed" + } + } } use self::error::*; diff --git a/tokio-sync/src/watch.rs b/tokio-sync/src/watch.rs index 4c2b2972..b3303bdc 100644 --- a/tokio-sync/src/watch.rs +++ b/tokio-sync/src/watch.rs @@ -104,6 +104,8 @@ pub struct Ref<'a, T: 'a> { pub mod error { //! Watch error types + use std::fmt; + /// Error produced when receiving a value fails. #[derive(Debug)] pub struct RecvError { @@ -115,6 +117,36 @@ pub mod error { pub struct SendError { pub(crate) inner: T, } + + // ===== impl RecvError ===== + + impl fmt::Display for RecvError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + use std::error::Error; + write!(fmt, "{}", self.description()) + } + } + + impl ::std::error::Error for RecvError { + fn description(&self) -> &str { + "channel closed" + } + } + + // ===== impl SendError ===== + + impl fmt::Display for SendError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + use std::error::Error; + write!(fmt, "{}", self.description()) + } + } + + impl ::std::error::Error for SendError { + fn description(&self) -> &str { + "channel closed" + } + } } #[derive(Debug)] diff --git a/tokio-sync/tests/errors.rs b/tokio-sync/tests/errors.rs index 0e56cc1a..129cbfc2 100644 --- a/tokio-sync/tests/errors.rs +++ b/tokio-sync/tests/errors.rs @@ -2,12 +2,12 @@ extern crate tokio_sync; -use tokio_sync::mpsc::error; - fn is_error() {} #[test] -fn error_bound() { +fn mpsc_error_bound() { + use tokio_sync::mpsc::error; + is_error::(); is_error::(); is_error::>(); @@ -15,3 +15,19 @@ fn error_bound() { is_error::(); is_error::>(); } + +#[test] +fn oneshot_error_bound() { + use tokio_sync::oneshot::error; + + is_error::(); + is_error::(); +} + +#[test] +fn watch_error_bound() { + use tokio_sync::watch::error; + + is_error::(); + is_error::>(); +} -- cgit v1.2.3