futures_core/future/
result.rs1use {Future, IntoFuture, Poll, Async};
2use task;
3
4#[derive(Debug, Clone)]
9#[must_use = "futures do nothing unless polled"]
10pub struct FutureResult<T, E> {
11 inner: Option<Result<T, E>>,
12}
13
14impl<T, E> IntoFuture for Result<T, E> {
15 type Future = FutureResult<T, E>;
16 type Item = T;
17 type Error = E;
18
19 fn into_future(self) -> Self::Future {
20 result(self)
21 }
22}
23
24pub fn result<T, E>(r: Result<T, E>) -> FutureResult<T, E> {
35 FutureResult { inner: Some(r) }
36}
37
38pub fn ok<T, E>(t: T) -> FutureResult<T, E> {
48 result(Ok(t))
49}
50
51pub fn err<T, E>(e: E) -> FutureResult<T, E> {
61 result(Err(e))
62}
63
64impl<T, E> Future for FutureResult<T, E> {
65 type Item = T;
66 type Error = E;
67
68 fn poll(&mut self, _: &mut task::Context) -> Poll<T, E> {
69 self.inner.take().expect("cannot poll Result twice").map(Async::Ready)
70 }
71}
72
73impl<T, E> From<Result<T, E>> for FutureResult<T, E> {
74 fn from(r: Result<T, E>) -> Self {
75 result(r)
76 }
77}