Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace futures-util with atomic-waker and manual poll_fn #721

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ members = [
]

[dependencies]
atomic-waker = "1.0.0"
futures-core = { version = "0.3", default-features = false }
futures-sink = { version = "0.3", default-features = false }
futures-util = { version = "0.3", default-features = false }
tokio-util = { version = "0.7.1", features = ["codec", "io"] }
tokio = { version = "1", features = ["io-util"] }
bytes = "1"
Expand Down
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ impl ResponseFuture {
impl PushPromises {
/// Get the next `PushPromise`.
pub async fn push_promise(&mut self) -> Option<Result<PushPromise, crate::Error>> {
futures_util::future::poll_fn(move |cx| self.poll_push_promise(cx)).await
crate::poll_fn(move |cx| self.poll_push_promise(cx)).await
}

#[doc(hidden)]
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,25 @@ pub use crate::share::{FlowControl, Ping, PingPong, Pong, RecvStream, SendStream

#[cfg(feature = "unstable")]
pub use codec::{Codec, SendError, UserError};

use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

/// Creates a future from a function that returns `Poll`.
fn poll_fn<T, F: FnMut(&mut Context<'_>) -> T>(f: F) -> PollFn<F> {
PollFn(f)
}

/// The future created by `poll_fn`.
struct PollFn<F>(F);

impl<F> Unpin for PollFn<F> {}

impl<T, F: FnMut(&mut Context<'_>) -> Poll<T>> Future for PollFn<F> {
type Output = T;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
(self.0)(cx)
}
}
2 changes: 1 addition & 1 deletion src/proto/ping_pong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::codec::Codec;
use crate::frame::Ping;
use crate::proto::{self, PingPayload};

use atomic_waker::AtomicWaker;
use bytes::Buf;
use futures_util::task::AtomicWaker;
use std::io;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ where
pub async fn accept(
&mut self,
) -> Option<Result<(Request<RecvStream>, SendResponse<B>), crate::Error>> {
futures_util::future::poll_fn(move |cx| self.poll_accept(cx)).await
crate::poll_fn(move |cx| self.poll_accept(cx)).await
}

#[doc(hidden)]
Expand Down
6 changes: 3 additions & 3 deletions src/share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ impl RecvStream {

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

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

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

#[doc(hidden)]
Expand Down