@@ -20,7 +20,7 @@ pin_project_lite::pin_project! {
20
20
/// use http_types::{Url, Method, Request};
21
21
///
22
22
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
23
- /// req.set_body("hello world ");
23
+ /// req.set_body("Hello, Nori! ");
24
24
/// ```
25
25
#[ derive( Debug ) ]
26
26
pub struct Request {
@@ -111,7 +111,7 @@ impl Request {
111
111
/// use http_types::{Url, Method, Request};
112
112
///
113
113
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
114
- /// req.set_body("hello world ");
114
+ /// req.set_body("Hello, Nori! ");
115
115
/// ```
116
116
pub fn set_body ( & mut self , body : impl Into < Body > ) {
117
117
self . body = body. into ( ) ;
@@ -121,16 +121,27 @@ impl Request {
121
121
}
122
122
}
123
123
124
- /// Replace the request body with a new body, and return the old body.
124
+ /// Swaps the value of the body with another body, without deinitializing
125
+ /// either one.
125
126
///
126
127
/// # Examples
127
128
///
128
129
/// ```
130
+ /// # use async_std::io::prelude::*;
131
+ /// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
132
+ /// # async_std::task::block_on(async {
133
+ /// #
129
134
/// use http_types::{Body, Url, Method, Request};
130
135
///
131
136
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
132
- /// req.set_body("hello world");
133
- /// let _body: Body = req.replace_body("hello planet");
137
+ /// req.set_body("Hello, Nori!");
138
+ /// let mut body: Body = req.replace_body("Hello, Chashu!");
139
+ ///
140
+ /// let mut string = String::new();
141
+ /// body.read_to_string(&mut string).await?;
142
+ /// assert_eq!(&string, "Hello, Nori!");
143
+ /// #
144
+ /// # Ok(()) }) }
134
145
/// ```
135
146
pub fn replace_body ( & mut self , body : impl Into < Body > ) -> Body {
136
147
mem:: replace ( & mut self . body , body. into ( ) )
@@ -141,12 +152,22 @@ impl Request {
141
152
/// # Examples
142
153
///
143
154
/// ```
155
+ /// # use async_std::io::prelude::*;
156
+ /// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
157
+ /// # async_std::task::block_on(async {
158
+ /// #
144
159
/// use http_types::{Body, Url, Method, Request};
145
160
///
146
161
/// 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();
162
+ /// req.set_body("Hello, Nori! ");
163
+ /// let mut body = "Hello, Chashu! ".into();
149
164
/// req.swap_body(&mut body);
165
+ ///
166
+ /// let mut string = String::new();
167
+ /// body.read_to_string(&mut string).await?;
168
+ /// assert_eq!(&string, "Hello, Nori!");
169
+ /// #
170
+ /// # Ok(()) }) }
150
171
/// ```
151
172
pub fn swap_body ( & mut self , body : & mut Body ) {
152
173
mem:: swap ( & mut self . body , body) ;
@@ -157,11 +178,25 @@ impl Request {
157
178
/// # Examples
158
179
///
159
180
/// ```
181
+ /// # use async_std::io::prelude::*;
182
+ /// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
183
+ /// # async_std::task::block_on(async {
184
+ /// #
160
185
/// use http_types::{Body, Url, Method, Request};
161
186
///
162
187
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com").unwrap());
163
- /// req.set_body("hello world");
164
- /// let _body: Body = req.take_body();
188
+ /// req.set_body("Hello, Nori!");
189
+ /// let mut body: Body = req.take_body();
190
+ ///
191
+ /// let mut string = String::new();
192
+ /// body.read_to_string(&mut string).await?;
193
+ /// assert_eq!(&string, "Hello, Nori!");
194
+ ///
195
+ /// # let mut string = String::new();
196
+ /// # req.read_to_string(&mut string).await?;
197
+ /// # assert_eq!(&string, "");
198
+ /// #
199
+ /// # Ok(()) }) }
165
200
/// ```
166
201
pub fn take_body ( & mut self ) -> Body {
167
202
self . replace_body ( Body :: empty ( ) )
0 commit comments