@@ -14,9 +14,12 @@ use futures::future::TryFuture;
14
14
use std:: fmt;
15
15
use std:: pin:: Pin ;
16
16
17
- /// The raw body of an http request or response.
18
- pub struct Body {
19
- reader : Box < dyn Read + Unpin + Send + ' static > ,
17
+ pin_project_lite:: pin_project! {
18
+ /// The raw body of an http request or response.
19
+ pub struct Body {
20
+ #[ pin]
21
+ reader: Box <dyn BufRead + Unpin + Send + ' static >,
22
+ }
20
23
}
21
24
22
25
impl Body {
@@ -28,7 +31,7 @@ impl Body {
28
31
}
29
32
30
33
/// Create a new instance from a reader.
31
- pub fn from_reader ( reader : impl Read + Unpin + Send + ' static ) -> Self {
34
+ pub fn from_reader ( reader : impl BufRead + Unpin + Send + ' static ) -> Self {
32
35
Self {
33
36
reader : Box :: new ( reader) ,
34
37
}
@@ -45,6 +48,17 @@ impl Read for Body {
45
48
}
46
49
}
47
50
51
+ impl BufRead for Body {
52
+ fn poll_fill_buf ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < & ' _ [ u8 ] > > {
53
+ let this = self . project ( ) ;
54
+ this. reader . poll_fill_buf ( cx)
55
+ }
56
+
57
+ fn consume ( mut self : Pin < & mut Self > , amt : usize ) {
58
+ Pin :: new ( & mut self . reader ) . consume ( amt)
59
+ }
60
+ }
61
+
48
62
impl fmt:: Debug for Body {
49
63
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
50
64
f. debug_struct ( "Body" ) . field ( "reader" , & "<hidden>" ) . finish ( )
@@ -59,7 +73,7 @@ impl From<Vec<u8>> for Body {
59
73
}
60
74
}
61
75
62
- impl < R : Read + Unpin + Send + ' static > From < Box < R > > for Body {
76
+ impl < R : BufRead + Unpin + Send + ' static > From < Box < R > > for Body {
63
77
/// Converts an `AsyncRead` into a Body.
64
78
fn from ( reader : Box < R > ) -> Self {
65
79
Self { reader }
0 commit comments