feat(client): accept &String as Body in RequestBuilder
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>`.
This commit is contained in:
@@ -151,8 +151,8 @@ pub struct RequestBuilder<'a, U: IntoUrl> {
|
|||||||
impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
|
impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
|
||||||
|
|
||||||
/// Set a request body to be sent.
|
/// Set a request body to be sent.
|
||||||
pub fn body<B: IntoBody<'a>>(mut self, body: B) -> RequestBuilder<'a, U> {
|
pub fn body<B: Into<Body<'a>>>(mut self, body: B) -> RequestBuilder<'a, U> {
|
||||||
self.body = Some(body.into_body());
|
self.body = Some(body.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,7 +190,7 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut body = if can_have_body {
|
let mut body = if can_have_body {
|
||||||
body.map(|b| b.into_body())
|
body
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
@@ -200,7 +200,7 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
|
|||||||
headers.as_ref().map(|headers| req.headers_mut().extend(headers.iter()));
|
headers.as_ref().map(|headers| req.headers_mut().extend(headers.iter()));
|
||||||
|
|
||||||
match (can_have_body, body.as_ref()) {
|
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)),
|
Some(size) => req.headers_mut().set(ContentLength(size)),
|
||||||
None => (), // chunked, Request will add it automatically
|
None => (), // chunked, Request will add it automatically
|
||||||
},
|
},
|
||||||
@@ -251,13 +251,7 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A helper trait to allow overloading of the body parameter.
|
/// An enum of possible body types for a Request.
|
||||||
pub trait IntoBody<'a> {
|
|
||||||
/// Consumes self into an instance of `Body`.
|
|
||||||
fn into_body(self) -> Body<'a>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The target enum for the IntoBody trait.
|
|
||||||
pub enum Body<'a> {
|
pub enum Body<'a> {
|
||||||
/// A Reader does not necessarily know it's size, so it is chunked.
|
/// A Reader does not necessarily know it's size, so it is chunked.
|
||||||
ChunkedBody(&'a mut (Read + 'a)),
|
ChunkedBody(&'a mut (Read + 'a)),
|
||||||
@@ -288,32 +282,31 @@ impl<'a> Read for Body<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// To allow someone to pass a `Body::SizedBody()` themselves.
|
impl<'a> Into<Body<'a>> for &'a [u8] {
|
||||||
impl<'a> IntoBody<'a> for Body<'a> {
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn into_body(self) -> Body<'a> {
|
fn into(self) -> Body<'a> {
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> IntoBody<'a> for &'a [u8] {
|
|
||||||
#[inline]
|
|
||||||
fn into_body(self) -> Body<'a> {
|
|
||||||
Body::BufBody(self, self.len())
|
Body::BufBody(self, self.len())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> IntoBody<'a> for &'a str {
|
impl<'a> Into<Body<'a>> for &'a str {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn into_body(self) -> Body<'a> {
|
fn into(self) -> Body<'a> {
|
||||||
self.as_bytes().into_body()
|
self.as_bytes().into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, R: Read> IntoBody<'a> for &'a mut R {
|
impl<'a> Into<Body<'a>> for &'a String {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn into_body(self) -> Body<'a> {
|
fn into(self) -> Body<'a> {
|
||||||
Body::ChunkedBody(self)
|
self.as_bytes().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, R: Read> From<&'a mut R> for Body<'a> {
|
||||||
|
#[inline]
|
||||||
|
fn from(r: &'a mut R) -> Body<'a> {
|
||||||
|
Body::ChunkedBody(r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user