2020-03-31 10:10:47 -07:00
|
|
|
use super::Error;
|
2020-04-14 13:42:37 -07:00
|
|
|
use pin_project::{pin_project, project};
|
2019-09-10 08:48:01 -07:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2019-03-08 08:46:12 -08:00
|
|
|
|
2019-12-02 16:14:15 -08:00
|
|
|
/// Future that resolves to the response or failure to connect.
|
2019-09-10 08:48:01 -07:00
|
|
|
#[pin_project]
|
2019-09-23 14:28:14 -07:00
|
|
|
#[derive(Debug)]
|
2020-04-14 13:42:37 -07:00
|
|
|
pub struct ResponseFuture<F, E> {
|
2019-09-10 08:48:01 -07:00
|
|
|
#[pin]
|
2020-04-14 13:42:37 -07:00
|
|
|
inner: Inner<F, E>,
|
2019-03-08 08:46:12 -08:00
|
|
|
}
|
|
|
|
|
2020-04-14 13:42:37 -07:00
|
|
|
#[pin_project]
|
|
|
|
#[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 {
|
2020-04-14 13:42:37 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 13:42:37 -07:00
|
|
|
impl<F, T, E, ME> Future for ResponseFuture<F, ME>
|
2019-03-08 08:46:12 -08:00
|
|
|
where
|
2019-09-10 08:48:01 -07:00
|
|
|
F: Future<Output = Result<T, E>>,
|
|
|
|
E: Into<Error>,
|
2020-04-14 13:42:37 -07:00
|
|
|
ME: Into<Error>,
|
2019-03-08 08:46:12 -08:00
|
|
|
{
|
2019-09-10 08:48:01 -07:00
|
|
|
type Output = Result<T, Error>;
|
2019-03-08 08:46:12 -08:00
|
|
|
|
2020-04-14 13:42:37 -07:00
|
|
|
#[project]
|
2019-09-30 11:58:27 -07:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2020-04-14 13:42:37 -07:00
|
|
|
let me = self.project();
|
|
|
|
#[project]
|
|
|
|
match me.inner.project() {
|
|
|
|
Inner::Future(fut) => fut.poll(cx).map_err(Into::into),
|
|
|
|
Inner::Error(e) => {
|
|
|
|
let e = e.take().expect("Polled after ready.").into();
|
|
|
|
Poll::Ready(Err(e))
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 08:46:12 -08:00
|
|
|
}
|
|
|
|
}
|