Skip to content

Commit

Permalink
chore(client): place the use of new rust features behind cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
nivkner authored and seanmonstar committed Aug 8, 2018
1 parent 97f4243 commit 9f8add6
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[package]

name = "hyper"
version = "0.12.7" # don't forget to update html_root_url
description = "A fast and correct HTTP library."
Expand Down Expand Up @@ -50,6 +49,9 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"

[build-dependencies]
rustc_version = "0.2.3"

[features]
default = [
"__internal_flaky_tests",
Expand Down
14 changes: 14 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
extern crate rustc_version;
use rustc_version::{version, Version};

fn main() {

// Check for a minimum version to see if new rust features can be used
let version = version().unwrap();
if version >= Version::parse("1.26.0").unwrap() {
println!("cargo:rustc-cfg=impl_trait_available");
}
if version >= Version::parse("1.23.0").unwrap() {
println!("cargo:rustc-cfg=inherent_ascii");
}
}
52 changes: 52 additions & 0 deletions src/client/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ where
}
}

#[cfg(impl_trait_available)]
pub(crate) fn send_request_retryable(&mut self, req: Request<B>) -> impl Future<Item = Response<Body>, Error = (::Error, Option<Request<B>>)> + Send
where
B: Send,
Expand All @@ -260,6 +261,31 @@ where
}
}
}

#[cfg(not(impl_trait_available))]
pub(super) fn send_request_retryable(&mut self, req: Request<B>) -> Box<Future<Item=Response<Body>, Error=(::Error, Option<Request<B>>)> + Send>
where
B: Send,
{
let inner = match self.dispatch.try_send(req) {
Ok(rx) => {
Either::A(rx.then(move |res| {
match res {
Ok(Ok(res)) => Ok(res),
Ok(Err(err)) => Err(err),
// this is definite bug if it happens, but it shouldn't happen!
Err(_) => panic!("dispatch dropped without returning error"),
}
}))
},
Err(req) => {
debug!("connection was not ready");
let err = ::Error::new_canceled(Some("connection was not ready"));
Either::B(future::err((err, Some(req))))
}
};
Box::new(inner)
}
}

/* TODO(0.12.0): when we change from tokio-service to tower.
Expand Down Expand Up @@ -298,6 +324,7 @@ impl<B> Http2SendRequest<B>
where
B: Payload + 'static,
{
#[cfg(impl_trait_available)]
pub(super) fn send_request_retryable(&mut self, req: Request<B>) -> impl Future<Item=Response<Body>, Error=(::Error, Option<Request<B>>)> + Send
where
B: Send,
Expand All @@ -320,6 +347,31 @@ where
}
}
}

#[cfg(not(impl_trait_available))]
pub(crate) fn send_request_retryable(&mut self, req: Request<B>) -> Box<Future<Item=Response<Body>, Error=(::Error, Option<Request<B>>)> + Send>
where
B: Send,
{
let inner = match self.dispatch.try_send(req) {
Ok(rx) => {
Either::A(rx.then(move |res| {
match res {
Ok(Ok(res)) => Ok(res),
Ok(Err(err)) => Err(err),
// this is definite bug if it happens, but it shouldn't happen!
Err(_) => panic!("dispatch dropped without returning error"),
}
}))
},
Err(req) => {
debug!("connection was not ready");
let err = ::Error::new_canceled(Some("connection was not ready"));
Either::B(future::err((err, Some(req))))
}
};
Box::new(inner)
}
}

impl<B> fmt::Debug for Http2SendRequest<B> {
Expand Down
12 changes: 12 additions & 0 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ impl<B> PoolClient<B> {
}

impl<B: Payload + 'static> PoolClient<B> {
#[cfg(impl_trait_available)]
fn send_request_retryable(&mut self, req: Request<B>) -> impl Future<Item = Response<Body>, Error = (::Error, Option<Request<B>>)> + Send
where
B: Send,
Expand All @@ -632,6 +633,17 @@ impl<B: Payload + 'static> PoolClient<B> {
PoolTx::Http2(ref mut tx) => Either::B(tx.send_request_retryable(req)),
}
}

#[cfg(not(impl_trait_available))]
fn send_request_retryable(&mut self, req: Request<B>) -> Box<Future<Item=Response<Body>, Error=(::Error, Option<Request<B>>)> + Send>
where
B: Send,
{
match self.tx {
PoolTx::Http1(ref mut tx) => tx.send_request_retryable(req),
PoolTx::Http2(ref mut tx) => tx.send_request_retryable(req),
}
}
}

impl<B> Poolable for PoolClient<B>
Expand Down
2 changes: 2 additions & 0 deletions src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use bytes::BytesMut;
use http::HeaderMap;
use http::header::{CONTENT_LENGTH, TRANSFER_ENCODING};
use http::header::{HeaderValue, OccupiedEntry, ValueIter};
#[cfg(not(inherent_ascii))]
use std::ascii::AsciiExt;

pub fn connection_keep_alive(value: &HeaderValue) -> bool {
connection_has(value, "keep-alive")
Expand Down

0 comments on commit 9f8add6

Please sign in to comment.