Skip to content

Commit

Permalink
Add Never type versions of Finished and Failed
Browse files Browse the repository at this point in the history
  • Loading branch information
cramertj committed Sep 3, 2016
1 parent 9df248e commit 6e2ed76
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,22 @@ pub use poll::{Poll, Async};
// Primitive futures
mod done;
mod empty;
#[cfg(not(feature = "never"))]
mod failed;
#[cfg(not(feature = "never"))]
mod finished;
mod lazy;
#[cfg(feature = "never")]
mod never;
pub use done::{done, Done};
pub use empty::{empty, Empty};
#[cfg(not(feature = "never"))]
pub use failed::{failed, Failed};
#[cfg(not(feature = "never"))]
pub use finished::{finished, Finished};
pub use lazy::{lazy, Lazy};
#[cfg(feature = "never")]
pub use never::{never, Never};
pub use never::{never, Never, failed, Failed, finished, Finished};

// combinators
mod and_then;
Expand Down
66 changes: 66 additions & 0 deletions src/never.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use {Future, Poll};
/// A future which is never resolved.
///
/// This future can be created with the `empty` function.
#[derive(Copy, Clone)]
pub struct Never {}

/// Creates a future which never resolves, representing a computation that never
Expand All @@ -23,3 +24,68 @@ impl Future for Never {
Poll::NotReady
}
}

/// A future representing a finished but erroneous computation.
///
/// Created by the `failed` function.
pub struct Failed<E> {
e: Option<E>,
}

/// Creates a "leaf future" from an immediate value of a failed computation.
///
/// The returned future is similar to `done` where it will immediately run a
/// scheduled callback with the provided value.
///
/// # Examples
///
/// ```
/// use futures::*;
///
/// let future_of_err_1 = failed::<u32, u32>(1);
/// ```
pub fn failed<T, E>(e: E) -> impl Future<Item = T, Error = E> {
Failed { e: Some(e) }.map(|x| x)
}

impl<E> Future for Failed<E> {
type Item = !;
type Error = E;

fn poll(&mut self) -> Poll<!, E> {
Poll::Err(self.e.take().expect("cannot poll Failed twice"))
}
}

/// A future representing a finished successful computation.
///
/// Created by the `finished` function.
pub struct Finished<T> {
t: Option<T>,
}

/// Creates a "leaf future" from an immediate value of a finished and
/// successful computation.
///
/// The returned future is similar to `done` where it will immediately run a
/// scheduled callback with the provided value.
///
/// # Examples
///
/// ```
/// use futures::*;
///
/// let future_of_1 = finished::<u32, u32>(1);
/// ```
pub fn finished<T, E>(t: T) -> impl Future<Item = T, Error = E> {
Finished { t: Some(t) }.map_err(|x| x)
}

impl<T> Future for Finished<T> {
type Item = T;
type Error = !;

fn poll(&mut self) -> Poll<T, !> {
Poll::Ok(self.t.take().expect("cannot poll Finished twice"))
}
}
20 changes: 20 additions & 0 deletions tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ fn test_finished() {
assert_done(|| failed(1), err(1));
}

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

#[cfg(feature = "never")]
#[test]
fn flatten() {
fn finished<T: Send + 'static>(a: T) -> impl Future<Item = T, Error = u32> {
futures::finished(a)
}
fn failed<E: Send + 'static>(b: E) -> impl Future<Item = i32, Error = E> {
futures::failed(b)
}

assert_done(|| finished(finished(1)).flatten(), ok(1));
assert_done(|| finished(failed(1)).flatten(), err(1));
assert_done(|| failed(1u32).map(finished).flatten(), err(1));
assert_done(|| futures::finished::<_, u8>(futures::finished::<_, u32>(1))
.flatten(), ok(1));
assert_empty(|| finished(empty::<i32, u32>()).flatten());
assert_empty(|| empty::<i32, u32>().map(finished).flatten());
}

#[test]
fn smoke_oneshot() {
assert_done(|| {
Expand Down

0 comments on commit 6e2ed76

Please sign in to comment.