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.
This commit is contained in:
Vincent Ambo 2019-02-26 14:33:02 +01:00
parent c6c1746428
commit 718d945753

View file

@ -78,28 +78,27 @@ impl <'a> Request<'a> {
} }
/// Add a header to a request. /// 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> {
self.headers.append(&format!("{}: {}", k, v))?; self.headers.append(&format!("{}: {}", k, v))?;
Ok(self) Ok(self)
} }
/// Set the User-Agent for this request. /// 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> {
self.handle.useragent(agent)?; self.handle.useragent(agent)?;
Ok(self) Ok(self)
} }
/// Add a byte-array body to a request using the specified /// Add a byte-array body to a request using the specified
/// Content-Type. /// 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.body = Body::Bytes { data, content_type };
self self
} }
/// Add a JSON-encoded body from a serializable type. /// Add a JSON-encoded body from a serializable type.
#[cfg(feature = "json")] #[cfg(feature = "json")]
pub fn json<T: Serialize>(&'a mut self, body: &T) pub fn json<T: Serialize>(mut self, body: &T) -> Result<Self, serde_json::Error> {
-> Result<&mut Self, serde_json::Error> {
let json = serde_json::to_vec(body)?; let json = serde_json::to_vec(body)?;
self.body = Body::Json(json); self.body = Body::Json(json);
Ok(self) Ok(self)