Skip to content

Commit 6e8417e

Browse files
committed
refactor(lib): use type macro to detect impl Trait
1 parent 9f8add6 commit 6e8417e

File tree

7 files changed

+36
-77
lines changed

7 files changed

+36
-77
lines changed

build.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ extern crate rustc_version;
22
use rustc_version::{version, Version};
33

44
fn main() {
5-
65
// Check for a minimum version to see if new rust features can be used
76
let version = version().unwrap();
87
if version >= Version::parse("1.26.0").unwrap() {
9-
println!("cargo:rustc-cfg=impl_trait_available");
8+
println!("cargo:rustc-cfg=__hyper_impl_trait_available");
109
}
1110
if version >= Version::parse("1.23.0").unwrap() {
12-
println!("cargo:rustc-cfg=inherent_ascii");
11+
println!("cargo:rustc-cfg=__hyper_inherent_ascii");
1312
}
1413
}

src/client/conn.rs

+6-58
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,11 @@ where
238238
}
239239
}
240240

241-
#[cfg(impl_trait_available)]
242-
pub(crate) fn send_request_retryable(&mut self, req: Request<B>) -> impl Future<Item = Response<Body>, Error = (::Error, Option<Request<B>>)> + Send
241+
pub(crate) fn send_request_retryable(&mut self, req: Request<B>) -> impl_trait!(ty: Future<Item = Response<Body>, Error = (::Error, Option<Request<B>>)> + Send)
243242
where
244243
B: Send,
245244
{
246-
match self.dispatch.try_send(req) {
245+
impl_trait!(e: match self.dispatch.try_send(req) {
247246
Ok(rx) => {
248247
Either::A(rx.then(move |res| {
249248
match res {
@@ -259,32 +258,7 @@ where
259258
let err = ::Error::new_canceled(Some("connection was not ready"));
260259
Either::B(future::err((err, Some(req))))
261260
}
262-
}
263-
}
264-
265-
#[cfg(not(impl_trait_available))]
266-
pub(super) fn send_request_retryable(&mut self, req: Request<B>) -> Box<Future<Item=Response<Body>, Error=(::Error, Option<Request<B>>)> + Send>
267-
where
268-
B: Send,
269-
{
270-
let inner = match self.dispatch.try_send(req) {
271-
Ok(rx) => {
272-
Either::A(rx.then(move |res| {
273-
match res {
274-
Ok(Ok(res)) => Ok(res),
275-
Ok(Err(err)) => Err(err),
276-
// this is definite bug if it happens, but it shouldn't happen!
277-
Err(_) => panic!("dispatch dropped without returning error"),
278-
}
279-
}))
280-
},
281-
Err(req) => {
282-
debug!("connection was not ready");
283-
let err = ::Error::new_canceled(Some("connection was not ready"));
284-
Either::B(future::err((err, Some(req))))
285-
}
286-
};
287-
Box::new(inner)
261+
})
288262
}
289263
}
290264

@@ -324,12 +298,11 @@ impl<B> Http2SendRequest<B>
324298
where
325299
B: Payload + 'static,
326300
{
327-
#[cfg(impl_trait_available)]
328-
pub(super) fn send_request_retryable(&mut self, req: Request<B>) -> impl Future<Item=Response<Body>, Error=(::Error, Option<Request<B>>)> + Send
301+
pub(super) fn send_request_retryable(&mut self, req: Request<B>) -> impl_trait!(ty: Future<Item=Response<Body>, Error=(::Error, Option<Request<B>>)> + Send)
329302
where
330303
B: Send,
331304
{
332-
match self.dispatch.try_send(req) {
305+
impl_trait!(e: match self.dispatch.try_send(req) {
333306
Ok(rx) => {
334307
Either::A(rx.then(move |res| {
335308
match res {
@@ -345,32 +318,7 @@ where
345318
let err = ::Error::new_canceled(Some("connection was not ready"));
346319
Either::B(future::err((err, Some(req))))
347320
}
348-
}
349-
}
350-
351-
#[cfg(not(impl_trait_available))]
352-
pub(crate) fn send_request_retryable(&mut self, req: Request<B>) -> Box<Future<Item=Response<Body>, Error=(::Error, Option<Request<B>>)> + Send>
353-
where
354-
B: Send,
355-
{
356-
let inner = match self.dispatch.try_send(req) {
357-
Ok(rx) => {
358-
Either::A(rx.then(move |res| {
359-
match res {
360-
Ok(Ok(res)) => Ok(res),
361-
Ok(Err(err)) => Err(err),
362-
// this is definite bug if it happens, but it shouldn't happen!
363-
Err(_) => panic!("dispatch dropped without returning error"),
364-
}
365-
}))
366-
},
367-
Err(req) => {
368-
debug!("connection was not ready");
369-
let err = ::Error::new_canceled(Some("connection was not ready"));
370-
Either::B(future::err((err, Some(req))))
371-
}
372-
};
373-
Box::new(inner)
321+
})
374322
}
375323
}
376324

src/client/mod.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -623,26 +623,14 @@ impl<B> PoolClient<B> {
623623
}
624624

625625
impl<B: Payload + 'static> PoolClient<B> {
626-
#[cfg(impl_trait_available)]
627-
fn send_request_retryable(&mut self, req: Request<B>) -> impl Future<Item = Response<Body>, Error = (::Error, Option<Request<B>>)> + Send
626+
fn send_request_retryable(&mut self, req: Request<B>) -> impl_trait!(ty: Future<Item = Response<Body>, Error = (::Error, Option<Request<B>>)> + Send)
628627
where
629628
B: Send,
630629
{
631-
match self.tx {
630+
impl_trait!(e: match self.tx {
632631
PoolTx::Http1(ref mut tx) => Either::A(tx.send_request_retryable(req)),
633632
PoolTx::Http2(ref mut tx) => Either::B(tx.send_request_retryable(req)),
634-
}
635-
}
636-
637-
#[cfg(not(impl_trait_available))]
638-
fn send_request_retryable(&mut self, req: Request<B>) -> Box<Future<Item=Response<Body>, Error=(::Error, Option<Request<B>>)> + Send>
639-
where
640-
B: Send,
641-
{
642-
match self.tx {
643-
PoolTx::Http1(ref mut tx) => tx.send_request_retryable(req),
644-
PoolTx::Http2(ref mut tx) => tx.send_request_retryable(req),
645-
}
633+
})
646634
}
647635
}
648636

src/common/macros.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#[macro_export]
2+
#[cfg(__hyper_impl_trait_available)]
3+
macro_rules! impl_trait {
4+
(ty: $($t:tt)+) => {
5+
impl $($t)+
6+
};
7+
(e: $e:expr) => {
8+
$e
9+
}
10+
}
11+
12+
#[macro_export]
13+
#[cfg(not(__hyper_impl_trait_available))]
14+
macro_rules! impl_trait {
15+
(ty: $($t:tt)+) => {
16+
Box<$($t)+>
17+
};
18+
(e: $e:expr) => {
19+
Box::new($e)
20+
}
21+
}

src/common/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ mod buf;
22
mod exec;
33
pub(crate) mod io;
44
mod lazy;
5+
#[macro_use]
6+
mod macros;
57
mod never;
68

79
pub(crate) use self::buf::StaticBuf;

src/headers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bytes::BytesMut;
22
use http::HeaderMap;
33
use http::header::{CONTENT_LENGTH, TRANSFER_ENCODING};
44
use http::header::{HeaderValue, OccupiedEntry, ValueIter};
5-
#[cfg(not(inherent_ascii))]
5+
#[cfg(not(__hyper_inherent_ascii))]
66
use std::ascii::AsciiExt;
77

88
pub fn connection_keep_alive(value: &HeaderValue) -> bool {

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub use error::{Result, Error};
5252
pub use body::{Body, Chunk};
5353
pub use server::Server;
5454

55+
#[macro_use]
5556
mod common;
5657
#[cfg(test)]
5758
mod mock;

0 commit comments

Comments
 (0)