refactor(lib): Switch from pin-project to pin-project-lite (#2566)
Note the practical affects of this change: - Dependency count with --features full dropped from 65 to 55. - Time to compile after a clean dropped from 48s to 35s (on a pretty underpowered VM). Closes #2388
This commit is contained in:
@@ -48,7 +48,7 @@
|
||||
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
#[cfg(feature = "http2")]
|
||||
#[cfg(not(all(feature = "http1", feature = "http2")))]
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
#[cfg(all(feature = "runtime", feature = "http2"))]
|
||||
@@ -57,12 +57,14 @@ use std::time::Duration;
|
||||
use bytes::Bytes;
|
||||
use futures_util::future::{self, Either, FutureExt as _};
|
||||
use httparse::ParserConfig;
|
||||
use pin_project::pin_project;
|
||||
use pin_project_lite::pin_project;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use tower_service::Service;
|
||||
|
||||
use super::dispatch;
|
||||
use crate::body::HttpBody;
|
||||
#[cfg(not(all(feature = "http1", feature = "http2")))]
|
||||
use crate::common::Never;
|
||||
use crate::common::{
|
||||
exec::{BoxSendFuture, Exec},
|
||||
task, Future, Pin, Poll,
|
||||
@@ -74,17 +76,33 @@ use crate::upgrade::Upgraded;
|
||||
use crate::{Body, Request, Response};
|
||||
|
||||
#[cfg(feature = "http1")]
|
||||
type Http1Dispatcher<T, B, R> = proto::dispatch::Dispatcher<proto::dispatch::Client<B>, B, T, R>;
|
||||
type Http1Dispatcher<T, B> =
|
||||
proto::dispatch::Dispatcher<proto::dispatch::Client<B>, B, T, proto::h1::ClientTransaction>;
|
||||
|
||||
#[pin_project(project = ProtoClientProj)]
|
||||
enum ProtoClient<T, B>
|
||||
where
|
||||
B: HttpBody,
|
||||
{
|
||||
#[cfg(feature = "http1")]
|
||||
H1(#[pin] Http1Dispatcher<T, B, proto::h1::ClientTransaction>),
|
||||
#[cfg(feature = "http2")]
|
||||
H2(#[pin] proto::h2::ClientTask<B>, PhantomData<fn(T)>),
|
||||
#[cfg(not(feature = "http1"))]
|
||||
type Http1Dispatcher<T, B> = (Never, PhantomData<(T, Pin<Box<B>>)>);
|
||||
|
||||
#[cfg(feature = "http2")]
|
||||
type Http2ClientTask<B> = proto::h2::ClientTask<B>;
|
||||
|
||||
#[cfg(not(feature = "http2"))]
|
||||
type Http2ClientTask<B> = (Never, PhantomData<Pin<Box<B>>>);
|
||||
|
||||
pin_project! {
|
||||
#[project = ProtoClientProj]
|
||||
enum ProtoClient<T, B>
|
||||
where
|
||||
B: HttpBody,
|
||||
{
|
||||
H1 {
|
||||
#[pin]
|
||||
h1: Http1Dispatcher<T, B>,
|
||||
},
|
||||
H2 {
|
||||
#[pin]
|
||||
h2: Http2ClientTask<B>,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a handshake future over some IO.
|
||||
@@ -405,7 +423,7 @@ where
|
||||
pub fn into_parts(self) -> Parts<T> {
|
||||
match self.inner.expect("already upgraded") {
|
||||
#[cfg(feature = "http1")]
|
||||
ProtoClient::H1(h1) => {
|
||||
ProtoClient::H1 { h1 } => {
|
||||
let (io, read_buf, _) = h1.into_inner();
|
||||
Parts {
|
||||
io,
|
||||
@@ -413,10 +431,12 @@ where
|
||||
_inner: (),
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "http2")]
|
||||
ProtoClient::H2(..) => {
|
||||
ProtoClient::H2 { .. } => {
|
||||
panic!("http2 cannot into_inner");
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "http1"))]
|
||||
ProtoClient::H1 { h1 } => match h1.0 {},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -434,9 +454,14 @@ where
|
||||
pub fn poll_without_shutdown(&mut self, cx: &mut task::Context<'_>) -> Poll<crate::Result<()>> {
|
||||
match *self.inner.as_mut().expect("already upgraded") {
|
||||
#[cfg(feature = "http1")]
|
||||
ProtoClient::H1(ref mut h1) => h1.poll_without_shutdown(cx),
|
||||
ProtoClient::H1 { ref mut h1 } => h1.poll_without_shutdown(cx),
|
||||
#[cfg(feature = "http2")]
|
||||
ProtoClient::H2(ref mut h2, _) => Pin::new(h2).poll(cx).map_ok(|_| ()),
|
||||
ProtoClient::H2 { ref mut h2, .. } => Pin::new(h2).poll(cx).map_ok(|_| ()),
|
||||
|
||||
#[cfg(not(feature = "http1"))]
|
||||
ProtoClient::H1 { ref mut h1 } => match h1.0 {},
|
||||
#[cfg(not(feature = "http2"))]
|
||||
ProtoClient::H2 { ref mut h2, .. } => match h2.0 {},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -465,7 +490,7 @@ where
|
||||
proto::Dispatched::Shutdown => Poll::Ready(Ok(())),
|
||||
#[cfg(feature = "http1")]
|
||||
proto::Dispatched::Upgrade(pending) => match self.inner.take() {
|
||||
Some(ProtoClient::H1(h1)) => {
|
||||
Some(ProtoClient::H1 { h1 }) => {
|
||||
let (io, buf, _) = h1.into_inner();
|
||||
pending.fulfill(Upgraded::new(io, buf));
|
||||
Poll::Ready(Ok(()))
|
||||
@@ -756,14 +781,14 @@ impl Builder {
|
||||
}
|
||||
let cd = proto::h1::dispatch::Client::new(rx);
|
||||
let dispatch = proto::h1::Dispatcher::new(cd, conn);
|
||||
ProtoClient::H1(dispatch)
|
||||
ProtoClient::H1 { h1: dispatch }
|
||||
}
|
||||
#[cfg(feature = "http2")]
|
||||
Proto::Http2 => {
|
||||
let h2 =
|
||||
proto::h2::client::handshake(io, rx, &opts.h2_builder, opts.exec.clone())
|
||||
.await?;
|
||||
ProtoClient::H2(h2, PhantomData)
|
||||
ProtoClient::H2 { h2 }
|
||||
}
|
||||
};
|
||||
|
||||
@@ -817,9 +842,14 @@ where
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
|
||||
match self.project() {
|
||||
#[cfg(feature = "http1")]
|
||||
ProtoClientProj::H1(c) => c.poll(cx),
|
||||
ProtoClientProj::H1 { h1 } => h1.poll(cx),
|
||||
#[cfg(feature = "http2")]
|
||||
ProtoClientProj::H2(c, _) => c.poll(cx),
|
||||
ProtoClientProj::H2 { h2, .. } => h2.poll(cx),
|
||||
|
||||
#[cfg(not(feature = "http1"))]
|
||||
ProtoClientProj::H1 { h1 } => match h1.0 {},
|
||||
#[cfg(not(feature = "http2"))]
|
||||
ProtoClientProj::H2 { h2, .. } => match h2.0 {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user