@@ -109,26 +109,46 @@ impl Response {
109
109
}
110
110
}
111
111
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.
113
113
///
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.
114
134
///
115
135
/// # Examples
116
136
///
117
137
/// ```
118
138
/// # fn main() -> Result<(), http_types::url::ParseError> {
119
139
/// #
120
- /// use http_types::{Url, Method, Request};
140
+ /// use http_types::{Body, Url, Method, Request};
121
141
///
122
142
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
123
143
/// 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);
125
147
/// #
126
148
/// # Ok(()) }
127
149
/// ```
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) ;
132
152
}
133
153
134
154
/// Take the request body, replacing it with an empty body.
@@ -138,7 +158,7 @@ impl Response {
138
158
/// ```
139
159
/// # fn main() -> Result<(), http_types::url::ParseError> {
140
160
/// #
141
- /// use http_types::{Url, Method, Request};
161
+ /// use http_types::{Body, Url, Method, Request};
142
162
///
143
163
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
144
164
/// req.set_body("Hello, Nori!");
@@ -147,7 +167,7 @@ impl Response {
147
167
/// # Ok(()) }
148
168
/// ```
149
169
pub fn take_body ( & mut self ) -> Body {
150
- self . swap_body ( Body :: empty ( ) )
170
+ self . replace_body ( Body :: empty ( ) )
151
171
}
152
172
153
173
/// Set the response MIME.
0 commit comments