Skip to content

Commit

Permalink
Merge pull request rust-lang#288 from sgrif/sg-unify-result-types
Browse files Browse the repository at this point in the history
Unify `future::Ok`, `future::Err`, and `feature::FutureResult`
  • Loading branch information
alexcrichton authored Dec 13, 2016
2 parents 0b76968 + 0b9e644 commit 3e4788d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 94 deletions.
40 changes: 0 additions & 40 deletions src/future/err.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/future/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ enum _Lazy<F, R> {
///
/// let a = lazy(|| ok::<u32, u32>(1));
///
/// let b = lazy(|| -> Ok<u32, u32> {
/// let b = lazy(|| -> FutureResult<u32, u32> {
/// panic!("oh no!")
/// });
/// drop(b); // closure is never run
Expand Down
20 changes: 11 additions & 9 deletions src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@ use core::result;

// Primitive futures
mod empty;
#[path = "err.rs"] // remove when deprecated reexports are gone
mod err_;
mod lazy;
#[path = "ok.rs"]
mod ok_;
mod poll_fn;
#[path = "result.rs"]
mod result_;
pub use self::empty::{empty, Empty};
pub use self::err_::{err, Err};
pub use self::lazy::{lazy, Lazy};
pub use self::ok_::{ok, Ok};
pub use self::poll_fn::{poll_fn, PollFn};
pub use self::result_::{result, FutureResult};
pub use self::result_::{result, ok, err, FutureResult};

#[doc(hidden)]
#[deprecated(since = "0.1.4", note = "use `ok` instead")]
Expand All @@ -34,6 +28,14 @@ pub use self::{err as failed, Err as Failed};
#[deprecated(since = "0.1.4", note = "use `result` instead")]
#[cfg(feature = "with-deprecated")]
pub use self::{result as done, FutureResult as Done};
#[doc(hidden)]
#[deprecated(since = "0.1.7", note = "use `FutureResult` instead")]
#[cfg(feature = "with-deprecated")]
pub use self::{FutureResult as Ok};
#[doc(hidden)]
#[deprecated(since = "0.1.7", note = "use `FutureResult` instead")]
#[cfg(feature = "with-deprecated")]
pub use self::{FutureResult as Err};

// combinators
mod and_then;
Expand Down Expand Up @@ -402,7 +404,7 @@ pub trait Future {
/// });
///
/// let future_of_err_1 = err::<u32, u32>(1);
/// future_of_err_1.and_then(|_| -> Ok<u32, u32> {
/// future_of_err_1.and_then(|_| -> FutureResult<u32, u32> {
/// panic!("should not be called in case of an error");
/// });
/// ```
Expand Down Expand Up @@ -442,7 +444,7 @@ pub trait Future {
/// });
///
/// let future_of_1 = ok::<u32, u32>(1);
/// future_of_1.or_else(|_| -> Ok<u32, u32> {
/// future_of_1.or_else(|_| -> FutureResult<u32, u32> {
/// panic!("should not be called in case of success");
/// });
/// ```
Expand Down
42 changes: 0 additions & 42 deletions src/future/ok.rs

This file was deleted.

33 changes: 33 additions & 0 deletions src/future/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,39 @@ pub fn result<T, E>(r: result::Result<T, E>) -> FutureResult<T, E> {
FutureResult { inner: Some(r) }
}

/// 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::future::*;
///
/// let future_of_1 = ok::<u32, u32>(1);
/// ```
pub fn ok<T, E>(t: T) -> FutureResult<T, E> {
result(Ok(t))
}

/// 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::future::*;
///
/// let future_of_err_1 = err::<u32, u32>(1);
/// ```
pub fn err<T, E>(e: E) -> FutureResult<T, E> {
result(Err(e))
}

impl<T, E> Future for FutureResult<T, E> {
type Item = T;
type Error = E;
Expand Down
4 changes: 2 additions & 2 deletions tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ fn test_ok() {

#[test]
fn flatten() {
fn ok<T: Send + 'static>(a: T) -> Ok<T, u32> {
fn ok<T: Send + 'static>(a: T) -> FutureResult<T, u32> {
future::ok(a)
}
fn err<E: Send + 'static>(b: E) -> Err<i32, E> {
fn err<E: Send + 'static>(b: E) -> FutureResult<i32, E> {
future::err(b)
}

Expand Down

0 comments on commit 3e4788d

Please sign in to comment.