Skip to content

Commit edd1e4d

Browse files
committed
Change Never to use ! instead of PhantomData
1 parent 9f9a78e commit edd1e4d

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ before_script:
2121
- pip install git+https://github.com/euclio/travis-cargo@kcov --user && export PATH=$HOME/.local/bin:$PATH
2222
script:
2323
- cargo test
24+
- cargo test --features "nightly"
2425
- cargo test --manifest-path futures-io/Cargo.toml
2526
- cargo test --manifest-path futures-iobuf/Cargo.toml
2627
- cargo test --manifest-path futures-cpupool/Cargo.toml

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ An implementation of futures and streams featuring zero allocations,
1313
composability, and iterator-like interfaces.
1414
"""
1515

16+
[features]
17+
nightly = []
18+
1619
[workspace]
1720
members = [
1821
"futures-tls",

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
//! [README]: https://github.com/alexcrichton/futures-rs#futures-rs
151151
152152
#![deny(missing_docs)]
153+
#![cfg_attr(feature = "nightly", feature(never_type, conservative_impl_trait))]
153154

154155
#[macro_use]
155156
extern crate log;

src/never.rs

+37
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
#[cfg(not(feature = "nightly"))]
12
use std::marker;
23

34
use {Future, Task, Poll};
45

56
/// A future which is never resolved.
67
///
78
/// This future can be created with the `empty` function.
9+
#[cfg(feature = "nightly")]
10+
pub struct Never {}
11+
12+
/// A future which is never resolved.
13+
///
14+
/// This future can be created with the `empty` function.
15+
#[cfg(not(feature = "nightly"))]
816
pub struct Never<T, E>
917
where T: 'static,
1018
E: 'static,
@@ -18,10 +26,39 @@ pub struct Never<T, E>
1826
/// The returned future will never resolve with a success but is still
1927
/// susceptible to cancellation. That is, if a callback is scheduled on the
2028
/// returned future, it is only run once the future is dropped (canceled).
29+
#[cfg(feature = "nightly")]
30+
pub fn never<T, E>() -> impl Future<Item = T, Error = E> + Send + 'static
31+
where T: 'static,
32+
E: 'static,
33+
{
34+
(Never {})
35+
.map(|x| x)
36+
.map_err(|x| x)
37+
}
38+
39+
/// Creates a future which never resolves, representing a computation that never
40+
/// finishes.
41+
///
42+
/// The returned future will never resolve with a success but is still
43+
/// susceptible to cancellation. That is, if a callback is scheduled on the
44+
/// returned future, it is only run once the future is dropped (canceled).
45+
#[cfg(not(feature = "nightly"))]
2146
pub fn never<T: 'static, E: 'static>() -> Never<T, E> {
2247
Never { _data: marker::PhantomData }
2348
}
2449

50+
#[cfg(feature = "nightly")]
51+
impl Future for Never {
52+
type Item = !;
53+
type Error = !;
54+
55+
fn poll(&mut self, _: &mut Task) -> Poll<!, !> {
56+
Poll::NotReady
57+
}
58+
59+
fn schedule(&mut self, _task: &mut Task) {}
60+
}
61+
#[cfg(not(feature = "nightly"))]
2562
impl<T, E> Future for Never<T, E>
2663
where T: 'static,
2764
E: 'static,

tests/all.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(feature = "nightly", feature(never_type, conservative_impl_trait))]
2+
13
extern crate futures;
24

35
use std::sync::mpsc::{channel, TryRecvError};
@@ -63,6 +65,10 @@ fn result_smoke() {
6365

6466
#[test]
6567
fn test_never() {
68+
#[cfg(feature = "nightly")]
69+
fn never() -> impl Future<Item = i32, Error = u32> + Send + 'static { futures::never() }
70+
71+
#[cfg(not(feature = "nightly"))]
6672
fn never() -> Never<i32, u32> { futures::never() }
6773

6874
assert_empty(|| never());

0 commit comments

Comments
 (0)