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 From b4fc1fc57fb973ee856d97079918b0051c93858b Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sun, 23 Jun 2019 20:46:46 -0400 Subject: Add doc and comments --- src/pipe.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/pipe.rs') diff --git a/src/pipe.rs b/src/pipe.rs index 710be1f..2fd9fac 100644 --- a/src/pipe.rs +++ b/src/pipe.rs @@ -3,12 +3,18 @@ use futures::sink::{Sink, Wait}; use futures::sync::mpsc::Sender; use std::io::{Error, ErrorKind, Result, Write}; +/// Adapter to implement the `std::io::Write` trait on a `Sender` from a futures channel. +/// +/// It uses an intermediate buffer to transfer packets. pub struct Pipe { + // Wrapping the sender in `Wait` makes it blocking, so we can implement blocking-style + // io::Write over the async-style Sender. dest: Wait>, bytes: BytesMut, } impl Pipe { + /// Wrap the given sender in a `Pipe`. pub fn new(destination: Sender) -> Self { Pipe { dest: destination.wait(), @@ -19,17 +25,24 @@ impl Pipe { impl Drop for Pipe { fn drop(&mut self) { + // This is the correct thing to do, but is not super important since the `Sink` + // implementation of `Sender` just returns `Ok` without doing anything else. let _ = self.dest.close(); } } impl Write for Pipe { fn write(&mut self, buf: &[u8]) -> Result { + // We are given a slice of bytes we do not own, so we must start by copying it. 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)), - } + + // Then, take the buffer and send it in the channel. + self.dest + .send(self.bytes.take().into()) + .map_err(|e| Error::new(ErrorKind::UnexpectedEof, e))?; + + // Return how much we sent - all of it. + Ok(buf.len()) } fn flush(&mut self) -> Result<()> { -- cgit v1.2.3 From 399929d8ef0f8c25ffec08d0b025cb05d70aa520 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sun, 23 Jun 2019 21:05:52 -0400 Subject: Add module documentation on src/pipe.r --- src/pipe.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'src/pipe.rs') diff --git a/src/pipe.rs b/src/pipe.rs index 2fd9fac..09ca580 100644 --- a/src/pipe.rs +++ b/src/pipe.rs @@ -1,3 +1,4 @@ +//! Define an adapter to implement `std::io::Write` on `Sender`. use bytes::{Bytes, BytesMut}; use futures::sink::{Sink, Wait}; use futures::sync::mpsc::Sender; -- cgit v1.2.3