Skip to content

Commit 9df248e

Browse files
committed
Add feature-gated Never future
1 parent 6de5151 commit 9df248e

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ log = { version = "0.3", default-features = false }
1818

1919
[features]
2020
use_std = []
21+
never = []
2122
default = ["use_std"]

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
152152
#![no_std]
153153
#![deny(missing_docs)]
154+
#![cfg_attr(feature = "never", feature(never_type, conservative_impl_trait))]
154155

155156
#[macro_use]
156157
#[cfg(feature = "use_std")]
@@ -176,11 +177,15 @@ mod empty;
176177
mod failed;
177178
mod finished;
178179
mod lazy;
180+
#[cfg(feature = "never")]
181+
mod never;
179182
pub use done::{done, Done};
180183
pub use empty::{empty, Empty};
181184
pub use failed::{failed, Failed};
182185
pub use finished::{finished, Finished};
183186
pub use lazy::{lazy, Lazy};
187+
#[cfg(feature = "never")]
188+
pub use never::{never, Never};
184189

185190
// combinators
186191
mod and_then;

src/never.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use {Future, Poll};
2+
3+
/// A future which is never resolved.
4+
///
5+
/// This future can be created with the `empty` function.
6+
pub struct Never {}
7+
8+
/// Creates a future which never resolves, representing a computation that never
9+
/// finishes.
10+
///
11+
/// The returned future will never resolve with a success but is still
12+
/// susceptible to cancellation. That is, if a callback is scheduled on the
13+
/// returned future, it is only run once the future is dropped (canceled).
14+
pub fn never<T, E>() -> impl Future<Item = T, Error = E> + Send + 'static {
15+
(Never {}).map(|x| x).map_err(|x| x)
16+
}
17+
18+
impl Future for Never {
19+
type Item = !;
20+
type Error = !;
21+
22+
fn poll(&mut self) -> Poll<!, !> {
23+
Poll::NotReady
24+
}
25+
}

tests/all.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(feature = "never", feature(conservative_impl_trait))]
2+
13
extern crate futures;
24

35
use std::sync::mpsc::{channel, TryRecvError};
@@ -79,6 +81,25 @@ fn test_empty() {
7981
assert_empty(|| empty().then(|a| a));
8082
}
8183

84+
#[cfg(feature = "never")]
85+
#[test]
86+
fn test_never() {
87+
fn never() -> impl Future<Item = i32, Error = u32> { futures::never() }
88+
89+
assert_empty(|| never());
90+
assert_empty(|| never().select(never()));
91+
assert_empty(|| never().join(never()));
92+
assert_empty(|| never().join(f_ok(1)));
93+
assert_empty(|| f_ok(1).join(never()));
94+
assert_empty(|| never().or_else(move |_| never()));
95+
assert_empty(|| never().and_then(move |_| never()));
96+
assert_empty(|| f_err(1).or_else(move |_| never()));
97+
assert_empty(|| f_ok(1).and_then(move |_| never()));
98+
assert_empty(|| never().map(|a| a + 1));
99+
assert_empty(|| never().map_err(|a| a + 1));
100+
assert_empty(|| never().then(|a| a));
101+
}
102+
82103
#[test]
83104
fn test_finished() {
84105
assert_done(|| finished(1), ok(1));

0 commit comments

Comments
 (0)