Skip to content

Commit

Permalink
fix(client): make ResponseFuture implement Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn authored and seanmonstar committed Oct 18, 2021
1 parent d0c6aaa commit bd6c35b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
29 changes: 20 additions & 9 deletions src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::pool::{
#[cfg(feature = "tcp")]
use super::HttpConnector;
use crate::body::{Body, HttpBody};
use crate::common::{exec::BoxSendFuture, lazy as hyper_lazy, task, Future, Lazy, Pin, Poll};
use crate::common::{exec::BoxSendFuture, sync_wrapper::SyncWrapper, lazy as hyper_lazy, task, Future, Lazy, Pin, Poll};
use crate::rt::Executor;

/// A Client to make outgoing HTTP requests.
Expand All @@ -45,7 +45,7 @@ struct Config {
/// This is returned by `Client::request` (and `Client::get`).
#[must_use = "futures do nothing unless polled"]
pub struct ResponseFuture {
inner: Pin<Box<dyn Future<Output = crate::Result<Response<Body>>> + Send>>,
inner: SyncWrapper<Pin<Box<dyn Future<Output = crate::Result<Response<Body>>> + Send>>>,
}

// ===== impl Client =====
Expand Down Expand Up @@ -168,9 +168,9 @@ where
Version::HTTP_10 => {
if is_http_connect {
warn!("CONNECT is not allowed for HTTP/1.0");
return ResponseFuture::new(Box::pin(future::err(
return ResponseFuture::new(future::err(
crate::Error::new_user_unsupported_request_method(),
)));
));
}
}
Version::HTTP_2 => (),
Expand All @@ -181,11 +181,11 @@ where
let pool_key = match extract_domain(req.uri_mut(), is_http_connect) {
Ok(s) => s,
Err(err) => {
return ResponseFuture::new(Box::pin(future::err(err)));
return ResponseFuture::new(future::err(err));
}
};

ResponseFuture::new(Box::pin(self.clone().retryably_send_request(req, pool_key)))
ResponseFuture::new(self.clone().retryably_send_request(req, pool_key))
}

async fn retryably_send_request(
Expand Down Expand Up @@ -580,8 +580,13 @@ impl<C, B> fmt::Debug for Client<C, B> {
// ===== impl ResponseFuture =====

impl ResponseFuture {
fn new(fut: Pin<Box<dyn Future<Output = crate::Result<Response<Body>>> + Send>>) -> Self {
Self { inner: fut }
fn new<F>(value: F) -> Self
where
F: Future<Output = crate::Result<Response<Body>>> + Send + 'static,
{
Self {
inner: SyncWrapper::new(Box::pin(value))
}
}

fn error_version(ver: Version) -> Self {
Expand All @@ -602,7 +607,7 @@ impl Future for ResponseFuture {
type Output = crate::Result<Response<Body>>;

fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.inner).poll(cx)
self.inner.get_mut().as_mut().poll(cx)
}
}

Expand Down Expand Up @@ -1276,6 +1281,12 @@ impl fmt::Debug for Builder {
mod unit_tests {
use super::*;

#[test]
fn response_future_is_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<ResponseFuture>();
}

#[test]
fn set_relative_uri_with_implicit_path() {
let mut uri = "http://hyper.rs".parse().unwrap();
Expand Down
5 changes: 4 additions & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ pub(crate) mod io;
#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
mod lazy;
mod never;
#[cfg(feature = "stream")]
#[cfg(any(
feature = "stream",
all(feature = "client", any(feature = "http1", feature = "http2"))
))]
pub(crate) mod sync_wrapper;
pub(crate) mod task;
pub(crate) mod watch;
Expand Down

0 comments on commit bd6c35b

Please sign in to comment.