1
- use async_std:: io:: prelude:: * ;
2
- use async_std:: io:: { self , Cursor } ;
1
+ use futures_lite:: { io, prelude:: * } ;
3
2
use serde:: { de:: DeserializeOwned , Serialize } ;
4
3
5
4
use std:: fmt:: { self , Debug } ;
@@ -54,7 +53,7 @@ pin_project_lite::pin_project! {
54
53
/// and not rely on the fallback mechanisms. However, they're still there if you need them.
55
54
pub struct Body {
56
55
#[ pin]
57
- reader: Box <dyn BufRead + Unpin + Send + Sync + ' static >,
56
+ reader: Box <dyn AsyncBufRead + Unpin + Send + Sync + ' static >,
58
57
mime: Mime ,
59
58
length: Option <usize >,
60
59
}
@@ -102,7 +101,7 @@ impl Body {
102
101
/// req.set_body(Body::from_reader(cursor, Some(len)));
103
102
/// ```
104
103
pub fn from_reader (
105
- reader : impl BufRead + Unpin + Send + Sync + ' static ,
104
+ reader : impl AsyncBufRead + Unpin + Send + Sync + ' static ,
106
105
len : Option < usize > ,
107
106
) -> Self {
108
107
Self {
@@ -125,7 +124,7 @@ impl Body {
125
124
/// let body = Body::from_reader(cursor, None);
126
125
/// let _ = body.into_reader();
127
126
/// ```
128
- pub fn into_reader ( self ) -> Box < dyn BufRead + Unpin + Send + Sync + ' static > {
127
+ pub fn into_reader ( self ) -> Box < dyn AsyncBufRead + Unpin + Send + Sync + ' static > {
129
128
self . reader
130
129
}
131
130
@@ -160,7 +159,7 @@ impl Body {
160
159
/// # Examples
161
160
///
162
161
/// ```
163
- /// # fn main() -> Result<(), http_types::Error > { async_std::task::block_on(async {
162
+ /// # fn main() -> http_types:: Result<()> { async_std::task::block_on(async {
164
163
/// use http_types::Body;
165
164
///
166
165
/// let bytes = vec![1, 2, 3];
@@ -209,9 +208,7 @@ impl Body {
209
208
/// # Examples
210
209
///
211
210
/// ```
212
- /// # use std::io::prelude::*;
213
- /// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
214
- /// # async_std::task::block_on(async {
211
+ /// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
215
212
/// use http_types::Body;
216
213
/// use async_std::io::Cursor;
217
214
///
@@ -246,7 +243,7 @@ impl Body {
246
243
let bytes = serde_json:: to_vec ( & json) ?;
247
244
let body = Self {
248
245
length : Some ( bytes. len ( ) ) ,
249
- reader : Box :: new ( Cursor :: new ( bytes) ) ,
246
+ reader : Box :: new ( io :: Cursor :: new ( bytes) ) ,
250
247
mime : mime:: JSON ,
251
248
} ;
252
249
Ok ( body)
@@ -257,7 +254,7 @@ impl Body {
257
254
/// # Examples
258
255
///
259
256
/// ```
260
- /// # fn main() -> Result<(), http_types::Error > { async_std::task::block_on(async {
257
+ /// # fn main() -> http_types:: Result<()> { async_std::task::block_on(async {
261
258
/// use http_types::Body;
262
259
/// use http_types::convert::{Serialize, Deserialize};
263
260
///
@@ -290,7 +287,7 @@ impl Body {
290
287
/// # Examples
291
288
///
292
289
/// ```
293
- /// # fn main() -> Result<(), http_types::Error > { async_std::task::block_on(async {
290
+ /// # fn main() -> http_types:: Result<()> { async_std::task::block_on(async {
294
291
/// use http_types::Body;
295
292
/// use http_types::convert::{Serialize, Deserialize};
296
293
///
@@ -310,7 +307,7 @@ impl Body {
310
307
311
308
let body = Self {
312
309
length : Some ( bytes. len ( ) ) ,
313
- reader : Box :: new ( Cursor :: new ( bytes) ) ,
310
+ reader : Box :: new ( io :: Cursor :: new ( bytes) ) ,
314
311
mime : mime:: FORM ,
315
312
} ;
316
313
Ok ( body)
@@ -326,7 +323,7 @@ impl Body {
326
323
/// # Examples
327
324
///
328
325
/// ```
329
- /// # fn main() -> Result<(), http_types::Error > { async_std::task::block_on(async {
326
+ /// # fn main() -> http_types:: Result<()> { async_std::task::block_on(async {
330
327
/// use http_types::Body;
331
328
/// use http_types::convert::{Serialize, Deserialize};
332
329
///
@@ -353,14 +350,14 @@ impl Body {
353
350
/// # Examples
354
351
///
355
352
/// ```no_run
356
- /// # fn main() -> Result<(), http_types::Error > { async_std::task::block_on(async {
353
+ /// # fn main() -> http_types:: Result<()> { async_std::task::block_on(async {
357
354
/// use http_types::{Body, Response, StatusCode};
358
355
///
359
356
/// let mut res = Response::new(StatusCode::Ok);
360
357
/// res.set_body(Body::from_file("/path/to/file").await?);
361
358
/// # Ok(()) }) }
362
359
/// ```
363
- #[ cfg( all( feature = "async_std " , not( target_os = "unknown" ) ) ) ]
360
+ #[ cfg( all( feature = "fs " , not( target_os = "unknown" ) ) ) ]
364
361
pub async fn from_file < P > ( path : P ) -> io:: Result < Self >
365
362
where
366
363
P : AsRef < std:: path:: Path > ,
@@ -455,7 +452,7 @@ impl<'a> From<&'a [u8]> for Body {
455
452
}
456
453
}
457
454
458
- impl Read for Body {
455
+ impl AsyncRead for Body {
459
456
#[ allow( missing_doc_code_examples) ]
460
457
fn poll_read (
461
458
mut self : Pin < & mut Self > ,
@@ -466,7 +463,7 @@ impl Read for Body {
466
463
}
467
464
}
468
465
469
- impl BufRead for Body {
466
+ impl AsyncBufRead for Body {
470
467
#[ allow( missing_doc_code_examples) ]
471
468
fn poll_fill_buf ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < & ' _ [ u8 ] > > {
472
469
let this = self . project ( ) ;
@@ -480,7 +477,7 @@ impl BufRead for Body {
480
477
481
478
/// Look at first few bytes of a file to determine the mime type.
482
479
/// This is used for various binary formats such as images and videos.
483
- #[ cfg( all( feature = "async_std " , not( target_os = "unknown" ) ) ) ]
480
+ #[ cfg( all( feature = "fs " , not( target_os = "unknown" ) ) ) ]
484
481
async fn peek_mime ( file : & mut async_std:: fs:: File ) -> io:: Result < Option < Mime > > {
485
482
// We need to read the first 300 bytes to correctly infer formats such as tar.
486
483
let mut buf = [ 0_u8 ; 300 ] ;
@@ -494,7 +491,7 @@ async fn peek_mime(file: &mut async_std::fs::File) -> io::Result<Option<Mime>> {
494
491
495
492
/// Look at the extension of a file to determine the mime type.
496
493
/// This is useful for plain-text formats such as HTML and CSS.
497
- #[ cfg( all( feature = "async_std " , not( target_os = "unknown" ) ) ) ]
494
+ #[ cfg( all( feature = "fs " , not( target_os = "unknown" ) ) ) ]
498
495
fn guess_ext ( path : & std:: path:: Path ) -> Option < Mime > {
499
496
let ext = path. extension ( ) . map ( |p| p. to_str ( ) ) . flatten ( ) ;
500
497
ext. and_then ( Mime :: from_extension)
0 commit comments