Skip to content

Commit 6e2ed76

Browse files
committed
Add Never type versions of Finished and Failed
1 parent 9df248e commit 6e2ed76

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,22 @@ pub use poll::{Poll, Async};
174174
// Primitive futures
175175
mod done;
176176
mod empty;
177+
#[cfg(not(feature = "never"))]
177178
mod failed;
179+
#[cfg(not(feature = "never"))]
178180
mod finished;
179181
mod lazy;
180182
#[cfg(feature = "never")]
181183
mod never;
182184
pub use done::{done, Done};
183185
pub use empty::{empty, Empty};
186+
#[cfg(not(feature = "never"))]
184187
pub use failed::{failed, Failed};
188+
#[cfg(not(feature = "never"))]
185189
pub use finished::{finished, Finished};
186190
pub use lazy::{lazy, Lazy};
187191
#[cfg(feature = "never")]
188-
pub use never::{never, Never};
192+
pub use never::{never, Never, failed, Failed, finished, Finished};
189193

190194
// combinators
191195
mod and_then;

src/never.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use {Future, Poll};
33
/// A future which is never resolved.
44
///
55
/// This future can be created with the `empty` function.
6+
#[derive(Copy, Clone)]
67
pub struct Never {}
78

89
/// Creates a future which never resolves, representing a computation that never
@@ -23,3 +24,68 @@ impl Future for Never {
2324
Poll::NotReady
2425
}
2526
}
27+
28+
/// A future representing a finished but erroneous computation.
29+
///
30+
/// Created by the `failed` function.
31+
pub struct Failed<E> {
32+
e: Option<E>,
33+
}
34+
35+
/// Creates a "leaf future" from an immediate value of a failed computation.
36+
///
37+
/// The returned future is similar to `done` where it will immediately run a
38+
/// scheduled callback with the provided value.
39+
///
40+
/// # Examples
41+
///
42+
/// ```
43+
/// use futures::*;
44+
///
45+
/// let future_of_err_1 = failed::<u32, u32>(1);
46+
/// ```
47+
pub fn failed<T, E>(e: E) -> impl Future<Item = T, Error = E> {
48+
Failed { e: Some(e) }.map(|x| x)
49+
}
50+
51+
impl<E> Future for Failed<E> {
52+
type Item = !;
53+
type Error = E;
54+
55+
fn poll(&mut self) -> Poll<!, E> {
56+
Poll::Err(self.e.take().expect("cannot poll Failed twice"))
57+
}
58+
}
59+
60+
/// A future representing a finished successful computation.
61+
///
62+
/// Created by the `finished` function.
63+
pub struct Finished<T> {
64+
t: Option<T>,
65+
}
66+
67+
/// Creates a "leaf future" from an immediate value of a finished and
68+
/// successful computation.
69+
///
70+
/// The returned future is similar to `done` where it will immediately run a
71+
/// scheduled callback with the provided value.
72+
///
73+
/// # Examples
74+
///
75+
/// ```
76+
/// use futures::*;
77+
///
78+
/// let future_of_1 = finished::<u32, u32>(1);
79+
/// ```
80+
pub fn finished<T, E>(t: T) -> impl Future<Item = T, Error = E> {
81+
Finished { t: Some(t) }.map_err(|x| x)
82+
}
83+
84+
impl<T> Future for Finished<T> {
85+
type Item = T;
86+
type Error = !;
87+
88+
fn poll(&mut self) -> Poll<T, !> {
89+
Poll::Ok(self.t.take().expect("cannot poll Finished twice"))
90+
}
91+
}

tests/all.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ fn test_finished() {
106106
assert_done(|| failed(1), err(1));
107107
}
108108

109+
#[cfg(not(feature = "never"))]
109110
#[test]
110111
fn flatten() {
111112
fn finished<T: Send + 'static>(a: T) -> Finished<T, u32> {
@@ -124,6 +125,25 @@ fn flatten() {
124125
assert_empty(|| empty::<i32, u32>().map(finished).flatten());
125126
}
126127

128+
#[cfg(feature = "never")]
129+
#[test]
130+
fn flatten() {
131+
fn finished<T: Send + 'static>(a: T) -> impl Future<Item = T, Error = u32> {
132+
futures::finished(a)
133+
}
134+
fn failed<E: Send + 'static>(b: E) -> impl Future<Item = i32, Error = E> {
135+
futures::failed(b)
136+
}
137+
138+
assert_done(|| finished(finished(1)).flatten(), ok(1));
139+
assert_done(|| finished(failed(1)).flatten(), err(1));
140+
assert_done(|| failed(1u32).map(finished).flatten(), err(1));
141+
assert_done(|| futures::finished::<_, u8>(futures::finished::<_, u32>(1))
142+
.flatten(), ok(1));
143+
assert_empty(|| finished(empty::<i32, u32>()).flatten());
144+
assert_empty(|| empty::<i32, u32>().map(finished).flatten());
145+
}
146+
127147
#[test]
128148
fn smoke_oneshot() {
129149
assert_done(|| {

0 commit comments

Comments
 (0)