diff options
Diffstat (limited to 'src/pipe.rs')
-rw-r--r-- | src/pipe.rs | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/src/pipe.rs b/src/pipe.rs index 09ca580..92f7019 100644 --- a/src/pipe.rs +++ b/src/pipe.rs @@ -1,7 +1,8 @@ //! Define an adapter to implement `std::io::Write` on `Sender<Bytes>`. -use bytes::{Bytes, BytesMut}; -use futures::sink::{Sink, Wait}; -use futures::sync::mpsc::Sender; +use actix_web::web::{Bytes, BytesMut}; +use futures::channel::mpsc::Sender; +use futures::executor::block_on; +use futures::sink::SinkExt; use std::io::{Error, ErrorKind, Result, Write}; /// Adapter to implement the `std::io::Write` trait on a `Sender<Bytes>` from a futures channel. @@ -10,15 +11,15 @@ use std::io::{Error, ErrorKind, Result, Write}; 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<Sender<Bytes>>, + dest: Sender<std::result::Result<Bytes, ()>>, bytes: BytesMut, } impl Pipe { /// Wrap the given sender in a `Pipe`. - pub fn new(destination: Sender<Bytes>) -> Self { + pub fn new(destination: Sender<std::result::Result<Bytes, ()>>) -> Self { Pipe { - dest: destination.wait(), + dest: destination, bytes: BytesMut::new(), } } @@ -28,7 +29,7 @@ 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(); + let _ = block_on(self.dest.close()); } } @@ -38,8 +39,7 @@ impl Write for Pipe { self.bytes.extend_from_slice(buf); // Then, take the buffer and send it in the channel. - self.dest - .send(self.bytes.take().into()) + block_on(self.dest.send(Ok(self.bytes.split().into()))) .map_err(|e| Error::new(ErrorKind::UnexpectedEof, e))?; // Return how much we sent - all of it. @@ -47,8 +47,6 @@ impl Write for Pipe { } fn flush(&mut self) -> Result<()> { - self.dest - .flush() - .map_err(|e| Error::new(ErrorKind::UnexpectedEof, e)) + block_on(self.dest.flush()).map_err(|e| Error::new(ErrorKind::UnexpectedEof, e)) } } |