diff --git a/src/client/mod.rs b/src/client/mod.rs index ea84664fb4..1e537e95fe 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -151,42 +151,42 @@ impl Client { } /// Build a Get request. - pub fn get(&self, url: U) -> RequestBuilder { + pub fn get(&self, url: U) -> RequestBuilder { self.request(Method::Get, url) } /// Build a Head request. - pub fn head(&self, url: U) -> RequestBuilder { + pub fn head(&self, url: U) -> RequestBuilder { self.request(Method::Head, url) } /// Build a Patch request. - pub fn patch(&self, url: U) -> RequestBuilder { + pub fn patch(&self, url: U) -> RequestBuilder { self.request(Method::Patch, url) } /// Build a Post request. - pub fn post(&self, url: U) -> RequestBuilder { + pub fn post(&self, url: U) -> RequestBuilder { self.request(Method::Post, url) } /// Build a Put request. - pub fn put(&self, url: U) -> RequestBuilder { + pub fn put(&self, url: U) -> RequestBuilder { self.request(Method::Put, url) } /// Build a Delete request. - pub fn delete(&self, url: U) -> RequestBuilder { + pub fn delete(&self, url: U) -> RequestBuilder { self.request(Method::Delete, url) } /// Build a new request using this Client. - pub fn request(&self, method: Method, url: U) -> RequestBuilder { + pub fn request(&self, method: Method, url: U) -> RequestBuilder { RequestBuilder { client: self, method: method, - url: url, + url: url.into_url(), body: None, headers: None, } @@ -201,30 +201,39 @@ impl Default for Client { /// /// One of these will be built for you if you use one of the convenience /// methods, such as `get()`, `post()`, etc. -pub struct RequestBuilder<'a, U: IntoUrl> { +pub struct RequestBuilder<'a> { client: &'a Client, - url: U, + // We store a result here because it's good to keep RequestBuilder + // from being generic, but it is a nicer API to report the error + // from `send` (when other errors may be happening, so it already + // returns a `Result`). Why's it good to keep it non-generic? It + // stops downstream crates having to remonomorphise and recompile + // the code, which can take a while, since `send` is fairly large. + // (For an extreme example, a tiny crate containing + // `hyper::Client::new().get("x").send().unwrap();` took ~4s to + // compile with a generic RequestBuilder, but 2s with this scheme,) + url: Result, headers: Option, method: Method, body: Option>, } -impl<'a, U: IntoUrl> RequestBuilder<'a, U> { +impl<'a> RequestBuilder<'a> { /// Set a request body to be sent. - pub fn body>>(mut self, body: B) -> RequestBuilder<'a, U> { + pub fn body>>(mut self, body: B) -> RequestBuilder<'a> { self.body = Some(body.into()); self } /// Add additional headers to the request. - pub fn headers(mut self, headers: Headers) -> RequestBuilder<'a, U> { + pub fn headers(mut self, headers: Headers) -> RequestBuilder<'a> { self.headers = Some(headers); self } /// Add an individual new header to the request. - pub fn header(mut self, header: H) -> RequestBuilder<'a, U> { + pub fn header(mut self, header: H) -> RequestBuilder<'a> { { let mut headers = match self.headers { Some(ref mut h) => h, @@ -242,7 +251,7 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> { /// Execute this request and receive a Response back. pub fn send(self) -> ::Result { let RequestBuilder { client, method, url, headers, body } = self; - let mut url = try!(url.into_url()); + let mut url = try!(url); trace!("send {:?} {:?}", method, url); let can_have_body = match &method {