Skip to content

Commit b101271

Browse files
committed
fix body methods, add replace_body
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent 0282ebb commit b101271

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

src/request.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,31 +126,45 @@ impl Request {
126126
/// # Examples
127127
///
128128
/// ```
129-
/// use http_types::{Url, Method, Request};
129+
/// use http_types::{Body, Url, Method, Request};
130130
///
131131
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
132132
/// req.set_body("hello world");
133-
/// let _body: Body = req.swap_body("hello planet");
133+
/// let _body: Body = req.replace_body("hello planet");
134134
/// ```
135-
pub fn swap_body(&mut self, body: impl Into<Body>) -> Body {
136-
let mut body = body.into();
137-
mem::swap(&mut self.body, &mut body);
138-
body
135+
pub fn replace_body(&mut self, body: impl Into<Body>) -> Body {
136+
mem::replace(&mut self.body, body.into())
137+
}
138+
139+
/// Replace the request body with a new body, and return the old body.
140+
///
141+
/// # Examples
142+
///
143+
/// ```
144+
/// use http_types::{Body, Url, Method, Request};
145+
///
146+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
147+
/// req.set_body("hello world");
148+
/// let mut body = "hello planet".into();
149+
/// req.swap_body(&mut body);
150+
/// ```
151+
pub fn swap_body(&mut self, body: &mut Body) {
152+
mem::swap(&mut self.body, body);
139153
}
140154

141155
/// Take the request body, replacing it with an empty body.
142156
///
143157
/// # Examples
144158
///
145159
/// ```
146-
/// use http_types::{Url, Method, Request};
160+
/// use http_types::{Body, Url, Method, Request};
147161
///
148162
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
149163
/// req.set_body("hello world");
150164
/// let _body: Body = req.take_body();
151165
/// ```
152166
pub fn take_body(&mut self) -> Body {
153-
self.swap_body(Body::empty())
167+
self.replace_body(Body::empty())
154168
}
155169

156170
/// Get an HTTP header.

src/response.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,46 @@ impl Response {
109109
}
110110
}
111111

112-
/// Replace the request body with a new body, and return the old body.
112+
/// Replace the request body with a new body, returning the old body.
113113
///
114+
/// # Examples
115+
///
116+
/// ```
117+
/// # fn main() -> Result<(), http_types::url::ParseError> {
118+
/// #
119+
/// use http_types::{Body, Url, Method, Request};
120+
///
121+
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
122+
/// req.set_body("Hello, Nori!");
123+
///
124+
/// let body: Body = req.replace_body("Hello, Chashu");
125+
/// #
126+
/// # Ok(()) }
127+
/// ```
128+
pub fn replace_body(&mut self, body: impl Into<Body>) -> Body {
129+
mem::replace(&mut self.body, body.into())
130+
}
131+
132+
/// Swaps the value of the body with another body, without deinitializing
133+
/// either one.
114134
///
115135
/// # Examples
116136
///
117137
/// ```
118138
/// # fn main() -> Result<(), http_types::url::ParseError> {
119139
/// #
120-
/// use http_types::{Url, Method, Request};
140+
/// use http_types::{Body, Url, Method, Request};
121141
///
122142
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
123143
/// req.set_body("Hello, Nori!");
124-
/// let _body: Body = req.swap_body("Hello, Chashu!");
144+
///
145+
/// let mut body = "Hello, Chashu".into();
146+
/// req.swap_body(&mut body);
125147
/// #
126148
/// # Ok(()) }
127149
/// ```
128-
pub fn swap_body(&mut self, body: impl Into<Body>) -> Body {
129-
let mut body = body.into();
130-
mem::swap(&mut self.body, &mut body);
131-
body
150+
pub fn swap_body(&mut self, body: &mut Body) {
151+
mem::swap(&mut self.body, body);
132152
}
133153

134154
/// Take the request body, replacing it with an empty body.
@@ -138,7 +158,7 @@ impl Response {
138158
/// ```
139159
/// # fn main() -> Result<(), http_types::url::ParseError> {
140160
/// #
141-
/// use http_types::{Url, Method, Request};
161+
/// use http_types::{Body, Url, Method, Request};
142162
///
143163
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
144164
/// req.set_body("Hello, Nori!");
@@ -147,7 +167,7 @@ impl Response {
147167
/// # Ok(()) }
148168
/// ```
149169
pub fn take_body(&mut self) -> Body {
150-
self.swap_body(Body::empty())
170+
self.replace_body(Body::empty())
151171
}
152172

153173
/// Set the response MIME.

0 commit comments

Comments
 (0)