Clean up constructors for future newtypes (#415)

This commit is contained in:
David Pedersen 2021-10-26 01:03:16 +02:00 committed by GitHub
parent 68e44fa9da
commit 040f63b8e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 21 deletions

View file

@ -86,6 +86,7 @@ where
type Error = Infallible;
type Future = ResponseFuture<Self::Response>;
#[inline]
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
@ -93,9 +94,7 @@ where
fn call(&mut self, target: T) -> Self::Future {
let connect_info = ConnectInfo(C::connect_info(target));
let svc = AddExtensionLayer::new(connect_info).layer(self.svc.clone());
ResponseFuture {
future: ready(Ok(svc)),
}
ResponseFuture::new(ready(Ok(svc)))
}
}

View file

@ -75,6 +75,6 @@ where
let handler = self.handler.clone();
let future = Handler::call(handler, req).map(Ok::<_, Infallible> as _);
super::future::IntoServiceFuture { future }
super::future::IntoServiceFuture::new(future)
}
}

View file

@ -12,7 +12,13 @@ macro_rules! opaque_future {
pin_project_lite::pin_project! {
$(#[$m])*
pub struct $name<$($param),*> {
#[pin] pub(crate) future: $actual,
#[pin] future: $actual,
}
}
impl<$($param),*> $name<$($param),*> {
pub(crate) fn new(future: $actual) -> Self {
Self { future }
}
}

View file

@ -1,11 +1,12 @@
//! Future types.
use crate::body::BoxBody;
use futures_util::future::Either;
use http::{Request, Response};
use pin_project_lite::pin_project;
use std::{
convert::Infallible,
future::Future,
future::{ready, Future},
pin::Pin,
task::{Context, Poll},
};
@ -23,15 +24,11 @@ opaque_future! {
impl<B> RouterFuture<B> {
pub(super) fn from_oneshot(future: Oneshot<super::Route<B>, Request<B>>) -> Self {
Self {
future: futures_util::future::Either::Left(future),
}
Self::new(Either::Left(future))
}
pub(super) fn from_response(response: Response<BoxBody>) -> Self {
RouterFuture {
future: futures_util::future::Either::Right(std::future::ready(Ok(response))),
}
RouterFuture::new(Either::Right(ready(Ok(response))))
}
}

View file

@ -928,9 +928,7 @@ where
.body(crate::body::empty())
.unwrap();
MethodNotAllowedFuture {
future: ready(Ok(res)),
}
MethodNotAllowedFuture::new(ready(Ok(res)))
}
}
@ -1019,9 +1017,7 @@ where
}
fn call(&mut self, _target: T) -> Self::Future {
future::MakeRouteServiceFuture {
future: ready(Ok(self.service.clone())),
}
future::MakeRouteServiceFuture::new(ready(Ok(self.service.clone())))
}
}
@ -1054,9 +1050,7 @@ impl<B> Service<Request<B>> for Route<B> {
#[inline]
fn call(&mut self, req: Request<B>) -> Self::Future {
RouteFuture {
future: self.0.call(req),
}
RouteFuture::new(self.0.call(req))
}
}