Skip to content

Commit

Permalink
construct bodies from impl Read instead of impl BufRead
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Dec 14, 2020
1 parent f77c653 commit 7cf5f91
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 42 deletions.
23 changes: 6 additions & 17 deletions src/body.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use futures_lite::{io, prelude::*, ready};
use futures_lite::{io, prelude::*, ready, AsyncRead as Read};
use serde::{de::DeserializeOwned, Serialize};

use std::fmt::{self, Debug};
Expand All @@ -12,7 +12,7 @@ pin_project_lite::pin_project! {
/// A streaming HTTP body.
///
/// `Body` represents the HTTP body of both `Request` and `Response`. It's completely
/// streaming, and implements `AsyncBufRead` to make reading from it both convenient and
/// streaming, and implements `AsyncRead` to make reading from it both convenient and
/// performant.
///
/// Both `Request` and `Response` take `Body` by `Into<Body>`, which means that passing string
Expand Down Expand Up @@ -53,7 +53,7 @@ pin_project_lite::pin_project! {
/// and not rely on the fallback mechanisms. However, they're still there if you need them.
pub struct Body {
#[pin]
reader: Box<dyn AsyncBufRead + Unpin + Send + Sync + 'static>,
reader: Box<dyn Read + Unpin + Send + Sync + 'static>,
mime: Mime,
length: Option<usize>,
bytes_read: usize
Expand Down Expand Up @@ -103,7 +103,7 @@ impl Body {
/// req.set_body(Body::from_reader(cursor, Some(len)));
/// ```
pub fn from_reader(
reader: impl AsyncBufRead + Unpin + Send + Sync + 'static,
reader: impl Read + Unpin + Send + Sync + 'static,
len: Option<usize>,
) -> Self {
Self {
Expand All @@ -127,7 +127,7 @@ impl Body {
/// let body = Body::from_reader(cursor, None);
/// let _ = body.into_reader();
/// ```
pub fn into_reader(self) -> Box<dyn AsyncBufRead + Unpin + Send + Sync + 'static> {
pub fn into_reader(self) -> Box<dyn Read + Unpin + Send + Sync + 'static> {
self.reader
}

Expand Down Expand Up @@ -383,7 +383,7 @@ impl Body {
Ok(Self {
mime,
length: Some(len as usize),
reader: Box::new(io::BufReader::new(file)),
reader: Box::new(file),
bytes_read: 0,
})
}
Expand Down Expand Up @@ -483,17 +483,6 @@ impl AsyncRead for Body {
}
}

impl AsyncBufRead for Body {
#[allow(missing_doc_code_examples)]
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'_ [u8]>> {
self.project().reader.poll_fill_buf(cx)
}

fn consume(mut self: Pin<&mut Self>, amt: usize) {
Pin::new(&mut self.reader).consume(amt)
}
}

/// Look at first few bytes of a file to determine the mime type.
/// This is used for various binary formats such as images and videos.
#[cfg(all(feature = "fs", not(target_os = "unknown")))]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
//! there are many different possible `Mime` types.
//!
//! `http-types`' `Body` struct can take anything that implements
//! [`AsyncBufRead`](https://docs.rs/futures/0.3.1/futures/io/trait.AsyncBufRead.html) and stream
//! [`AsyncRead`](https://docs.rs/futures/0.3.1/futures/io/trait.AsyncRead.html) and stream
//! it out. Depending on the version of HTTP used, the underlying bytes will be transmitted
//! differently. As a rule, if you know the size of the body it's usually more efficient to
//! declare it up front. But if you don't, things will still work.
Expand Down
12 changes: 0 additions & 12 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,18 +892,6 @@ impl AsyncRead for Request {
}
}

impl AsyncBufRead for Request {
#[allow(missing_doc_code_examples)]
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'_ [u8]>> {
let this = self.project();
this.body.poll_fill_buf(cx)
}

fn consume(mut self: Pin<&mut Self>, amt: usize) {
Pin::new(&mut self.body).consume(amt)
}
}

impl AsRef<Headers> for Request {
fn as_ref(&self) -> &Headers {
&self.headers
Expand Down
12 changes: 0 additions & 12 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,18 +603,6 @@ impl AsyncRead for Response {
}
}

impl AsyncBufRead for Response {
#[allow(missing_doc_code_examples)]
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'_ [u8]>> {
let this = self.project();
this.body.poll_fill_buf(cx)
}

fn consume(mut self: Pin<&mut Self>, amt: usize) {
Pin::new(&mut self.body).consume(amt)
}
}

impl AsRef<Headers> for Response {
fn as_ref(&self) -> &Headers {
&self.headers
Expand Down

0 comments on commit 7cf5f91

Please sign in to comment.