use {Future, IntoFuture, Poll}; use super::chain::Chain; /// Future for the `or_else` combinator, chaining a computation onto the end of /// a future which fails with an error. /// /// This is created by the `Future::or_else` method. #[derive(Debug)] #[must_use = "futures do nothing unless polled"] pub struct OrElse where A: Future, B: IntoFuture { state: Chain, } pub fn new(future: A, f: F) -> OrElse where A: Future, B: IntoFuture, { OrElse { state: Chain::new(future, f), } } impl Future for OrElse where A: Future, B: IntoFuture, F: FnOnce(A::Error) -> B, { type Item = B::Item; type Error = B::Error; fn poll(&mut self) -> Poll { self.state.poll(|a, f| { match a { Ok(item) => Ok(Ok(item)), Err(e) => Ok(Err(f(e).into_future())) } }) } }