Skip to content

Commit 3c41151

Browse files
authored
Replace futures-util with atomic-waker and manual poll_fn (#721)
This removes ones of the heavier dependencies. The goal is to also remove futures-util from the rest of the dependency tree, which is hard to justify when futures-util is used in more fundamental dependencies.
1 parent c83b2d5 commit 3c41151

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ members = [
3939
]
4040

4141
[dependencies]
42+
atomic-waker = "1.0.0"
4243
futures-core = { version = "0.3", default-features = false }
4344
futures-sink = { version = "0.3", default-features = false }
44-
futures-util = { version = "0.3", default-features = false }
4545
tokio-util = { version = "0.7.1", features = ["codec", "io"] }
4646
tokio = { version = "1", features = ["io-util"] }
4747
bytes = "1"

src/client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,7 @@ impl ResponseFuture {
14921492
impl PushPromises {
14931493
/// Get the next `PushPromise`.
14941494
pub async fn push_promise(&mut self) -> Option<Result<PushPromise, crate::Error>> {
1495-
futures_util::future::poll_fn(move |cx| self.poll_push_promise(cx)).await
1495+
crate::poll_fn(move |cx| self.poll_push_promise(cx)).await
14961496
}
14971497

14981498
#[doc(hidden)]

src/lib.rs

+22
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,25 @@ pub use crate::share::{FlowControl, Ping, PingPong, Pong, RecvStream, SendStream
139139

140140
#[cfg(feature = "unstable")]
141141
pub use codec::{Codec, SendError, UserError};
142+
143+
use std::future::Future;
144+
use std::pin::Pin;
145+
use std::task::{Context, Poll};
146+
147+
/// Creates a future from a function that returns `Poll`.
148+
fn poll_fn<T, F: FnMut(&mut Context<'_>) -> T>(f: F) -> PollFn<F> {
149+
PollFn(f)
150+
}
151+
152+
/// The future created by `poll_fn`.
153+
struct PollFn<F>(F);
154+
155+
impl<F> Unpin for PollFn<F> {}
156+
157+
impl<T, F: FnMut(&mut Context<'_>) -> Poll<T>> Future for PollFn<F> {
158+
type Output = T;
159+
160+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
161+
(self.0)(cx)
162+
}
163+
}

src/proto/ping_pong.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use crate::codec::Codec;
22
use crate::frame::Ping;
33
use crate::proto::{self, PingPayload};
44

5+
use atomic_waker::AtomicWaker;
56
use bytes::Buf;
6-
use futures_util::task::AtomicWaker;
77
use std::io;
88
use std::sync::atomic::{AtomicUsize, Ordering};
99
use std::sync::Arc;

src/server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ where
413413
pub async fn accept(
414414
&mut self,
415415
) -> Option<Result<(Request<RecvStream>, SendResponse<B>), crate::Error>> {
416-
futures_util::future::poll_fn(move |cx| self.poll_accept(cx)).await
416+
crate::poll_fn(move |cx| self.poll_accept(cx)).await
417417
}
418418

419419
#[doc(hidden)]

src/share.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,12 @@ impl RecvStream {
410410

411411
/// Get the next data frame.
412412
pub async fn data(&mut self) -> Option<Result<Bytes, crate::Error>> {
413-
futures_util::future::poll_fn(move |cx| self.poll_data(cx)).await
413+
crate::poll_fn(move |cx| self.poll_data(cx)).await
414414
}
415415

416416
/// Get optional trailers for this stream.
417417
pub async fn trailers(&mut self) -> Result<Option<HeaderMap>, crate::Error> {
418-
futures_util::future::poll_fn(move |cx| self.poll_trailers(cx)).await
418+
crate::poll_fn(move |cx| self.poll_trailers(cx)).await
419419
}
420420

421421
/// Poll for the next data frame.
@@ -549,7 +549,7 @@ impl PingPong {
549549
/// Send a PING frame and wait for the peer to send the pong.
550550
pub async fn ping(&mut self, ping: Ping) -> Result<Pong, crate::Error> {
551551
self.send_ping(ping)?;
552-
futures_util::future::poll_fn(|cx| self.poll_pong(cx)).await
552+
crate::poll_fn(|cx| self.poll_pong(cx)).await
553553
}
554554

555555
#[doc(hidden)]

0 commit comments

Comments
 (0)