aboutsummaryrefslogtreecommitdiffstats
path: root/src/pipe.rs
diff options
context:
space:
mode:
authorAlexandre Bury <alexandre.bury@gmail.com>2019-06-14 16:43:09 +0000
committerAlexandre Bury <alexandre.bury@gmail.com>2019-06-14 21:50:00 +0000
commit5440794ac9268d82f100ac0565f1a1ed815d83aa (patch)
treec569a459bff8a287e7ce1ab154fc46d4c0f77d7d /src/pipe.rs
parentUpdate deps (diff)
downloadminiserve-5440794ac9268d82f100ac0565f1a1ed815d83aa.tar.gz
miniserve-5440794ac9268d82f100ac0565f1a1ed815d83aa.zip
Enable streaming tarball download
Also add a non-compressed tar option
Diffstat (limited to '')
-rw-r--r--src/pipe.rs40
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))
+ }
+}