From 7bdfb8ad6ee2fb2e69cf0845f395ad7d9f8e8c85 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Mon, 2 Sep 2019 09:42:04 +0900 Subject: [PATCH] Clean up error types --- futures-channel/src/mpsc/mod.rs | 12 +++++------- futures-core/src/task/spawn.rs | 4 ++-- futures-executor/src/enter.rs | 23 ++++++++++++++++++----- futures-util/src/future/abortable.rs | 10 ++++++++++ futures-util/src/lock/bilock.rs | 6 +----- futures-util/src/stream/split.rs | 16 ++++++---------- 6 files changed, 42 insertions(+), 29 deletions(-) diff --git a/futures-channel/src/mpsc/mod.rs b/futures-channel/src/mpsc/mod.rs index 6409ebc710..fde963575d 100644 --- a/futures-channel/src/mpsc/mod.rs +++ b/futures-channel/src/mpsc/mod.rs @@ -81,8 +81,6 @@ use futures_core::stream::{FusedStream, Stream}; use futures_core::task::{Context, Poll, Waker}; use futures_core::task::__internal::AtomicWaker; -use std::any::Any; -use std::error::Error; use std::fmt; use std::pin::Pin; use std::sync::{Arc, Mutex}; @@ -166,7 +164,7 @@ enum SendErrorKind { /// The error type returned from [`try_next`](Receiver::try_next). pub struct TryRecvError { - _inner: (), + _priv: (), } impl fmt::Display for SendError { @@ -179,7 +177,7 @@ impl fmt::Display for SendError { } } -impl Error for SendError {} +impl std::error::Error for SendError {} impl SendError { /// Returns true if this error is a result of the channel being full. @@ -217,7 +215,7 @@ impl fmt::Display for TrySendError { } } -impl Error for TrySendError {} +impl std::error::Error for TrySendError {} impl TrySendError { /// Returns true if this error is a result of the channel being full. @@ -254,7 +252,7 @@ impl fmt::Display for TryRecvError { } } -impl Error for TryRecvError {} +impl std::error::Error for TryRecvError {} #[derive(Debug)] struct Inner { @@ -834,7 +832,7 @@ impl Receiver { Poll::Ready(msg) => { Ok(msg) }, - Poll::Pending => Err(TryRecvError { _inner: () }), + Poll::Pending => Err(TryRecvError { _priv: () }), } } diff --git a/futures-core/src/task/spawn.rs b/futures-core/src/task/spawn.rs index 6e66e067df..895e81b598 100644 --- a/futures-core/src/task/spawn.rs +++ b/futures-core/src/task/spawn.rs @@ -55,7 +55,7 @@ pub trait LocalSpawn { /// An error that occurred during spawning. pub struct SpawnError { - _hidden: (), + _priv: (), } impl fmt::Debug for SpawnError { @@ -78,7 +78,7 @@ impl std::error::Error for SpawnError {} impl SpawnError { /// Spawning failed because the executor has been shut down. pub fn shutdown() -> Self { - Self { _hidden: () } + Self { _priv: () } } /// Check whether spawning failed to the executor being shut down. diff --git a/futures-executor/src/enter.rs b/futures-executor/src/enter.rs index c9c0a451a6..5895a9efb6 100644 --- a/futures-executor/src/enter.rs +++ b/futures-executor/src/enter.rs @@ -7,16 +7,29 @@ thread_local!(static ENTERED: Cell = Cell::new(false)); /// /// For more details, see [`enter` documentation](enter()). pub struct Enter { - _a: () + _priv: (), } /// An error returned by `enter` if an execution scope has already been /// entered. -#[derive(Debug)] pub struct EnterError { - _a: (), + _priv: (), } +impl fmt::Debug for EnterError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("EnterError").finish() + } +} + +impl fmt::Display for EnterError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "an execution scope has already been entered") + } +} + +impl std::error::Error for EnterError {} + /// Marks the current thread as being within the dynamic extent of an /// executor. /// @@ -42,11 +55,11 @@ pub struct EnterError { pub fn enter() -> Result { ENTERED.with(|c| { if c.get() { - Err(EnterError { _a: () }) + Err(EnterError { _priv: () }) } else { c.set(true); - Ok(Enter { _a: () }) + Ok(Enter { _priv: () }) } }) } diff --git a/futures-util/src/future/abortable.rs b/futures-util/src/future/abortable.rs index 3c57cfc01d..281cf6b481 100644 --- a/futures-util/src/future/abortable.rs +++ b/futures-util/src/future/abortable.rs @@ -2,6 +2,7 @@ use crate::task::AtomicWaker; use futures_core::future::Future; use futures_core::task::{Context, Poll}; use pin_utils::unsafe_pinned; +use core::fmt; use core::pin::Pin; use core::sync::atomic::{AtomicBool, Ordering}; use alloc::sync::Arc; @@ -124,6 +125,15 @@ pub fn abortable(future: Fut) -> (Abortable, AbortHandle) #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub struct Aborted; +impl fmt::Display for Aborted { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "`Abortable` future has been aborted") + } +} + +#[cfg(feature = "std")] +impl std::error::Error for Aborted {} + impl Future for Abortable where Fut: Future { type Output = Result; diff --git a/futures-util/src/lock/bilock.rs b/futures-util/src/lock/bilock.rs index 8ea809d07a..ab590bb543 100644 --- a/futures-util/src/lock/bilock.rs +++ b/futures-util/src/lock/bilock.rs @@ -11,10 +11,6 @@ use core::sync::atomic::AtomicUsize; use core::sync::atomic::Ordering::SeqCst; use alloc::boxed::Box; use alloc::sync::Arc; -#[cfg(feature = "std")] -use std::any::Any; -#[cfg(feature = "std")] -use std::error::Error; /// A type of futures-powered synchronization primitive which is a mutex between /// two possible owners. @@ -218,7 +214,7 @@ impl fmt::Display for ReuniteError { } #[cfg(feature = "std")] -impl Error for ReuniteError {} +impl std::error::Error for ReuniteError {} /// Returned RAII guard from the `poll_lock` method. /// diff --git a/futures-util/src/stream/split.rs b/futures-util/src/stream/split.rs index c6151fbf79..28d7aa71a0 100644 --- a/futures-util/src/stream/split.rs +++ b/futures-util/src/stream/split.rs @@ -3,10 +3,6 @@ use futures_core::task::{Context, Poll}; use futures_sink::Sink; use core::fmt; use core::pin::Pin; -#[cfg(feature = "std")] -use std::any::Any; -#[cfg(feature = "std")] -use std::error::Error; use crate::lock::BiLock; @@ -47,12 +43,12 @@ fn SplitSink, Item>(lock: BiLock) -> SplitSink { /// A `Sink` part of the split pair #[derive(Debug)] #[must_use = "sinks do nothing unless polled"] -pub struct SplitSink, Item> { +pub struct SplitSink { lock: BiLock, slot: Option, } -impl, Item> Unpin for SplitSink {} +impl Unpin for SplitSink {} impl + Unpin, Item> SplitSink { /// Attempts to put the two "halves" of a split `Stream + Sink` back @@ -112,9 +108,9 @@ pub(super) fn split, Item>(s: S) -> (SplitSink, /// Error indicating a `SplitSink` and `SplitStream` were not two halves /// of a `Stream + Split`, and thus could not be `reunite`d. -pub struct ReuniteError, Item>(pub SplitSink, pub SplitStream); +pub struct ReuniteError(pub SplitSink, pub SplitStream); -impl, Item> fmt::Debug for ReuniteError { +impl fmt::Debug for ReuniteError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("ReuniteError") .field(&"...") @@ -122,11 +118,11 @@ impl, Item> fmt::Debug for ReuniteError { } } -impl, Item> fmt::Display for ReuniteError { +impl fmt::Display for ReuniteError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "tried to reunite a SplitStream and SplitSink that don't form a pair") } } #[cfg(feature = "std")] -impl, Item> Error for ReuniteError {} +impl std::error::Error for ReuniteError {}