From 5440794ac9268d82f100ac0565f1a1ed815d83aa Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Fri, 14 Jun 2019 09:43:09 -0700 Subject: Enable streaming tarball download Also add a non-compressed tar option --- src/pipe.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/pipe.rs (limited to 'src/pipe.rs') diff --git a/src/pipe.rs b/src/pipe.rs new file mode 100644 index 0000000..710be1f --- /dev/null +++ b/src/pipe.rs @@ -0,0 +1,40 @@ +use bytes::{Bytes, BytesMut}; +use futures::sink::{Sink, Wait}; +use futures::sync::mpsc::Sender; +use std::io::{Error, ErrorKind, Result, Write}; + +pub struct Pipe { + dest: Wait>, + bytes: BytesMut, +} + +impl Pipe { + pub fn new(destination: Sender) -> Self { + Pipe { + dest: destination.wait(), + bytes: BytesMut::new(), + } + } +} + +impl Drop for Pipe { + fn drop(&mut self) { + let _ = self.dest.close(); + } +} + +impl Write for Pipe { + fn write(&mut self, buf: &[u8]) -> Result { + self.bytes.extend_from_slice(buf); + match self.dest.send(self.bytes.take().into()) { + Ok(_) => Ok(buf.len()), + Err(e) => Err(Error::new(ErrorKind::UnexpectedEof, e)), + } + } + + fn flush(&mut self) -> Result<()> { + self.dest + .flush() + .map_err(|e| Error::new(ErrorKind::UnexpectedEof, e)) + } +} -- cgit v1.2.3