From 0ced15d3cc10ace477ebda13ead8e6857b51867e Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 7 Mar 2023 15:20:50 -0500 Subject: [PATCH] feat(client): deprecate `client::conn` types (#3156) `client::conn::{SendRequest, Connection, Builder, handshake}` are deprecated as they are removed in 1.0. This adds the `deprecated` feature to Cargo, and only when `hyper/deprecated` is enabled will these warnings be emitted. Co-authored-by: KOVACS Tamas --- Cargo.toml | 3 +++ examples/tower_client.rs | 48 +++++++++++++++++++++++++++++++++------- src/client/client.rs | 9 ++++++++ src/client/conn.rs | 38 +++++++++++++++++++++++++++++++ src/client/service.rs | 5 +++++ src/ffi/client.rs | 1 + tests/client.rs | 2 ++ tests/server.rs | 1 + 8 files changed, 99 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d54d703107..e89657cc9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -112,6 +112,9 @@ ffi = ["libc"] # enable 1.0 backports backports = [] +# whether or not to display deprecation warnings +deprecated = [] + # internal features used in CI nightly = [] __internal_happy_eyeballs_tests = [] diff --git a/examples/tower_client.rs b/examples/tower_client.rs index 5a2a6e78df..f733fe0d35 100644 --- a/examples/tower_client.rs +++ b/examples/tower_client.rs @@ -1,20 +1,20 @@ #![deny(warnings)] -use hyper::client::conn::Builder; -use hyper::client::connect::HttpConnector; -use hyper::client::service::Connect; +use std::future::Future; +use std::pin::Pin; +use std::task::{Context, Poll}; + use hyper::service::Service; -use hyper::{Body, Request}; +use hyper::{Body, Request, Response}; +use tokio::net::TcpStream; #[tokio::main] -async fn main() -> Result<(), Box> { +async fn main() -> Result<(), Box> { pretty_env_logger::init(); - let mut mk_svc = Connect::new(HttpConnector::new(), Builder::new()); - let uri = "http://127.0.0.1:8080".parse::()?; - let mut svc = mk_svc.call(uri.clone()).await?; + let mut svc = Connector; let body = Body::empty(); @@ -25,3 +25,35 @@ async fn main() -> Result<(), Box> { Ok(()) } + +struct Connector; + +impl Service> for Connector { + type Response = Response; + type Error = Box; + type Future = Pin>>>; + + fn poll_ready(&mut self, _cx: &mut Context<'_>) -> std::task::Poll> { + Poll::Ready(Ok(())) + } + + fn call(&mut self, req: Request) -> Self::Future { + Box::pin(async move { + let host = req.uri().host().expect("no host in uri"); + let port = req.uri().port_u16().expect("no port in uri"); + + let stream = TcpStream::connect(format!("{}:{}", host, port)).await?; + + let (mut sender, conn) = hyper::client::conn::http1::handshake(stream).await?; + + tokio::task::spawn(async move { + if let Err(err) = conn.await { + println!("Connection error: {:?}", err); + } + }); + + let res = sender.send_request(req).await?; + Ok(res) + }) + } +} diff --git a/src/client/client.rs b/src/client/client.rs index 96de907655..9baead7349 100644 --- a/src/client/client.rs +++ b/src/client/client.rs @@ -33,6 +33,7 @@ use super::HttpConnector; #[cfg_attr(docsrs, doc(cfg(any(feature = "http1", feature = "http2"))))] pub struct Client { config: Config, + #[cfg_attr(feature = "deprecated", allow(deprecated))] conn_builder: conn::Builder, connector: C, pool: Pool>, @@ -327,12 +328,14 @@ where drop(delayed_tx); }); + #[cfg_attr(feature = "deprecated", allow(deprecated))] self.conn_builder.exec.execute(on_idle); } else { // There's no body to delay, but the connection isn't // ready yet. Only re-insert when it's ready let on_idle = future::poll_fn(move |cx| pooled.poll_ready(cx)).map(|_| ()); + #[cfg_attr(feature = "deprecated", allow(deprecated))] self.conn_builder.exec.execute(on_idle); } @@ -386,6 +389,7 @@ where }); // An execute error here isn't important, we're just trying // to prevent a waste of a socket... + #[cfg_attr(feature = "deprecated", allow(deprecated))] self.conn_builder.exec.execute(bg); } Ok(checked_out) @@ -430,6 +434,7 @@ where &self, pool_key: PoolKey, ) -> impl Lazy>>> + Unpin { + #[cfg_attr(feature = "deprecated", allow(deprecated))] let executor = self.conn_builder.exec.clone(); let pool = self.pool.clone(); #[cfg(not(feature = "http2"))] @@ -629,6 +634,7 @@ struct PoolClient { } enum PoolTx { + #[cfg_attr(feature = "deprecated", allow(deprecated))] Http1(conn::SendRequest), #[cfg(feature = "http2")] Http2(conn::Http2SendRequest), @@ -905,6 +911,7 @@ fn is_schema_secure(uri: &Uri) -> bool { #[derive(Clone)] pub struct Builder { client_config: Config, + #[cfg_attr(feature = "deprecated", allow(deprecated))] conn_builder: conn::Builder, pool_config: pool::Config, } @@ -917,6 +924,7 @@ impl Default for Builder { set_host: true, ver: Ver::Auto, }, + #[cfg_attr(feature = "deprecated", allow(deprecated))] conn_builder: conn::Builder::new(), pool_config: pool::Config { idle_timeout: Some(Duration::from_secs(90)), @@ -1381,6 +1389,7 @@ impl Builder { B: HttpBody + Send, B::Data: Send, { + #[cfg_attr(feature = "deprecated", allow(deprecated))] Client { config: self.client_config, conn_builder: self.conn_builder.clone(), diff --git a/src/client/conn.rs b/src/client/conn.rs index a8b41e4adb..3a6fb88a3a 100644 --- a/src/client/conn.rs +++ b/src/client/conn.rs @@ -123,16 +123,30 @@ pin_project! { /// /// This is a shortcut for `Builder::new().handshake(io)`. /// See [`client::conn`](crate::client::conn) for more. +#[cfg_attr( + feature = "deprecated", + deprecated( + note = "This function will be replaced with `client::conn::http1::handshake` and `client::conn::http2::handshake` in 1.0, enable the \"backports\" feature to use them now." + ) +)] +#[cfg_attr(feature = "deprecated", allow(deprecated))] pub async fn handshake( io: T, ) -> crate::Result<(SendRequest, Connection)> where T: AsyncRead + AsyncWrite + Unpin + Send + 'static, { + #[allow(deprecated)] Builder::new().handshake(io).await } /// The sender side of an established connection. +#[cfg_attr( + feature = "deprecated", + deprecated( + note = "This type will be replaced with `client::conn::http1::SendRequest` and `client::conn::http2::SendRequest` in 1.0, enable the \"backports\" feature to use them now." + ) +)] pub struct SendRequest { dispatch: dispatch::Sender, Response>, } @@ -142,6 +156,12 @@ pub struct SendRequest { /// In most cases, this should just be spawned into an executor, so that it /// can process incoming and outgoing messages, notice hangups, and the like. #[must_use = "futures do nothing unless polled"] +#[cfg_attr( + feature = "deprecated", + deprecated( + note = "This type will be replaced with `client::conn::http1::Connection` and `client::conn::http2::Connection` in 1.0, enable the \"backports\" feature to use them now." + ) +)] pub struct Connection where T: AsyncRead + AsyncWrite + Send + 'static, @@ -154,6 +174,12 @@ where /// /// After setting options, the builder is used to create a handshake future. #[derive(Clone, Debug)] +#[cfg_attr( + feature = "deprecated", + deprecated( + note = "This type will be replaced with `client::conn::http1::Builder` and `client::conn::http2::Builder` in 1.0, enable the \"backports\" feature to use them now." + ) +)] pub struct Builder { pub(super) exec: Exec, h09_responses: bool, @@ -226,6 +252,7 @@ pub(super) struct Http2SendRequest { // ===== impl SendRequest +#[cfg_attr(feature = "deprecated", allow(deprecated))] impl SendRequest { /// Polls to determine whether this sender can be used yet for a request. /// @@ -259,6 +286,7 @@ impl SendRequest { } } +#[cfg_attr(feature = "deprecated", allow(deprecated))] impl SendRequest where B: HttpBody + 'static, @@ -344,6 +372,7 @@ where } } +#[cfg_attr(feature = "deprecated", allow(deprecated))] impl Service> for SendRequest where B: HttpBody + 'static, @@ -361,6 +390,7 @@ where } } +#[cfg_attr(feature = "deprecated", allow(deprecated))] impl fmt::Debug for SendRequest { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("SendRequest").finish() @@ -430,6 +460,7 @@ impl Clone for Http2SendRequest { // ===== impl Connection +#[cfg_attr(feature = "deprecated", allow(deprecated))] impl Connection where T: AsyncRead + AsyncWrite + Unpin + Send + 'static, @@ -513,6 +544,7 @@ where } } +#[cfg_attr(feature = "deprecated", allow(deprecated))] impl Future for Connection where T: AsyncRead + AsyncWrite + Unpin + Send + 'static, @@ -541,6 +573,7 @@ where } } +#[cfg_attr(feature = "deprecated", allow(deprecated))] impl fmt::Debug for Connection where T: AsyncRead + AsyncWrite + fmt::Debug + Send + 'static, @@ -553,6 +586,7 @@ where // ===== impl Builder +#[cfg_attr(feature = "deprecated", allow(deprecated))] impl Builder { /// Creates a new connection builder. #[inline] @@ -1090,9 +1124,11 @@ where trait AssertSend: Send {} trait AssertSendSync: Send + Sync {} +#[cfg_attr(feature = "deprecated", allow(deprecated))] #[doc(hidden)] impl AssertSendSync for SendRequest {} +#[cfg_attr(feature = "deprecated", allow(deprecated))] #[doc(hidden)] impl AssertSend for Connection where @@ -1102,6 +1138,7 @@ where { } +#[cfg_attr(feature = "deprecated", allow(deprecated))] #[doc(hidden)] impl AssertSendSync for Connection where @@ -1111,6 +1148,7 @@ where { } +#[cfg_attr(feature = "deprecated", allow(deprecated))] #[doc(hidden)] impl AssertSendSync for Builder {} diff --git a/src/client/service.rs b/src/client/service.rs index 406f61edc9..f3560ea088 100644 --- a/src/client/service.rs +++ b/src/client/service.rs @@ -8,6 +8,7 @@ use std::marker::PhantomData; use tracing::debug; +#[cfg_attr(feature = "deprecated", allow(deprecated))] use super::conn::{Builder, SendRequest}; use crate::{ body::HttpBody, @@ -23,6 +24,7 @@ use crate::{ #[derive(Debug)] pub struct Connect { inner: C, + #[cfg_attr(feature = "deprecated", allow(deprecated))] builder: Builder, _pd: PhantomData, } @@ -30,6 +32,7 @@ pub struct Connect { impl Connect { /// Create a new `Connect` with some inner connector `C` and a connection /// builder. + #[cfg_attr(feature = "deprecated", allow(deprecated))] pub fn new(inner: C, builder: Builder) -> Self { Self { inner, @@ -49,6 +52,7 @@ where B::Data: Send + Unpin, B::Error: Into>, { + #[cfg_attr(feature = "deprecated", allow(deprecated))] type Response = SendRequest; type Error = crate::Error; type Future = @@ -68,6 +72,7 @@ where match io.await { Ok(io) => match builder.handshake(io).await { Ok((sr, conn)) => { + #[cfg_attr(feature = "deprecated", allow(deprecated))] builder.exec.execute(async move { if let Err(e) = conn.await { debug!("connection error: {:?}", e); diff --git a/src/ffi/client.rs b/src/ffi/client.rs index 4cdb257e30..670f77d141 100644 --- a/src/ffi/client.rs +++ b/src/ffi/client.rs @@ -93,6 +93,7 @@ unsafe impl AsTaskType for hyper_clientconn { ffi_fn! { /// Creates a new set of HTTP clientconn options to be used in a handshake. fn hyper_clientconn_options_new() -> *mut hyper_clientconn_options { + #[allow(deprecated)] let builder = conn::Builder::new(); Box::into_raw(Box::new(hyper_clientconn_options { diff --git a/tests/client.rs b/tests/client.rs index 965c21bb12..010860adc6 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -2181,6 +2181,7 @@ mod dispatch_impl { } } +#[allow(deprecated)] mod conn { use std::io::{self, Read, Write}; use std::net::{SocketAddr, TcpListener}; @@ -2246,6 +2247,7 @@ mod conn { future::join(server, client).await; } + #[deny(deprecated)] #[cfg(feature = "backports")] mod backports { use super::*; diff --git a/tests/server.rs b/tests/server.rs index 017cca9405..d5e09f9795 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -2536,6 +2536,7 @@ async fn http2_keep_alive_with_responsive_client() { }); let tcp = connect_async(addr).await; + #[allow(deprecated)] let (mut client, conn) = hyper::client::conn::Builder::new() .http2_only(true) .handshake::<_, Body>(tcp)