diff options
author | Alexandre Bury <alexandre.bury@gmail.com> | 2019-06-14 16:43:09 +0000 |
---|---|---|
committer | Alexandre Bury <alexandre.bury@gmail.com> | 2019-06-14 21:50:00 +0000 |
commit | 5440794ac9268d82f100ac0565f1a1ed815d83aa (patch) | |
tree | c569a459bff8a287e7ce1ab154fc46d4c0f77d7d /src/pipe.rs | |
parent | Update deps (diff) | |
download | miniserve-5440794ac9268d82f100ac0565f1a1ed815d83aa.tar.gz miniserve-5440794ac9268d82f100ac0565f1a1ed815d83aa.zip |
Enable streaming tarball download
Also add a non-compressed tar option
Diffstat (limited to 'src/pipe.rs')
-rw-r--r-- | src/pipe.rs | 40 |
1 files changed, 40 insertions, 0 deletions
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<Sender<Bytes>>, + bytes: BytesMut, +} + +impl Pipe { + pub fn new(destination: Sender<Bytes>) -> 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<usize> { + 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)) + } +} |