Skip to content

Commit 9be5f49

Browse files
committed
Add Never type versions of Finished and Failed
1 parent 3f63269 commit 9be5f49

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

Diff for: src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,22 @@ pub use poll::Poll;
177177
// Primitive futures
178178
mod done;
179179
mod empty;
180+
#[cfg(not(feature = "never"))]
180181
mod failed;
182+
#[cfg(not(feature = "never"))]
181183
mod finished;
182184
mod lazy;
183185
#[cfg(feature = "never")]
184186
mod never;
185187
pub use done::{done, Done};
186188
pub use empty::{empty, Empty};
189+
#[cfg(not(feature = "never"))]
187190
pub use failed::{failed, Failed};
191+
#[cfg(not(feature = "never"))]
188192
pub use finished::{finished, Finished};
189193
pub use lazy::{lazy, Lazy};
190194
#[cfg(feature = "never")]
191-
pub use never::{never, Never};
195+
pub use never::{never, Never, failed, Failed, finished, Finished};
192196

193197
// combinators
194198
mod and_then;

Diff for: src/never.rs

+66
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+
}

Diff for: tests/all.rs

+20
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ fn test_finished() {
107107
assert_done(|| failed(1), err(1));
108108
}
109109

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

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

0 commit comments

Comments
 (0)