Skip to content

Commit

Permalink
refactor: Explicit future for trait Connection::when_ready
Browse files Browse the repository at this point in the history
Named future for the when-ready method which skips the use of the `poll_fn` combinator (which becomes un-nameable because of the closure) and replaces it with an explicit struct that impls Future.
  • Loading branch information
alexrudy committed Oct 8, 2024
1 parent 63645db commit 243bbcc
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/client/conn/connection.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Connections are responsible for sending and receiving HTTP requests and responses
//! over an arbitrary two-way stream of bytes.
use std::{fmt, future::Future};
use std::{fmt, future::Future, pin::Pin, task::Poll};

use futures_core::future::BoxFuture;
use futures_util::FutureExt as _;
use http_body::Body as HttpBody;
use hyper::body::Incoming;
use thiserror::Error;
Expand Down Expand Up @@ -38,17 +37,47 @@ pub trait Connection<B> {
) -> std::task::Poll<Result<(), Self::Error>>;

/// Future which resolves when the connection is ready to accept a new request.
fn when_ready(&mut self) -> BoxFuture<'_, Result<(), Self::Error>>
where
Self: Send,
{
futures_util::future::poll_fn(|cx| self.poll_ready(cx)).boxed()
fn when_ready(&mut self) -> WhenReady<'_, Self, B> {
WhenReady::new(self)
}

/// What HTTP version is this connection using?
fn version(&self) -> ::http::Version;
}

/// A future which resolves when the connection is ready again
#[derive(Debug)]
pub struct WhenReady<'a, C, B>
where
C: Connection<B> + ?Sized,
{
conn: &'a mut C,
_private: std::marker::PhantomData<fn(B)>,
}

impl<'a, C, B> WhenReady<'a, C, B>
where
C: Connection<B> + ?Sized,
{
pub(crate) fn new(conn: &'a mut C) -> Self {
Self {
conn,
_private: std::marker::PhantomData,
}
}
}

impl<'a, C, B> Future for WhenReady<'a, C, B>
where
C: Connection<B> + ?Sized,
{
type Output = Result<(), C::Error>;

fn poll(mut self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
self.conn.poll_ready(cx)
}
}

/// An HTTP connection.
pub struct HttpConnection<B> {
inner: InnerConnection<B>,
Expand Down

0 comments on commit 243bbcc

Please sign in to comment.