Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

construct bodies from impl Read instead of impl BufRead #298

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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};
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 AsyncRead + 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 AsyncRead + 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 AsyncRead + 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