Skip to content

Commit a49c9dc

Browse files
committed
Add a StreamedBody utility.
1 parent 9c0d2ef commit a49c9dc

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/http/body.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ impl<T: AsRef<[u8]>> Body for BoundedBody<T> {
107107
}
108108
}
109109

110+
/// An HTTP body with an unknown length
111+
#[derive(Debug)]
112+
pub struct StreamedBody<S: AsyncRead>(S);
113+
114+
impl<S: AsyncRead> StreamedBody<S> {
115+
/// Wrap an `AsyncRead` impl in a type that provides a [`Body`] implementation.
116+
pub fn new(s: S) -> Self {
117+
Self(s)
118+
}
119+
}
120+
impl<S: AsyncRead> AsyncRead for StreamedBody<S> {
121+
async fn read(&mut self, buf: &mut [u8]) -> crate::io::Result<usize> {
122+
self.0.read(buf).await
123+
}
124+
}
125+
impl<S: AsyncRead> Body for StreamedBody<S> {
126+
fn len(&self) -> Option<usize> {
127+
None
128+
}
129+
}
130+
110131
impl Body for Empty {
111132
fn len(&self) -> Option<usize> {
112133
Some(0)

0 commit comments

Comments
 (0)