futures_util/future/
or_else.rsuse futures_core::{Future, IntoFuture, Poll};
use futures_core::task;
use super::chain::Chain;
#[derive(Debug)]
#[must_use = "futures do nothing unless polled"]
pub struct OrElse<A, B, F> where A: Future, B: IntoFuture {
state: Chain<A, B::Future, F>,
}
pub fn new<A, B, F>(future: A, f: F) -> OrElse<A, B, F>
where A: Future,
B: IntoFuture<Item=A::Item>,
{
OrElse {
state: Chain::new(future, f),
}
}
impl<A, B, F> Future for OrElse<A, B, F>
where A: Future,
B: IntoFuture<Item=A::Item>,
F: FnOnce(A::Error) -> B,
{
type Item = B::Item;
type Error = B::Error;
fn poll(&mut self, cx: &mut task::Context) -> Poll<B::Item, B::Error> {
self.state.poll(cx, |a, f| {
match a {
Ok(item) => Ok(Ok(item)),
Err(e) => Ok(Err(f(e).into_future()))
}
})
}
}