Skip to content

Commit

Permalink
refactor(lib): use type macro to detect impl Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Aug 8, 2018
1 parent 9f8add6 commit 6e8417e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 77 deletions.
5 changes: 2 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ 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");
println!("cargo:rustc-cfg=__hyper_impl_trait_available");
}
if version >= Version::parse("1.23.0").unwrap() {
println!("cargo:rustc-cfg=inherent_ascii");
println!("cargo:rustc-cfg=__hyper_inherent_ascii");
}
}
64 changes: 6 additions & 58 deletions src/client/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,11 @@ 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
pub(crate) fn send_request_retryable(&mut self, req: Request<B>) -> impl_trait!(ty: Future<Item = Response<Body>, Error = (::Error, Option<Request<B>>)> + Send)
where
B: Send,
{
match self.dispatch.try_send(req) {
impl_trait!(e: match self.dispatch.try_send(req) {
Ok(rx) => {
Either::A(rx.then(move |res| {
match res {
Expand All @@ -259,32 +258,7 @@ where
let err = ::Error::new_canceled(Some("connection was not ready"));
Either::B(future::err((err, Some(req))))
}
}
}

#[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)
})
}
}

Expand Down Expand Up @@ -324,12 +298,11 @@ 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
pub(super) fn send_request_retryable(&mut self, req: Request<B>) -> impl_trait!(ty: Future<Item=Response<Body>, Error=(::Error, Option<Request<B>>)> + Send)
where
B: Send,
{
match self.dispatch.try_send(req) {
impl_trait!(e: match self.dispatch.try_send(req) {
Ok(rx) => {
Either::A(rx.then(move |res| {
match res {
Expand All @@ -345,32 +318,7 @@ where
let err = ::Error::new_canceled(Some("connection was not ready"));
Either::B(future::err((err, Some(req))))
}
}
}

#[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)
})
}
}

Expand Down
18 changes: 3 additions & 15 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,26 +623,14 @@ 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
fn send_request_retryable(&mut self, req: Request<B>) -> impl_trait!(ty: Future<Item = Response<Body>, Error = (::Error, Option<Request<B>>)> + Send)
where
B: Send,
{
match self.tx {
impl_trait!(e: match self.tx {
PoolTx::Http1(ref mut tx) => Either::A(tx.send_request_retryable(req)),
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),
}
})
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/common/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#[macro_export]
#[cfg(__hyper_impl_trait_available)]
macro_rules! impl_trait {
(ty: $($t:tt)+) => {
impl $($t)+
};
(e: $e:expr) => {
$e
}
}

#[macro_export]
#[cfg(not(__hyper_impl_trait_available))]
macro_rules! impl_trait {
(ty: $($t:tt)+) => {
Box<$($t)+>
};
(e: $e:expr) => {
Box::new($e)
}
}
2 changes: 2 additions & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ mod buf;
mod exec;
pub(crate) mod io;
mod lazy;
#[macro_use]
mod macros;
mod never;

pub(crate) use self::buf::StaticBuf;
Expand Down
2 changes: 1 addition & 1 deletion src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bytes::BytesMut;
use http::HeaderMap;
use http::header::{CONTENT_LENGTH, TRANSFER_ENCODING};
use http::header::{HeaderValue, OccupiedEntry, ValueIter};
#[cfg(not(inherent_ascii))]
#[cfg(not(__hyper_inherent_ascii))]
use std::ascii::AsciiExt;

pub fn connection_keep_alive(value: &HeaderValue) -> bool {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub use error::{Result, Error};
pub use body::{Body, Chunk};
pub use server::Server;

#[macro_use]
mod common;
#[cfg(test)]
mod mock;
Expand Down

0 comments on commit 6e8417e

Please sign in to comment.