tower/tower/src/reconnect/future.rs

56 lines
1.3 KiB
Rust
Raw Normal View History

use pin_project::pin_project;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
2019-03-08 08:46:12 -08:00
/// Future that resolves to the response or failure to connect.
#[pin_project]
#[derive(Debug)]
pub struct ResponseFuture<F, E> {
#[pin]
inner: Inner<F, E>,
2019-03-08 08:46:12 -08:00
}
#[pin_project(project = InnerProj)]
#[derive(Debug)]
enum Inner<F, E> {
Future(#[pin] F),
Error(Option<E>),
}
impl<F, E> ResponseFuture<F, E> {
2019-03-08 08:46:12 -08:00
pub(crate) fn new(inner: F) -> Self {
ResponseFuture {
inner: Inner::Future(inner),
}
}
pub(crate) fn error(error: E) -> Self {
ResponseFuture {
inner: Inner::Error(Some(error)),
}
2019-03-08 08:46:12 -08:00
}
}
impl<F, T, E, ME> Future for ResponseFuture<F, ME>
2019-03-08 08:46:12 -08:00
where
F: Future<Output = Result<T, E>>,
E: Into<crate::BoxError>,
ME: Into<crate::BoxError>,
2019-03-08 08:46:12 -08:00
{
type Output = Result<T, crate::BoxError>;
2019-03-08 08:46:12 -08:00
2019-09-30 11:58:27 -07:00
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let me = self.project();
match me.inner.project() {
InnerProj::Future(fut) => fut.poll(cx).map_err(Into::into),
InnerProj::Error(e) => {
let e = e.take().expect("Polled after ready.").into();
Poll::Ready(Err(e))
}
}
2019-03-08 08:46:12 -08:00
}
}