From a3c9fde7b3412535ea814ef9ae9bf759f8875cee Mon Sep 17 00:00:00 2001 From: tottoto Date: Mon, 6 Nov 2023 23:03:28 +0900 Subject: [PATCH] refactor(common): replace Never with Infallible (#3392) --- src/body/body.rs | 6 +++--- src/client/conn.rs | 8 ++++---- src/client/pool.rs | 4 ++-- src/common/mod.rs | 3 --- src/common/never.rs | 21 --------------------- src/common/task.rs | 9 +++++---- src/proto/h1/dispatch.rs | 4 ++-- src/proto/h2/client.rs | 9 +++++---- src/server/conn.rs | 8 ++++---- 9 files changed, 25 insertions(+), 47 deletions(-) delete mode 100644 src/common/never.rs diff --git a/src/body/body.rs b/src/body/body.rs index fddf4f07b8..7df87404f6 100644 --- a/src/body/body.rs +++ b/src/body/body.rs @@ -1,4 +1,6 @@ use std::borrow::Cow; +#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))] +use std::convert::Infallible; #[cfg(feature = "stream")] use std::error::Error as StdError; use std::fmt; @@ -19,8 +21,6 @@ use super::DecodedLength; #[cfg(feature = "stream")] use crate::common::sync_wrapper::SyncWrapper; use crate::common::watch; -#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))] -use crate::common::Never; #[cfg(all(feature = "http2", any(feature = "client", feature = "server")))] use crate::proto::h2::ping; @@ -79,7 +79,7 @@ struct Extra { } #[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))] -type DelayEofUntil = oneshot::Receiver; +type DelayEofUntil = oneshot::Receiver; enum DelayEof { /// Initial state, stream hasn't seen EOF yet. diff --git a/src/client/conn.rs b/src/client/conn.rs index 35770d4e6b..8da457da64 100644 --- a/src/client/conn.rs +++ b/src/client/conn.rs @@ -59,6 +59,8 @@ pub mod http1; #[cfg(all(feature = "backports", feature = "http2"))] pub mod http2; +#[cfg(not(all(feature = "http1", feature = "http2")))] +use std::convert::Infallible; use std::error::Error as StdError; use std::fmt; use std::future::Future; @@ -82,8 +84,6 @@ use tracing::{debug, trace}; use super::dispatch; use crate::body::HttpBody; use crate::common::exec::{BoxSendFuture, Exec}; -#[cfg(not(all(feature = "http1", feature = "http2")))] -use crate::common::Never; use crate::proto; use crate::rt::Executor; #[cfg(feature = "http1")] @@ -95,13 +95,13 @@ type Http1Dispatcher = proto::dispatch::Dispatcher, B, T, proto::h1::ClientTransaction>; #[cfg(not(feature = "http1"))] -type Http1Dispatcher = (Never, PhantomData<(T, Pin>)>); +type Http1Dispatcher = (Infallible, PhantomData<(T, Pin>)>); #[cfg(feature = "http2")] type Http2ClientTask = proto::h2::ClientTask; #[cfg(not(feature = "http2"))] -type Http2ClientTask = (Never, PhantomData>>); +type Http2ClientTask = (Infallible, PhantomData>>); pin_project! { #[project = ProtoClientProj] diff --git a/src/client/pool.rs b/src/client/pool.rs index 96425611e9..1dfd6ba3d3 100644 --- a/src/client/pool.rs +++ b/src/client/pool.rs @@ -83,7 +83,7 @@ struct PoolInner { // A oneshot channel is used to allow the interval to be notified when // the Pool completely drops. That way, the interval can cancel immediately. #[cfg(feature = "runtime")] - idle_interval_ref: Option>, + idle_interval_ref: Option>, #[cfg(feature = "runtime")] exec: Exec, timeout: Option, @@ -741,7 +741,7 @@ pin_project_lite::pin_project! { // Pool is fully dropped, and shutdown. This channel is never sent on, // but Err(Canceled) will be received when the Pool is dropped. #[pin] - pool_drop_notifier: oneshot::Receiver, + pool_drop_notifier: oneshot::Receiver, } } diff --git a/src/common/mod.rs b/src/common/mod.rs index 5327f21b5b..3d83946243 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -17,7 +17,6 @@ pub(crate) mod exec; pub(crate) mod io; #[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))] mod lazy; -mod never; #[cfg(any( feature = "stream", all(feature = "client", any(feature = "http1", feature = "http2")) @@ -29,5 +28,3 @@ pub(crate) mod watch; #[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))] pub(crate) use self::lazy::{lazy, Started as Lazy}; -#[cfg(any(feature = "http1", feature = "http2", feature = "runtime"))] -pub(crate) use self::never::Never; diff --git a/src/common/never.rs b/src/common/never.rs deleted file mode 100644 index f143caf60f..0000000000 --- a/src/common/never.rs +++ /dev/null @@ -1,21 +0,0 @@ -//! An uninhabitable type meaning it can never happen. -//! -//! To be replaced with `!` once it is stable. - -use std::error::Error; -use std::fmt; - -#[derive(Debug)] -pub(crate) enum Never {} - -impl fmt::Display for Never { - fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self {} - } -} - -impl Error for Never { - fn description(&self) -> &str { - match *self {} - } -} diff --git a/src/common/task.rs b/src/common/task.rs index 1d9f2a9982..0ac047a462 100644 --- a/src/common/task.rs +++ b/src/common/task.rs @@ -1,11 +1,12 @@ -use std::task::{Context, Poll}; - -use super::Never; +use std::{ + convert::Infallible, + task::{Context, Poll}, +}; /// A function to help "yield" a future, such that it is re-scheduled immediately. /// /// Useful for spin counts, so a future doesn't hog too much time. -pub(crate) fn yield_now(cx: &mut Context<'_>) -> Poll { +pub(crate) fn yield_now(cx: &mut Context<'_>) -> Poll { cx.waker().wake_by_ref(); Poll::Pending } diff --git a/src/proto/h1/dispatch.rs b/src/proto/h1/dispatch.rs index a0c4daec1e..3516d7ad21 100644 --- a/src/proto/h1/dispatch.rs +++ b/src/proto/h1/dispatch.rs @@ -566,13 +566,13 @@ cfg_client! { { type PollItem = RequestHead; type PollBody = B; - type PollError = crate::common::Never; + type PollError = std::convert::Infallible; type RecvItem = crate::proto::ResponseHead; fn poll_msg( mut self: Pin<&mut Self>, cx: &mut Context<'_>, - ) -> Poll>> { + ) -> Poll>> { let mut this = self.as_mut(); debug_assert!(!this.rx_closed); match this.rx.poll_recv(cx) { diff --git a/src/proto/h2/client.rs b/src/proto/h2/client.rs index f43aaa0c01..8c2a4d2e0f 100644 --- a/src/proto/h2/client.rs +++ b/src/proto/h2/client.rs @@ -1,3 +1,4 @@ +use std::convert::Infallible; use std::error::Error as StdError; use std::future::Future; use std::marker::Unpin; @@ -19,7 +20,7 @@ use tracing::{debug, trace, warn}; use super::{ping, H2Upgraded, PipeToSendStream, SendBuf}; use crate::body::HttpBody; use crate::client::dispatch::Callback; -use crate::common::{exec::Exec, Never}; +use crate::common::exec::Exec; use crate::ext::Protocol; use crate::headers; use crate::proto::h2::UpgradedSendStream; @@ -32,11 +33,11 @@ type ClientRx = crate::client::dispatch::Receiver, Response> ///// An mpsc channel is used to help notify the `Connection` task when *all* ///// other handles to it have been dropped, so that it can shutdown. -type ConnDropRef = mpsc::Sender; +type ConnDropRef = mpsc::Sender; ///// A oneshot channel watches the `Connection` task, and when it completes, ///// the "dispatch" task will be notified and can shutdown sooner. -type ConnEof = oneshot::Receiver; +type ConnEof = oneshot::Receiver; // Our defaults are chosen for the "majority" case, which usually are not // resource constrained, and so the spec default of 64kb can be too limiting @@ -181,7 +182,7 @@ where }) } -async fn conn_task(conn: C, drop_rx: D, cancel_tx: oneshot::Sender) +async fn conn_task(conn: C, drop_rx: D, cancel_tx: oneshot::Sender) where C: Future + Unpin, D: Future + Unpin, diff --git a/src/server/conn.rs b/src/server/conn.rs index d8b881a778..8ce4c95193 100644 --- a/src/server/conn.rs +++ b/src/server/conn.rs @@ -72,6 +72,8 @@ cfg_feature! { use std::pin::Pin; use std::future::Future; use std::marker::Unpin; + #[cfg(not(all(feature = "http1", feature = "http2")))] + use std::convert::Infallible; use bytes::Bytes; use pin_project_lite::pin_project; @@ -80,8 +82,6 @@ cfg_feature! { pub use super::server::Connecting; use crate::body::{Body, HttpBody}; - #[cfg(not(all(feature = "http1", feature = "http2")))] - use crate::common::Never; use crate::common::exec::{ConnStreamExec, Exec}; use crate::proto; use crate::service::HttpService; @@ -159,14 +159,14 @@ type Http1Dispatcher = proto::h1::Dispatcher, B, T, proto::ServerTransaction>; #[cfg(all(not(feature = "http1"), feature = "http2"))] -type Http1Dispatcher = (Never, PhantomData<(T, Box>, Box>)>); +type Http1Dispatcher = (Infallible, PhantomData<(T, Box>, Box>)>); #[cfg(feature = "http2")] type Http2Server = proto::h2::Server, S, B, E>; #[cfg(all(not(feature = "http2"), feature = "http1"))] type Http2Server = ( - Never, + Infallible, PhantomData<(T, Box>, Box>, Box>)>, );