Skip to content
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

Never error #1492

Merged
merged 2 commits into from
Apr 16, 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
1 change: 1 addition & 0 deletions futures-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ io-compat = ["compat", "tokio-io"]
bench = []
nightly = ["futures-core-preview/nightly", "futures-sink-preview/nightly"]
cfg-target-has-atomic = ["futures-core-preview/cfg-target-has-atomic"]
never-type = []
alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc"]

[dependencies]
Expand Down
17 changes: 16 additions & 1 deletion futures-util/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ pub use self::inspect::Inspect;
mod unit_error;
pub use self::unit_error::UnitError;

#[cfg(feature = "never-type")]
mod never_error;
#[cfg(feature = "never-type")]
pub use self::never_error::NeverError;

// Implementation details
mod chain;
pub(crate) use self::chain::Chain;
Expand Down Expand Up @@ -530,13 +535,23 @@ pub trait FutureExt: Future {
Box::pin(self)
}

/// Turns a `Future` into a `TryFuture` with `Error = ()`.
/// Turns a [`Future<Output = T>`](Future) into a
/// [`TryFuture<Ok = T, Error = ()`>](futures_core::future::TryFuture).
fn unit_error(self) -> UnitError<Self>
where Self: Sized
{
UnitError::new(self)
}

#[cfg(feature = "never-type")]
/// Turns a [`Future<Output = T>`](Future) into a
/// [`TryFuture<Ok = T, Error = !`>](futures_core::future::TryFuture).
fn never_error(self) -> NeverError<Self>
where Self: Sized
{
NeverError::new(self)
}

/// A convenience for calling `Future::poll` on `Unpin` future types.
fn poll_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output>
where Self: Unpin + Sized
Expand Down
35 changes: 35 additions & 0 deletions futures-util/src/future/never_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use core::pin::Pin;
use futures_core::future::{FusedFuture, Future};
use futures_core::task::{self, Poll};
use pin_utils::unsafe_pinned;

/// Future for the [`never_error`](super::FutureExt::never_error) combinator.
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct NeverError<Fut> {
future: Fut,
}

impl<Fut> NeverError<Fut> {
unsafe_pinned!(future: Fut);

pub(super) fn new(future: Fut) -> NeverError<Fut> {
NeverError { future }
}
}

impl<Fut: Unpin> Unpin for NeverError<Fut> {}

impl<Fut: FusedFuture> FusedFuture for NeverError<Fut> {
fn is_terminated(&self) -> bool { self.future.is_terminated() }
}

impl<Fut, T> Future for NeverError<Fut>
where Fut: Future<Output = T>,
{
type Output = Result<T, !>;

fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Result<T, !>> {
self.future().poll(cx).map(Ok)
}
}
5 changes: 1 addition & 4 deletions futures-util/src/future/unit_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use futures_core::future::{FusedFuture, Future};
use futures_core::task::{Context, Poll};
use pin_utils::unsafe_pinned;

/// Future for the `unit_error` combinator, turning a `Future` into a `TryFuture`.
///
/// This is created by the `FutureExt::unit_error` method.
/// Future for the [`unit_error`](super::FutureExt::unit_error) combinator.
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct UnitError<Fut> {
Expand All @@ -15,7 +13,6 @@ pub struct UnitError<Fut> {
impl<Fut> UnitError<Fut> {
unsafe_pinned!(future: Fut);

/// Creates a new UnitError.
pub(super) fn new(future: Fut) -> UnitError<Fut> {
UnitError { future }
}
Expand Down
4 changes: 4 additions & 0 deletions futures-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![cfg_attr(feature = "alloc", feature(box_into_pin))]
#![cfg_attr(feature = "std", feature(async_await, await_macro))]
#![cfg_attr(feature = "cfg-target-has-atomic", feature(cfg_target_has_atomic))]
#![cfg_attr(feature = "never-type", feature(never_type))]

#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
Expand All @@ -14,6 +15,9 @@
#[cfg(all(feature = "cfg-target-has-atomic", not(feature = "nightly")))]
compile_error!("The `cfg-target-has-atomic` feature requires the `nightly` feature as an explicit opt-in to unstable features");

#[cfg(all(feature = "never-type", not(feature = "nightly")))]
compile_error!("The `never-type` feature requires the `nightly` feature as an explicit opt-in to unstable features");

#[cfg(feature = "alloc")]
extern crate alloc;

Expand Down
1 change: 1 addition & 0 deletions futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ default = ["std"]
compat = ["std", "futures-util-preview/compat"]
io-compat = ["compat", "futures-util-preview/io-compat"]
cfg-target-has-atomic = ["futures-core-preview/cfg-target-has-atomic", "futures-util-preview/cfg-target-has-atomic"]
never-type = ["futures-util-preview/never-type"]
alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc", "futures-util-preview/alloc"]
9 changes: 8 additions & 1 deletion futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#![feature(futures_api)]
#![cfg_attr(feature = "cfg-target-has-atomic", feature(cfg_target_has_atomic))]
#![cfg_attr(feature = "never-type", feature(never_type))]

#![cfg_attr(not(feature = "std"), no_std)]

Expand All @@ -33,6 +34,9 @@
#[cfg(all(feature = "cfg-target-has-atomic", not(feature = "nightly")))]
compile_error!("The `cfg-target-has-atomic` feature requires the `nightly` feature as an explicit opt-in to unstable features");

#[cfg(all(feature = "never-type", not(feature = "nightly")))]
compile_error!("The `never-type` feature requires the `nightly` feature as an explicit opt-in to unstable features");

#[doc(hidden)] pub use futures_util::core_reexport;

#[doc(hidden)] pub use futures_core::future::Future;
Expand Down Expand Up @@ -197,7 +201,7 @@ pub mod future {
OptionFuture,

FutureExt,
FlattenStream, Flatten, Fuse, Inspect, IntoStream, Map, Then,
FlattenStream, Flatten, Fuse, Inspect, IntoStream, Map, Then, UnitError,
};

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -230,6 +234,9 @@ pub mod future {
UnwrapOrElse,
};

#[cfg(feature = "never-type")]
pub use futures_util::future::NeverError;

#[cfg(feature = "alloc")]
pub use futures_util::try_future::{
try_join_all, TryJoinAll,
Expand Down