@@ -69,7 +69,7 @@ pub trait HttpClient: Debug + Unpin + Send + Sync + Clone + 'static {
69
69
/// Both `Body` and `Bytes` values can be easily created from standard owned byte buffer types
70
70
/// like `Vec<u8>` or `String`, using the `From` trait.
71
71
pub struct Body {
72
- reader : Box < dyn AsyncRead + Unpin + Send + ' static > ,
72
+ reader : Option < Box < dyn AsyncRead + Unpin + Send + ' static > > ,
73
73
/// Intentionally use `u64` over `usize` here.
74
74
/// `usize` won't work if you try to send 10GB file from 32bit host.
75
75
#[ allow( dead_code) ] // not all backends make use of this
@@ -80,18 +80,23 @@ impl Body {
80
80
/// Create a new empty body.
81
81
pub fn empty ( ) -> Self {
82
82
Self {
83
- reader : Box :: new ( futures :: io :: empty ( ) ) ,
83
+ reader : None ,
84
84
len : Some ( 0 ) ,
85
85
}
86
86
}
87
87
88
88
/// Create a new instance from a reader.
89
89
pub fn from_reader ( reader : impl AsyncRead + Unpin + Send + ' static ) -> Self {
90
90
Self {
91
- reader : Box :: new ( reader) ,
91
+ reader : Some ( Box :: new ( reader) ) ,
92
92
len : None ,
93
93
}
94
94
}
95
+
96
+ /// Validate that the body was created with `Body::empty()`.
97
+ pub fn is_empty ( & self ) -> bool {
98
+ self . reader . is_none ( )
99
+ }
95
100
}
96
101
97
102
impl AsyncRead for Body {
@@ -101,7 +106,10 @@ impl AsyncRead for Body {
101
106
cx : & mut Context < ' _ > ,
102
107
buf : & mut [ u8 ] ,
103
108
) -> Poll < io:: Result < usize > > {
104
- Pin :: new ( & mut self . reader ) . poll_read ( cx, buf)
109
+ match self . reader . as_mut ( ) {
110
+ Some ( reader) => Pin :: new ( reader) . poll_read ( cx, buf) ,
111
+ None => Poll :: Ready ( Ok ( 0 ) ) ,
112
+ }
105
113
}
106
114
}
107
115
@@ -118,7 +126,7 @@ impl From<Vec<u8>> for Body {
118
126
fn from ( vec : Vec < u8 > ) -> Body {
119
127
let len = vec. len ( ) as u64 ;
120
128
Self {
121
- reader : Box :: new ( Cursor :: new ( vec) ) ,
129
+ reader : Some ( Box :: new ( Cursor :: new ( vec) ) ) ,
122
130
len : Some ( len) ,
123
131
}
124
132
}
@@ -128,6 +136,9 @@ impl<R: AsyncRead + Unpin + Send + 'static> From<Box<R>> for Body {
128
136
/// Converts an `AsyncRead` into a Body.
129
137
#[ allow( missing_doc_code_examples) ]
130
138
fn from ( reader : Box < R > ) -> Self {
131
- Self { reader, len : None }
139
+ Self {
140
+ reader : Some ( reader) ,
141
+ len : None ,
142
+ }
132
143
}
133
144
}
0 commit comments