H3: change structure of Body

This commit is contained in:
Dirkjan Ochtman
2020-05-04 14:31:42 +02:00
committed by Jean-Christophe BEGUE
parent 74d8798b5d
commit 8a97c8d397
3 changed files with 10 additions and 15 deletions
+5 -10
View File
@@ -39,28 +39,23 @@ use crate::{
///
/// [`http::Request<B>`]: https://docs.rs/http/*/http/request/index.html
/// [`http::Response<B>`]: https://docs.rs/http/*/http/response/index.html
pub enum Body {
/// Inexistent body
None,
/// Buffer-contained body
Buf(Bytes),
}
pub struct Body(pub(crate) Option<Bytes>);
impl From<()> for Body {
fn from(_: ()) -> Self {
Body::None
Body(None)
}
}
impl From<Bytes> for Body {
fn from(buf: Bytes) -> Self {
Body::Buf(buf)
Body(Some(buf))
}
}
impl From<&str> for Body {
fn from(buf: &str) -> Self {
Body::Buf(Bytes::copy_from_slice(buf.as_ref()))
Body(Some(Bytes::copy_from_slice(buf.as_ref())))
}
}
@@ -360,7 +355,7 @@ impl BodyWriter {
/// let mut trailers = HeaderMap::new();
/// trailers.insert("trailing", "here".parse()?);
/// body_writer.trailers(trailers).await?;
///
///
/// Ok(())
/// }
/// ```
+3 -3
View File
@@ -450,7 +450,7 @@ impl Connection {
/// let (response, mut body_reader) = recv_response.await?;
/// let mut body = String::new();
/// body_reader.read_to_string(&mut body).await?;
///
///
/// Ok(())
/// }
/// ```
@@ -512,14 +512,14 @@ impl Connection {
let recv = RecvResponse::new(FrameDecoder::stream(recv), self.0.clone(), stream_id);
match body.into() {
Body::Buf(payload) => {
Body(Some(payload)) => {
let send: WriteFrame<DataFrame<_>> = WriteFrame::new(send, DataFrame { payload });
Ok((
recv,
BodyWriter::new(send.await?, self.0.clone(), stream_id, false),
))
}
Body::None => Ok((
Body(None) => Ok((
recv,
BodyWriter::new(send, self.0.clone(), stream_id, false),
)),
+2 -2
View File
@@ -785,8 +785,8 @@ impl Sender {
)?
.await?;
let send = match body.into() {
Body::None => send,
Body::Buf(payload) => WriteFrame::new(send, DataFrame { payload }).await?,
Body(None) => send,
Body(Some(payload)) => WriteFrame::new(send, DataFrame { payload }).await?,
};
Ok(BodyWriter::new(send, self.conn, self.stream_id, true))
}