Skip to content

Commit

Permalink
Replace DrainError and VecSinkError with Never
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jun 19, 2019
1 parent cd6983f commit 91f514c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 41 deletions.
3 changes: 3 additions & 0 deletions futures-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ pub mod stream;

pub mod task;
#[doc(hidden)] pub use self::task::Poll;

pub mod never;
#[doc(hidden)] pub use self::never::Never;
26 changes: 26 additions & 0 deletions futures-core/src/never.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use core::fmt;

/// A type with no possible values.
///
/// This is used to indicate values which can never be created, such as the
/// error type of infallible futures.
///
/// This type is a stable equivalent to the `!` type from `std`.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum Never {}

impl Never {
/// Convert the `Never` type into any other type.
pub fn into_any<T>(self) -> T {
match self {}
}
}

impl fmt::Display for Never {
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {}
}
}

#[cfg(feature = "std")]
impl std::error::Error for Never {}
19 changes: 6 additions & 13 deletions futures-sink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,10 @@ mod channel_impls;
#[cfg(feature = "alloc")]
mod if_alloc {
use super::*;
use futures_core::never::Never;

/// The error type for `Vec` and `VecDequeue` when used as `Sink`s.
/// Values of this type can never be created.
#[derive(Copy, Clone, Debug)]
pub enum VecSinkError {}

impl<T> Sink<T> for ::alloc::vec::Vec<T> {
type SinkError = VecSinkError;
impl<T> Sink<T> for alloc::vec::Vec<T> {
type SinkError = Never;

fn poll_ready(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> {
Poll::Ready(Ok(()))
Expand All @@ -191,8 +187,8 @@ mod if_alloc {
}
}

impl<T> Sink<T> for ::alloc::collections::VecDeque<T> {
type SinkError = VecSinkError;
impl<T> Sink<T> for alloc::collections::VecDeque<T> {
type SinkError = Never;

fn poll_ready(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> {
Poll::Ready(Ok(()))
Expand All @@ -213,7 +209,7 @@ mod if_alloc {
}
}

impl<S: ?Sized + Sink<Item> + Unpin, Item> Sink<Item> for ::alloc::boxed::Box<S> {
impl<S: ?Sized + Sink<Item> + Unpin, Item> Sink<Item> for alloc::boxed::Box<S> {
type SinkError = S::SinkError;

fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> {
Expand All @@ -233,6 +229,3 @@ mod if_alloc {
}
}
}

#[cfg(feature = "alloc")]
pub use self::if_alloc::*;
29 changes: 3 additions & 26 deletions futures-util/src/sink/drain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;
use core::marker::PhantomData;
use core::pin::Pin;
use futures_core::task::{Context, Poll};
use futures_core::never::Never;
use futures_sink::Sink;

/// Sink for the [`drain`] function.
Expand All @@ -11,11 +11,6 @@ pub struct Drain<T> {
marker: PhantomData<T>,
}

/// The error type for the [`Drain`] sink.
#[derive(Debug)]
pub enum DrainError {
}

/// Create a sink that will just discard all items given to it.
///
/// Similar to [`io::Sink`](::std::io::Sink).
Expand All @@ -29,14 +24,14 @@ pub enum DrainError {
///
/// let mut drain = sink::drain();
/// drain.send(5).await?;
/// # Ok::<(), futures::sink::DrainError>(()) }).unwrap();
/// # Ok::<(), futures::never::Never>(()) }).unwrap();
/// ```
pub fn drain<T>() -> Drain<T> {
Drain { marker: PhantomData }
}

impl<T> Sink<T> for Drain<T> {
type SinkError = DrainError;
type SinkError = Never;

fn poll_ready(
self: Pin<&mut Self>,
Expand Down Expand Up @@ -66,21 +61,3 @@ impl<T> Sink<T> for Drain<T> {
Poll::Ready(Ok(()))
}
}

impl fmt::Display for DrainError {
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for DrainError {}

impl DrainError {
/// Convert this drain error into any type
pub fn into_any<T>(self) -> T {
match self {
}
}
}
2 changes: 1 addition & 1 deletion futures-util/src/sink/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod close;
pub use self::close::Close;

mod drain;
pub use self::drain::{drain, Drain, DrainError};
pub use self::drain::{drain, Drain};

mod fanout;
pub use self::fanout::Fanout;
Expand Down
12 changes: 11 additions & 1 deletion futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ compile_error!("The `never-type` feature requires the `nightly` feature as an ex

#[doc(hidden)] pub use futures_core::task::Poll;

#[doc(hidden)] pub use futures_core::never::Never;

// Macro reexports
pub use futures_util::ready; // Readiness propagation
#[cfg(feature = "async-await")]
Expand Down Expand Up @@ -366,7 +368,7 @@ pub mod sink {

pub use futures_util::sink::{
Close, Flush, Send, SendAll, SinkErrInto, SinkMapErr, With,
SinkExt, Fanout, Drain, DrainError, drain,
SinkExt, Fanout, Drain, drain,
WithFlatMap,
};

Expand Down Expand Up @@ -500,6 +502,14 @@ pub mod task {
pub use futures_util::task::AtomicWaker;
}

pub mod never {
//! This module contains the `Never` type.
//!
//! Values of this type can never be created and will never exist.

pub use futures_core::never::Never;
}

// `select!` re-export --------------------------------------

#[cfg(feature = "async-await")]
Expand Down

0 comments on commit 91f514c

Please sign in to comment.