Skip to content

Commit 3f63269

Browse files
committed
Add feature-gated Never future
1 parent a118130 commit 3f63269

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ log = { version = "0.3", default-features = true }
3434
scoped-tls = { version = "0.1", optional = true }
3535

3636
[features]
37+
never = []
3738
use_std = ["scoped-tls"]
3839
default = ["use_std"]

src/lib.rs

+5
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")]
@@ -179,11 +180,15 @@ mod empty;
179180
mod failed;
180181
mod finished;
181182
mod lazy;
183+
#[cfg(feature = "never")]
184+
mod never;
182185
pub use done::{done, Done};
183186
pub use empty::{empty, Empty};
184187
pub use failed::{failed, Failed};
185188
pub use finished::{finished, Finished};
186189
pub use lazy::{lazy, Lazy};
190+
#[cfg(feature = "never")]
191+
pub use never::{never, Never};
187192

188193
// combinators
189194
mod and_then;

src/never.rs

+25
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

+21
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};
@@ -80,6 +82,25 @@ fn test_empty() {
8082
assert_empty(|| empty().then(|a| a));
8183
}
8284

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

0 commit comments

Comments
 (0)