zebra/tower-batch/src/future.rs

88 lines
2.3 KiB
Rust
Raw Normal View History

//! Future types for the `Batch` middleware.
use super::{error::Closed, message};
use futures_core::ready;
2020-06-16 13:56:18 -07:00
use pin_project::pin_project;
use std::{
fmt::Debug,
future::Future,
pin::Pin,
task::{Context, Poll},
};
use tower::Service;
/// Future that completes when the batch processing is complete.
#[pin_project]
#[derive(Debug)]
pub struct ResponseFuture<T, E, R>
where
T: Service<crate::BatchControl<R>>,
{
#[pin]
state: ResponseState<T, E, R>,
}
2020-06-16 13:56:18 -07:00
#[pin_project(project = ResponseStateProj)]
enum ResponseState<T, E, R>
where
T: Service<crate::BatchControl<R>>,
{
Failed(Option<E>),
Rx(#[pin] message::Rx<T::Future, T::Error>),
Poll(#[pin] T::Future),
}
impl<T, E, R> Debug for ResponseState<T, E, R>
where
T: Service<crate::BatchControl<R>>,
{
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}
impl<T, E, R> ResponseFuture<T, E, R>
where
T: Service<crate::BatchControl<R>>,
{
pub(crate) fn new(rx: message::Rx<T::Future, T::Error>) -> Self {
ResponseFuture {
state: ResponseState::Rx(rx),
}
}
pub(crate) fn failed(err: E) -> Self {
ResponseFuture {
state: ResponseState::Failed(Some(err)),
}
}
}
impl<S, E2, R> Future for ResponseFuture<S, E2, R>
where
S: Service<crate::BatchControl<R>>,
S::Future: Future<Output = Result<S::Response, S::Error>>,
S::Error: Into<E2>,
crate::error::Closed: Into<E2>,
{
type Output = Result<S::Response, E2>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project();
loop {
match this.state.as_mut().project() {
2020-06-16 13:56:18 -07:00
ResponseStateProj::Failed(e) => {
return Poll::Ready(Err(e.take().expect("polled after error")));
}
2020-06-16 13:56:18 -07:00
ResponseStateProj::Rx(rx) => match ready!(rx.poll(cx)) {
Ok(Ok(f)) => this.state.set(ResponseState::Poll(f)),
Ok(Err(e)) => return Poll::Ready(Err(e.into())),
Err(_) => return Poll::Ready(Err(Closed::new().into())),
},
2020-06-16 13:56:18 -07:00
ResponseStateProj::Poll(fut) => return fut.poll(cx).map_err(Into::into),
}
}
}
}