From 924393b81158e7302640e633ed1b06b88895cbe7 Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Sat, 30 Jun 2018 14:55:58 +0200 Subject: [PATCH] std::io::copy instead of own --- src/body.rs | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/src/body.rs b/src/body.rs index 522514a..e575954 100644 --- a/src/body.rs +++ b/src/body.rs @@ -1,5 +1,5 @@ use chunked_transfer; -use std::io::{empty, Cursor, Read, Result as IoResult, Write}; +use std::io::{copy, empty, Cursor, Read, Result as IoResult}; use stream::Stream; #[cfg(feature = "charset")] @@ -14,8 +14,6 @@ use super::SerdeValue; #[cfg(feature = "json")] use serde_json; -const CHUNK_SIZE: usize = 1024 * 1024; - pub enum Payload { Empty, Text(String, String), @@ -71,28 +69,13 @@ impl Payload { } } -pub fn send_body(body: SizedReader, do_chunk: bool, stream: &mut Stream) -> IoResult<()> { +pub fn send_body(mut body: SizedReader, do_chunk: bool, stream: &mut Stream) -> IoResult<()> { if do_chunk { - pipe(body.reader, chunked_transfer::Encoder::new(stream))?; + let mut chunker = chunked_transfer::Encoder::new(stream); + copy(&mut body.reader, &mut chunker)?; } else { - pipe(body.reader, stream)?; + copy(&mut body.reader, stream)?; } Ok(()) } - -fn pipe(mut reader: R, mut writer: W) -> IoResult<()> -where - R: Read, - W: Write, -{ - let mut buf = [0_u8; CHUNK_SIZE]; - loop { - let len = reader.read(&mut buf)?; - if len == 0 { - break; - } - writer.write_all(&buf[0..len])?; - } - Ok(()) -}