Skip to content
This repository was archived by the owner on Jun 21, 2020. It is now read-only.

Commit d23244f

Browse files
committed
impl BufRead for Body
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent 18e18af commit d23244f

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ members = ["http-service-hyper", "http-service-lambda", "http-service-mock"]
2222
bytes = "0.4.12"
2323
futures = "0.3.1"
2424
http = "0.1.17"
25-
async-std = "=0.99.11"
25+
async-std = { version = "1.0.1", default-features = false, features = ["std"] }
26+
pin-project-lite = "0.1.0"
2627

2728
[patch.crates-io]
2829
http-service = { path = "." }

src/lib.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ use futures::future::TryFuture;
1414
use std::fmt;
1515
use std::pin::Pin;
1616

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+
}
2023
}
2124

2225
impl Body {
@@ -28,7 +31,7 @@ impl Body {
2831
}
2932

3033
/// 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 {
3235
Self {
3336
reader: Box::new(reader),
3437
}
@@ -45,6 +48,17 @@ impl Read for Body {
4548
}
4649
}
4750

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+
4862
impl fmt::Debug for Body {
4963
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5064
f.debug_struct("Body").field("reader", &"<hidden>").finish()
@@ -59,7 +73,7 @@ impl From<Vec<u8>> for Body {
5973
}
6074
}
6175

62-
impl<R: Read + Unpin + Send + 'static> From<Box<R>> for Body {
76+
impl<R: BufRead + Unpin + Send + 'static> From<Box<R>> for Body {
6377
/// Converts an `AsyncRead` into a Body.
6478
fn from(reader: Box<R>) -> Self {
6579
Self { reader }

0 commit comments

Comments
 (0)