Skip to content

Commit a2aefd9

Browse files
committed
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>`.
1 parent 8bc179f commit a2aefd9

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

src/client/mod.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ pub struct RequestBuilder<'a, U: IntoUrl> {
151151
impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
152152

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

@@ -190,17 +190,17 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
190190
};
191191

192192
let mut body = if can_have_body {
193-
body.map(|b| b.into_body())
193+
body
194194
} else {
195-
None
195+
None
196196
};
197197

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

202202
match (can_have_body, body.as_ref()) {
203-
(true, Some(ref body)) => match body.size() {
203+
(true, Some(body)) => match body.size() {
204204
Some(size) => req.headers_mut().set(ContentLength(size)),
205205
None => (), // chunked, Request will add it automatically
206206
},
@@ -251,13 +251,7 @@ impl<'a, U: IntoUrl> RequestBuilder<'a, U> {
251251
}
252252
}
253253

254-
/// A helper trait to allow overloading of the body parameter.
255-
pub trait IntoBody<'a> {
256-
/// Consumes self into an instance of `Body`.
257-
fn into_body(self) -> Body<'a>;
258-
}
259-
260-
/// The target enum for the IntoBody trait.
254+
/// An enum of possible body types for a Request.
261255
pub enum Body<'a> {
262256
/// A Reader does not necessarily know it's size, so it is chunked.
263257
ChunkedBody(&'a mut (Read + 'a)),
@@ -288,32 +282,31 @@ impl<'a> Read for Body<'a> {
288282
}
289283
}
290284

291-
// To allow someone to pass a `Body::SizedBody()` themselves.
292-
impl<'a> IntoBody<'a> for Body<'a> {
285+
impl<'a> Into<Body<'a>> for &'a [u8] {
293286
#[inline]
294-
fn into_body(self) -> Body<'a> {
295-
self
287+
fn into(self) -> Body<'a> {
288+
Body::BufBody(self, self.len())
296289
}
297290
}
298291

299-
impl<'a> IntoBody<'a> for &'a [u8] {
292+
impl<'a> Into<Body<'a>> for &'a str {
300293
#[inline]
301-
fn into_body(self) -> Body<'a> {
302-
Body::BufBody(self, self.len())
294+
fn into(self) -> Body<'a> {
295+
self.as_bytes().into()
303296
}
304297
}
305298

306-
impl<'a> IntoBody<'a> for &'a str {
299+
impl<'a> Into<Body<'a>> for &'a String {
307300
#[inline]
308-
fn into_body(self) -> Body<'a> {
309-
self.as_bytes().into_body()
301+
fn into(self) -> Body<'a> {
302+
self.as_bytes().into()
310303
}
311304
}
312305

313-
impl<'a, R: Read> IntoBody<'a> for &'a mut R {
306+
impl<'a, R: Read> From<&'a mut R> for Body<'a> {
314307
#[inline]
315-
fn into_body(self) -> Body<'a> {
316-
Body::ChunkedBody(self)
308+
fn from(r: &'a mut R) -> Body<'a> {
309+
Body::ChunkedBody(r)
317310
}
318311
}
319312

0 commit comments

Comments
 (0)