Skip to content

Commit b958711

Browse files
2000andreaAquilaIrreale
authored andcommitted
Wrap hyper's GaiResolver in a newtype
This allows to implement the Default trait for the GaiResolver. Using this newtype instead of hyper's GaiResolver permits to have a uniform interface where RestClient/Builder require for resolver types to implement the Default trait for internal default resolver construction.
1 parent 6b376c3 commit b958711

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ use hyper_tls::HttpsConnector;
5252
#[cfg(feature = "rustls")]
5353
use hyper_rustls::{HttpsConnector, HttpsConnectorBuilder};
5454

55+
use resolvers::GaiResolver;
56+
57+
pub mod resolvers;
5558
#[cfg(feature = "blocking")]
5659
pub mod blocking;
5760

@@ -121,7 +124,7 @@ impl<T> Deref for Response<T> {
121124
}
122125

123126
/// REST client to make HTTP GET and POST requests.
124-
pub struct RestClient<R = dns::GaiResolver> {
127+
pub struct RestClient<R = GaiResolver> {
125128
client: HyperClient<R>,
126129
baseurl: url::Url,
127130
auth: Option<String>,
@@ -170,7 +173,7 @@ pub enum Error {
170173
}
171174

172175
/// Builder for `RestClient`
173-
pub struct Builder<R = dns::GaiResolver> {
176+
pub struct Builder<R = GaiResolver> {
174177
/// Request timeout
175178
timeout: Duration,
176179

src/resolvers.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use core::task::{Context, Poll};
2+
3+
use hyper::client::connect::dns::{self, GaiResolver as HyperGaiResolver};
4+
use hyper::service::Service;
5+
6+
/// Newtype wrapper around hyper's GaiResolver to provide Default
7+
/// trait implementation
8+
#[derive(Clone, Debug)]
9+
pub struct GaiResolver(HyperGaiResolver);
10+
11+
impl GaiResolver {
12+
pub fn new() -> Self {
13+
Self::default()
14+
}
15+
}
16+
17+
impl From<HyperGaiResolver> for GaiResolver {
18+
fn from(gai: HyperGaiResolver) -> Self {
19+
Self(gai)
20+
}
21+
}
22+
23+
impl Into<HyperGaiResolver> for GaiResolver {
24+
fn into(self) -> HyperGaiResolver {
25+
self.0
26+
}
27+
}
28+
29+
impl Default for GaiResolver {
30+
fn default() -> Self {
31+
Self(HyperGaiResolver::new())
32+
}
33+
}
34+
35+
impl Service<dns::Name> for GaiResolver {
36+
type Response = <HyperGaiResolver as Service<dns::Name>>::Response;
37+
type Error = <HyperGaiResolver as Service<dns::Name>>::Error;
38+
type Future = <HyperGaiResolver as Service<dns::Name>>::Future;
39+
40+
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
41+
self.0.poll_ready(cx)
42+
}
43+
44+
fn call(&mut self, name: dns::Name) -> Self::Future {
45+
self.0.call(name)
46+
}
47+
}

0 commit comments

Comments
 (0)