From 718d9457535d47c5c1f04d8d7a7bdff07ff0bad5 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Tue, 26 Feb 2019 14:33:02 +0100 Subject: [PATCH] refactor: Use `mut self`-consuming builder functions Writing the functions this way makes it slightly nicer to chain them without having to assign the request to an intermediate variable. --- src/lib.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 88e47c0bf..b04f0cec3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,28 +78,27 @@ impl <'a> Request<'a> { } /// Add a header to a request. - pub fn header(&mut self, k: &str, v: &str) -> CurlResult<&mut Self> { + pub fn header(mut self, k: &str, v: &str) -> CurlResult { self.headers.append(&format!("{}: {}", k, v))?; Ok(self) } /// Set the User-Agent for this request. - pub fn user_agent(&mut self, agent: &str) -> CurlResult<&mut Self> { + pub fn user_agent<'b: 'a>(mut self, agent: &str) -> CurlResult { self.handle.useragent(agent)?; Ok(self) } /// Add a byte-array body to a request using the specified /// Content-Type. - pub fn body(&'a mut self, content_type: &'a str, data: &'a [u8]) -> &mut Self { + pub fn body(mut self, content_type: &'a str, data: &'a [u8]) -> Self { self.body = Body::Bytes { data, content_type }; self } /// Add a JSON-encoded body from a serializable type. #[cfg(feature = "json")] - pub fn json(&'a mut self, body: &T) - -> Result<&mut Self, serde_json::Error> { + pub fn json(mut self, body: &T) -> Result { let json = serde_json::to_vec(body)?; self.body = Body::Json(json); Ok(self)