encoder: add AsyncWrite implementation

This commit is contained in:
Alexandre Bury 2017-04-14 11:47:49 -07:00
parent 6c915474b6
commit 3c3ab356a8
3 changed files with 23 additions and 0 deletions

View file

@ -16,6 +16,8 @@ travis-ci = { repository = "gyscos/zstd-rs" }
[dependencies]
libc = "0.2"
zstd-sys = { path="zstd-sys", version = "1.1.5-a", default-features = false }
tokio-io = { version = "0.1", optional = true }
futures = { version = "0.1", optional = true }
[dev-dependencies]
clap = "2.6.0"
@ -24,3 +26,4 @@ clap = "2.6.0"
default = ["legacy"]
legacy = ["zstd-sys/legacy"]
bindgen = ["zstd-sys/bindgen"]
tokio = ["tokio-io", "futures"]

View file

@ -31,6 +31,13 @@ pub mod stream;
pub mod block;
pub mod dict;
#[cfg(feature = "tokio")]
#[macro_use]
extern crate tokio_io;
#[cfg(feature = "tokio")]
extern crate futures;
use std::ffi::CStr;
use std::io;

View file

@ -4,6 +4,11 @@ use parse_code;
use std::io::{self, Write};
use zstd_sys;
#[cfg(feature = "tokio")]
use futures::Poll;
#[cfg(feature = "tokio")]
use tokio_io::AsyncWrite;
struct EncoderContext {
s: *mut zstd_sys::ZSTD_CStream,
}
@ -328,6 +333,14 @@ impl<W: Write> Write for Encoder<W> {
}
}
#[cfg(feature = "tokio")]
impl <W: AsyncWrite> AsyncWrite for Encoder<W> {
fn shutdown(&mut self) -> Poll<(), io::Error> {
try_nb!(self.do_finish());
self.writer.shutdown()
}
}
#[cfg(test)]
mod tests {
use super::Encoder;