Hidden ResponseFuture type

This commit is contained in:
ItsEthra 2023-12-03 15:16:33 +03:00
parent 140a74c071
commit d7a8ee55b1

View file

@ -63,7 +63,7 @@ where
{ {
type Response = S::Response; type Response = S::Response;
type Error = S::Error; type Error = S::Error;
type Future = ResponseFuture<'a, S::Future>; type Future = private::ResponseFuture<'a, S::Future>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx) self.inner.poll_ready(cx)
@ -77,7 +77,7 @@ where
let response_future = self.inner.call(req); let response_future = self.inner.call(req);
ResponseFuture { private::ResponseFuture {
response_future, response_future,
hx_request: self.hx_request, hx_request: self.hx_request,
layer: self.layer.clone(), layer: self.layer.clone(),
@ -85,36 +85,40 @@ where
} }
} }
pin_project! { mod private {
pub struct ResponseFuture<'a, F> { use super::*;
#[pin]
response_future: F, pin_project! {
hx_request: bool, pub struct ResponseFuture<'a, F> {
layer: HxRequestGuardLayer<'a>, #[pin]
pub(super) response_future: F,
pub(super) hx_request: bool,
pub(super) layer: HxRequestGuardLayer<'a>,
}
} }
}
impl<'a, F, B, E> Future for ResponseFuture<'a, F> impl<'a, F, B, E> Future for ResponseFuture<'a, F>
where where
F: Future<Output = Result<Response<B>, E>>, F: Future<Output = Result<Response<B>, E>>,
B: Default, B: Default,
{ {
type Output = Result<Response<B>, E>; type Output = Result<Response<B>, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let this = self.project();
let response: Response<B> = ready!(this.response_future.poll(cx))?; let response: Response<B> = ready!(this.response_future.poll(cx))?;
match *this.hx_request { match *this.hx_request {
true => Poll::Ready(Ok(response)), true => Poll::Ready(Ok(response)),
false => { false => {
let res = Response::builder() let res = Response::builder()
.status(StatusCode::SEE_OTHER) .status(StatusCode::SEE_OTHER)
.header(LOCATION, this.layer.redirect_to) .header(LOCATION, this.layer.redirect_to)
.body(B::default()) .body(B::default())
.expect("failed to build response"); .expect("failed to build response");
Poll::Ready(Ok(res)) Poll::Ready(Ok(res))
}
} }
} }
} }