Skip to content

Clean up error types #1847

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

Merged
merged 2 commits into from
Sep 3, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions futures-channel/src/mpsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -217,7 +215,7 @@ impl<T> fmt::Display for TrySendError<T> {
}
}

impl<T: Any> Error for TrySendError<T> {}
impl<T: core::any::Any> std::error::Error for TrySendError<T> {}

impl<T> TrySendError<T> {
/// Returns true if this error is a result of the channel being full.
Expand Down Expand Up @@ -254,7 +252,7 @@ impl fmt::Display for TryRecvError {
}
}

impl Error for TryRecvError {}
impl std::error::Error for TryRecvError {}

#[derive(Debug)]
struct Inner<T> {
Expand Down Expand Up @@ -834,7 +832,7 @@ impl<T> Receiver<T> {
Poll::Ready(msg) => {
Ok(msg)
},
Poll::Pending => Err(TryRecvError { _inner: () }),
Poll::Pending => Err(TryRecvError { _priv: () }),
}
}

Expand Down
4 changes: 2 additions & 2 deletions futures-core/src/task/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub trait LocalSpawn {

/// An error that occurred during spawning.
pub struct SpawnError {
_hidden: (),
_priv: (),
}

impl fmt::Debug for SpawnError {
Expand All @@ -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.
Expand Down
23 changes: 18 additions & 5 deletions futures-executor/src/enter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,29 @@ thread_local!(static ENTERED: Cell<bool> = 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.
///
Expand All @@ -42,11 +55,11 @@ pub struct EnterError {
pub fn enter() -> Result<Enter, EnterError> {
ENTERED.with(|c| {
if c.get() {
Err(EnterError { _a: () })
Err(EnterError { _priv: () })
} else {
c.set(true);

Ok(Enter { _a: () })
Ok(Enter { _priv: () })
}
})
}
Expand Down
10 changes: 10 additions & 0 deletions futures-util/src/future/abortable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -124,6 +125,15 @@ pub fn abortable<Fut>(future: Fut) -> (Abortable<Fut>, 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<Fut> Future for Abortable<Fut> where Fut: Future {
type Output = Result<Fut::Output, Aborted>;

Expand Down
6 changes: 1 addition & 5 deletions futures-util/src/lock/bilock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -218,7 +214,7 @@ impl<T> fmt::Display for ReuniteError<T> {
}

#[cfg(feature = "std")]
impl<T: Any> Error for ReuniteError<T> {}
impl<T: core::any::Any> std::error::Error for ReuniteError<T> {}

/// Returned RAII guard from the `poll_lock` method.
///
Expand Down
16 changes: 6 additions & 10 deletions futures-util/src/stream/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -47,12 +43,12 @@ fn SplitSink<S: Sink<Item>, Item>(lock: BiLock<S>) -> SplitSink<S, Item> {
/// A `Sink` part of the split pair
#[derive(Debug)]
#[must_use = "sinks do nothing unless polled"]
pub struct SplitSink<S: Sink<Item>, Item> {
pub struct SplitSink<S, Item> {
lock: BiLock<S>,
slot: Option<Item>,
}

impl<S: Sink<Item>, Item> Unpin for SplitSink<S, Item> {}
impl<S, Item> Unpin for SplitSink<S, Item> {}

impl<S: Sink<Item> + Unpin, Item> SplitSink<S, Item> {
/// Attempts to put the two "halves" of a split `Stream + Sink` back
Expand Down Expand Up @@ -112,21 +108,21 @@ pub(super) fn split<S: Stream + Sink<Item>, Item>(s: S) -> (SplitSink<S, Item>,

/// Error indicating a `SplitSink<S>` and `SplitStream<S>` were not two halves
/// of a `Stream + Split`, and thus could not be `reunite`d.
pub struct ReuniteError<T: Sink<Item>, Item>(pub SplitSink<T, Item>, pub SplitStream<T>);
pub struct ReuniteError<T, Item>(pub SplitSink<T, Item>, pub SplitStream<T>);

impl<T: Sink<Item>, Item> fmt::Debug for ReuniteError<T, Item> {
impl<T, Item> fmt::Debug for ReuniteError<T, Item> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("ReuniteError")
.field(&"...")
.finish()
}
}

impl<T: Sink<Item>, Item> fmt::Display for ReuniteError<T, Item> {
impl<T, Item> fmt::Display for ReuniteError<T, Item> {
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<T: Any + Sink<Item>, Item> Error for ReuniteError<T, Item> {}
impl<T: core::any::Any, Item> std::error::Error for ReuniteError<T, Item> {}