Skip to content

Commit 292b4e6

Browse files
committed
Merge pull request #664 from huonw/less-genericity
refactor(client): make RequestBuilder non-generic
2 parents b524adf + ff4a607 commit 292b4e6

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

Diff for: src/client/mod.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -151,42 +151,42 @@ impl Client {
151151
}
152152

153153
/// Build a Get request.
154-
pub fn get<U: IntoUrl>(&self, url: U) -> RequestBuilder<U> {
154+
pub fn get<U: IntoUrl>(&self, url: U) -> RequestBuilder {
155155
self.request(Method::Get, url)
156156
}
157157

158158
/// Build a Head request.
159-
pub fn head<U: IntoUrl>(&self, url: U) -> RequestBuilder<U> {
159+
pub fn head<U: IntoUrl>(&self, url: U) -> RequestBuilder {
160160
self.request(Method::Head, url)
161161
}
162162

163163
/// Build a Patch request.
164-
pub fn patch<U: IntoUrl>(&self, url: U) -> RequestBuilder<U> {
164+
pub fn patch<U: IntoUrl>(&self, url: U) -> RequestBuilder {
165165
self.request(Method::Patch, url)
166166
}
167167

168168
/// Build a Post request.
169-
pub fn post<U: IntoUrl>(&self, url: U) -> RequestBuilder<U> {
169+
pub fn post<U: IntoUrl>(&self, url: U) -> RequestBuilder {
170170
self.request(Method::Post, url)
171171
}
172172

173173
/// Build a Put request.
174-
pub fn put<U: IntoUrl>(&self, url: U) -> RequestBuilder<U> {
174+
pub fn put<U: IntoUrl>(&self, url: U) -> RequestBuilder {
175175
self.request(Method::Put, url)
176176
}
177177

178178
/// Build a Delete request.
179-
pub fn delete<U: IntoUrl>(&self, url: U) -> RequestBuilder<U> {
179+
pub fn delete<U: IntoUrl>(&self, url: U) -> RequestBuilder {
180180
self.request(Method::Delete, url)
181181
}
182182

183183

184184
/// Build a new request using this Client.
185-
pub fn request<U: IntoUrl>(&self, method: Method, url: U) -> RequestBuilder<U> {
185+
pub fn request<U: IntoUrl>(&self, method: Method, url: U) -> RequestBuilder {
186186
RequestBuilder {
187187
client: self,
188188
method: method,
189-
url: url,
189+
url: url.into_url(),
190190
body: None,
191191
headers: None,
192192
}
@@ -201,30 +201,39 @@ impl Default for Client {
201201
///
202202
/// One of these will be built for you if you use one of the convenience
203203
/// methods, such as `get()`, `post()`, etc.
204-
pub struct RequestBuilder<'a, U: IntoUrl> {
204+
pub struct RequestBuilder<'a> {
205205
client: &'a Client,
206-
url: U,
206+
// We store a result here because it's good to keep RequestBuilder
207+
// from being generic, but it is a nicer API to report the error
208+
// from `send` (when other errors may be happening, so it already
209+
// returns a `Result`). Why's it good to keep it non-generic? It
210+
// stops downstream crates having to remonomorphise and recompile
211+
// the code, which can take a while, since `send` is fairly large.
212+
// (For an extreme example, a tiny crate containing
213+
// `hyper::Client::new().get("x").send().unwrap();` took ~4s to
214+
// compile with a generic RequestBuilder, but 2s with this scheme,)
215+
url: Result<Url, UrlError>,
207216
headers: Option<Headers>,
208217
method: Method,
209218
body: Option<Body<'a>>,
210219
}
211220

212-
impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
221+
impl<'a> RequestBuilder<'a> {
213222

214223
/// Set a request body to be sent.
215-
pub fn body<B: Into<Body<'a>>>(mut self, body: B) -> RequestBuilder<'a, U> {
224+
pub fn body<B: Into<Body<'a>>>(mut self, body: B) -> RequestBuilder<'a> {
216225
self.body = Some(body.into());
217226
self
218227
}
219228

220229
/// Add additional headers to the request.
221-
pub fn headers(mut self, headers: Headers) -> RequestBuilder<'a, U> {
230+
pub fn headers(mut self, headers: Headers) -> RequestBuilder<'a> {
222231
self.headers = Some(headers);
223232
self
224233
}
225234

226235
/// Add an individual new header to the request.
227-
pub fn header<H: Header + HeaderFormat>(mut self, header: H) -> RequestBuilder<'a, U> {
236+
pub fn header<H: Header + HeaderFormat>(mut self, header: H) -> RequestBuilder<'a> {
228237
{
229238
let mut headers = match self.headers {
230239
Some(ref mut h) => h,
@@ -242,7 +251,7 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
242251
/// Execute this request and receive a Response back.
243252
pub fn send(self) -> ::Result<Response> {
244253
let RequestBuilder { client, method, url, headers, body } = self;
245-
let mut url = try!(url.into_url());
254+
let mut url = try!(url);
246255
trace!("send {:?} {:?}", method, url);
247256

248257
let can_have_body = match &method {

0 commit comments

Comments
 (0)