Skip to content

Commit c20cd20

Browse files
authored
clean up exposed crate features (#50)
- Put `TokioExecutor` and `TokioIo` into a single `rt::tokio` module. - The `rt::tokio` module is behind a `tokio` crate feature. - Changed `auto` crate feature to `server-auto`. - Added `client-legacy` crate feature, with `client::legacy` behind it. - Removed `tcp` and `runtime` features, code now depends on the `tokio` feature.
1 parent 02dc44f commit c20cd20

File tree

9 files changed

+87
-85
lines changed

9 files changed

+87
-85
lines changed

Cargo.toml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ http = "1.0"
2222
http-body = "1.0.0"
2323
bytes = "1"
2424
pin-project-lite = "0.2.4"
25-
socket2 = "0.5"
25+
socket2 = { version = "0.5", optional = true }
2626
tracing = { version = "0.1", default-features = false, features = ["std"] }
27-
tokio = { version = "1", features = ["net", "rt", "time"] }
27+
tokio = { version = "1", optional = true, features = ["net", "rt", "time"] }
2828
tower-service = "0.3"
2929
tower = { version = "0.4", features = ["make", "util"] }
3030

@@ -42,21 +42,30 @@ pnet_datalink = "0.34.0"
4242
default = []
4343

4444
# Shorthand to enable everything
45-
full = ["client", "server", "http1", "http2", "tcp", "auto", "runtime"]
45+
full = [
46+
"client",
47+
"client-legacy",
48+
"server",
49+
"server-auto",
50+
"http1",
51+
"http2",
52+
"tokio",
53+
]
4654

4755
client = ["hyper/client"]
56+
client-legacy = ["client"]
57+
4858
server = ["hyper/server"]
59+
server-auto = ["hyper/server", "http1", "http2"]
4960

5061
http1 = ["hyper/http1"]
5162
http2 = ["hyper/http2"]
5263

53-
tcp = []
54-
auto = ["hyper/server", "http1", "http2"]
55-
runtime = []
64+
tokio = ["dep:tokio", "dep:socket2"]
5665

5766
# internal features used in CI
5867
__internal_happy_eyeballs_tests = []
5968

6069
[[example]]
6170
name = "client"
62-
required-features = ["client", "http1", "tcp", "runtime"]
71+
required-features = ["client-legacy", "http1", "tokio"]

src/client/legacy/client.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use hyper::{body::Body, Method, Request, Response, Uri, Version};
1919
use tracing::{debug, trace, warn};
2020

2121
use super::super::pool::{self, Ver};
22-
#[cfg(feature = "tcp")]
22+
#[cfg(feature = "tokio")]
2323
use super::connect::HttpConnector;
2424
use super::connect::{Alpn, Connect, Connected, Connection};
2525
use crate::common::{lazy as hyper_lazy, Exec, Lazy, SyncWrapper};
@@ -103,7 +103,7 @@ impl Client<(), ()> {
103103
/// # Example
104104
///
105105
/// ```
106-
/// # #[cfg(feature = "runtime")]
106+
/// # #[cfg(feature = "tokio")]
107107
/// # fn run () {
108108
/// use std::time::Duration;
109109
/// use hyper::Client;
@@ -144,7 +144,7 @@ where
144144
/// # Example
145145
///
146146
/// ```
147-
/// # #[cfg(feature = "runtime")]
147+
/// # #[cfg(feature = "tokio")]
148148
/// # fn run () {
149149
/// use hyper::{Client, Uri};
150150
///
@@ -173,7 +173,7 @@ where
173173
/// # Example
174174
///
175175
/// ```
176-
/// # #[cfg(feature = "runtime")]
176+
/// # #[cfg(feature = "tokio")]
177177
/// # fn run () {
178178
/// use hyper::{Method, Client, Request};
179179
/// use http_body_util::Full;
@@ -934,7 +934,7 @@ fn is_schema_secure(uri: &Uri) -> bool {
934934
/// # Example
935935
///
936936
/// ```
937-
/// # #[cfg(feature = "runtime")]
937+
/// # #[cfg(feature = "tokio")]
938938
/// # fn run () {
939939
/// use std::time::Duration;
940940
/// use hyper::Client;
@@ -1286,7 +1286,7 @@ impl Builder {
12861286
/// # Cargo Feature
12871287
///
12881288
/// Requires the `runtime` cargo feature to be enabled.
1289-
#[cfg(feature = "runtime")]
1289+
#[cfg(feature = "tokio")]
12901290
#[cfg(feature = "http2")]
12911291
#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
12921292
pub fn http2_keep_alive_interval(
@@ -1307,7 +1307,7 @@ impl Builder {
13071307
/// # Cargo Feature
13081308
///
13091309
/// Requires the `runtime` cargo feature to be enabled.
1310-
#[cfg(feature = "runtime")]
1310+
#[cfg(feature = "tokio")]
13111311
#[cfg(feature = "http2")]
13121312
#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
13131313
pub fn http2_keep_alive_timeout(&mut self, timeout: Duration) -> &mut Self {
@@ -1327,7 +1327,7 @@ impl Builder {
13271327
/// # Cargo Feature
13281328
///
13291329
/// Requires the `runtime` cargo feature to be enabled.
1330-
#[cfg(feature = "runtime")]
1330+
#[cfg(feature = "tokio")]
13311331
#[cfg(feature = "http2")]
13321332
#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
13331333
pub fn http2_keep_alive_while_idle(&mut self, enabled: bool) -> &mut Self {
@@ -1410,7 +1410,7 @@ impl Builder {
14101410
}
14111411

14121412
/// Builder a client with this configuration and the default `HttpConnector`.
1413-
#[cfg(feature = "tcp")]
1413+
#[cfg(feature = "tokio")]
14141414
pub fn build_http<B>(&self) -> Client<HttpConnector, B>
14151415
where
14161416
B: Body + Send,

src/client/legacy/connect/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ use std::fmt;
6666

6767
use ::http::Extensions;
6868

69+
#[cfg(feature = "tokio")]
6970
pub use self::http::{HttpConnector, HttpInfo};
7071

72+
#[cfg(feature = "tokio")]
7173
pub mod dns;
74+
#[cfg(feature = "tokio")]
7275
mod http;
7376

7477
pub use self::sealed::Connect;

src/client/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! HTTP client utilities
22
33
/// Legacy implementations of `connect` module and `Client`
4+
#[cfg(feature = "client-legacy")]
45
pub mod legacy;
56
#[doc(hidden)]
67
pub mod pool;

src/client/pool.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ use std::pin::Pin;
1212
use std::sync::{Arc, Mutex, Weak};
1313
use std::task::{self, Poll};
1414

15-
#[cfg(not(feature = "runtime"))]
1615
use std::time::{Duration, Instant};
17-
#[cfg(feature = "runtime")]
18-
use tokio::time::{Duration, Instant, Interval};
1916

2017
use futures_channel::oneshot;
2118
use tracing::{debug, trace};
@@ -97,9 +94,7 @@ struct PoolInner<T, K: Eq + Hash> {
9794
waiters: HashMap<K, VecDeque<oneshot::Sender<T>>>,
9895
// A oneshot channel is used to allow the interval to be notified when
9996
// the Pool completely drops. That way, the interval can cancel immediately.
100-
#[cfg(feature = "runtime")]
10197
idle_interval_ref: Option<oneshot::Sender<Infallible>>,
102-
#[cfg(feature = "runtime")]
10398
exec: Exec,
10499
timeout: Option<Duration>,
105100
}
@@ -130,11 +125,9 @@ impl<T, K: Key> Pool<T, K> {
130125
Some(Arc::new(Mutex::new(PoolInner {
131126
connecting: HashSet::new(),
132127
idle: HashMap::new(),
133-
#[cfg(feature = "runtime")]
134128
idle_interval_ref: None,
135129
max_idle_per_host: config.max_idle_per_host,
136130
waiters: HashMap::new(),
137-
#[cfg(feature = "runtime")]
138131
exec,
139132
timeout: config.idle_timeout,
140133
})))
@@ -152,7 +145,6 @@ impl<T, K: Key> Pool<T, K> {
152145
#[cfg(test)]
153146
pub(super) fn no_timer(&self) {
154147
// Prevent an actual interval from being created for this pool...
155-
#[cfg(feature = "runtime")]
156148
{
157149
let mut inner = self.inner.as_ref().unwrap().lock().unwrap();
158150
assert!(inner.idle_interval_ref.is_none(), "timer already spawned");
@@ -207,13 +199,11 @@ impl<T: Poolable, K: Key> Pool<T, K> {
207199
}
208200

209201
/* Used in client/tests.rs...
210-
#[cfg(feature = "runtime")]
211202
#[cfg(test)]
212203
pub(super) fn h1_key(&self, s: &str) -> Key {
213204
Arc::new(s.to_string())
214205
}
215206
216-
#[cfg(feature = "runtime")]
217207
#[cfg(test)]
218208
pub(super) fn idle_count(&self, key: &Key) -> usize {
219209
self
@@ -403,10 +393,7 @@ impl<T: Poolable, K: Key> PoolInner<T, K> {
403393
});
404394
}
405395

406-
#[cfg(feature = "runtime")]
407-
{
408-
self.spawn_idle_interval(__pool_ref);
409-
}
396+
self.spawn_idle_interval(__pool_ref);
410397
}
411398
None => trace!("put; found waiter for {:?}", key),
412399
}
@@ -423,8 +410,9 @@ impl<T: Poolable, K: Key> PoolInner<T, K> {
423410
self.waiters.remove(key);
424411
}
425412

426-
#[cfg(feature = "runtime")]
427-
fn spawn_idle_interval(&mut self, pool_ref: &Arc<Mutex<PoolInner<T, K>>>) {
413+
fn spawn_idle_interval(&mut self, _pool_ref: &Arc<Mutex<PoolInner<T, K>>>) {
414+
// TODO
415+
/*
428416
let (dur, rx) = {
429417
if self.idle_interval_ref.is_some() {
430418
return;
@@ -446,6 +434,7 @@ impl<T: Poolable, K: Key> PoolInner<T, K> {
446434
};
447435
448436
self.exec.execute(interval);
437+
*/
449438
}
450439
}
451440

@@ -466,7 +455,6 @@ impl<T, K: Eq + Hash> PoolInner<T, K> {
466455
}
467456
}
468457

469-
#[cfg(feature = "runtime")]
470458
impl<T: Poolable, K: Key> PoolInner<T, K> {
471459
/// This should *only* be called by the IdleTask
472460
fn clear_expired(&mut self) {
@@ -766,7 +754,7 @@ impl Expiration {
766754
}
767755
}
768756

769-
#[cfg(feature = "runtime")]
757+
/*
770758
pin_project_lite::pin_project! {
771759
struct IdleTask<T, K: Key> {
772760
#[pin]
@@ -780,7 +768,6 @@ pin_project_lite::pin_project! {
780768
}
781769
}
782770
783-
#[cfg(feature = "runtime")]
784771
impl<T: Poolable + 'static, K: Key> Future for IdleTask<T, K> {
785772
type Output = ();
786773
@@ -809,6 +796,7 @@ impl<T: Poolable + 'static, K: Key> Future for IdleTask<T, K> {
809796
}
810797
}
811798
}
799+
*/
812800

813801
impl<T> WeakOpt<T> {
814802
fn none() -> Self {
@@ -932,7 +920,6 @@ mod tests {
932920
assert!(is_not_ready);
933921
}
934922

935-
#[cfg(feature = "runtime")]
936923
#[tokio::test]
937924
async fn test_pool_checkout_removes_expired() {
938925
let pool = pool_no_timer();
@@ -971,7 +958,6 @@ mod tests {
971958
);
972959
}
973960

974-
#[cfg(feature = "runtime")]
975961
#[tokio::test]
976962
async fn test_pool_timer_removes_expired() {
977963
tokio::time::pause();

src/rt/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Runtime utilities
22
3-
/// Implementation of [`hyper::rt::Executor`] that utilises [`tokio::spawn`].
4-
pub mod tokio_executor;
5-
mod tokio_io;
3+
#[cfg(feature = "tokio")]
4+
pub mod tokio;
65

7-
pub use tokio_executor::TokioExecutor;
8-
pub use tokio_io::TokioIo;
6+
#[cfg(feature = "tokio")]
7+
pub use self::tokio::{TokioExecutor, TokioIo};

src/rt/tokio_io.rs renamed to src/rt/tokio.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#![allow(dead_code)]
22
//! Tokio IO integration for hyper
33
use std::{
4+
future::Future,
45
pin::Pin,
56
task::{Context, Poll},
67
};
78

9+
use hyper::rt::Executor;
810
use pin_project_lite::pin_project;
911

12+
/// Future executor that utilises `tokio` threads.
13+
#[non_exhaustive]
14+
#[derive(Default, Debug, Clone)]
15+
pub struct TokioExecutor {}
16+
1017
pin_project! {
1118
/// A wrapping implementing hyper IO traits for a type that
1219
/// implements Tokio's IO traits.
@@ -17,6 +24,27 @@ pin_project! {
1724
}
1825
}
1926

27+
// ===== impl TokioExecutor =====
28+
29+
impl<Fut> Executor<Fut> for TokioExecutor
30+
where
31+
Fut: Future + Send + 'static,
32+
Fut::Output: Send + 'static,
33+
{
34+
fn execute(&self, fut: Fut) {
35+
tokio::spawn(fut);
36+
}
37+
}
38+
39+
impl TokioExecutor {
40+
/// Create new executor that relies on [`tokio::spawn`] to execute futures.
41+
pub fn new() -> Self {
42+
Self {}
43+
}
44+
}
45+
46+
// ==== impl TokioIo =====
47+
2048
impl<T> TokioIo<T> {
2149
/// Wrap a type implementing Tokio's IO traits.
2250
pub fn new(inner: T) -> Self {
@@ -161,3 +189,21 @@ where
161189
hyper::rt::Write::poll_write_vectored(self.project().inner, cx, bufs)
162190
}
163191
}
192+
193+
#[cfg(test)]
194+
mod tests {
195+
use crate::rt::tokio_executor::TokioExecutor;
196+
use hyper::rt::Executor;
197+
use tokio::sync::oneshot;
198+
199+
#[cfg(not(miri))]
200+
#[tokio::test]
201+
async fn simple_execute() -> Result<(), Box<dyn std::error::Error>> {
202+
let (tx, rx) = oneshot::channel();
203+
let executor = TokioExecutor::new();
204+
executor.execute(async move {
205+
tx.send(()).unwrap();
206+
});
207+
rx.await.map_err(Into::into)
208+
}
209+
}

0 commit comments

Comments
 (0)