Skip to content

Commit

Permalink
feat(client): accept &String as Body in RequestBuilder
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This removes the trait `IntoBody`, and instead using
  `Into<Body>`, as it's more idiomatic. This will only have broken code
  that had custom implementations of `IntoBody`, and can be fixed by
  changing them to `Into<Body>`.
  • Loading branch information
seanmonstar committed Apr 16, 2015
1 parent 8bc179f commit a2aefd9
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ pub struct RequestBuilder<'a, U: IntoUrl> {
impl<'a, U: IntoUrl> RequestBuilder<'a, U> {

/// Set a request body to be sent.
pub fn body<B: IntoBody<'a>>(mut self, body: B) -> RequestBuilder<'a, U> {
self.body = Some(body.into_body());
pub fn body<B: Into<Body<'a>>>(mut self, body: B) -> RequestBuilder<'a, U> {
self.body = Some(body.into());
self
}

Expand Down Expand Up @@ -190,17 +190,17 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
};

let mut body = if can_have_body {
body.map(|b| b.into_body())
body
} else {
None
None
};

loop {
let mut req = try!(Request::with_connector(method.clone(), url.clone(), &mut client.connector));
headers.as_ref().map(|headers| req.headers_mut().extend(headers.iter()));

match (can_have_body, body.as_ref()) {
(true, Some(ref body)) => match body.size() {
(true, Some(body)) => match body.size() {
Some(size) => req.headers_mut().set(ContentLength(size)),
None => (), // chunked, Request will add it automatically
},
Expand Down Expand Up @@ -251,13 +251,7 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
}
}

/// A helper trait to allow overloading of the body parameter.
pub trait IntoBody<'a> {
/// Consumes self into an instance of `Body`.
fn into_body(self) -> Body<'a>;
}

/// The target enum for the IntoBody trait.
/// An enum of possible body types for a Request.
pub enum Body<'a> {
/// A Reader does not necessarily know it's size, so it is chunked.
ChunkedBody(&'a mut (Read + 'a)),
Expand Down Expand Up @@ -288,32 +282,31 @@ impl<'a> Read for Body<'a> {
}
}

// To allow someone to pass a `Body::SizedBody()` themselves.
impl<'a> IntoBody<'a> for Body<'a> {
impl<'a> Into<Body<'a>> for &'a [u8] {
#[inline]
fn into_body(self) -> Body<'a> {
self
fn into(self) -> Body<'a> {
Body::BufBody(self, self.len())
}
}

impl<'a> IntoBody<'a> for &'a [u8] {
impl<'a> Into<Body<'a>> for &'a str {
#[inline]
fn into_body(self) -> Body<'a> {
Body::BufBody(self, self.len())
fn into(self) -> Body<'a> {
self.as_bytes().into()
}
}

impl<'a> IntoBody<'a> for &'a str {
impl<'a> Into<Body<'a>> for &'a String {
#[inline]
fn into_body(self) -> Body<'a> {
self.as_bytes().into_body()
fn into(self) -> Body<'a> {
self.as_bytes().into()
}
}

impl<'a, R: Read> IntoBody<'a> for &'a mut R {
impl<'a, R: Read> From<&'a mut R> for Body<'a> {
#[inline]
fn into_body(self) -> Body<'a> {
Body::ChunkedBody(self)
fn from(r: &'a mut R) -> Body<'a> {
Body::ChunkedBody(r)
}
}

Expand Down

0 comments on commit a2aefd9

Please sign in to comment.