Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs #14

Merged
merged 5 commits into from
May 12, 2019
Merged

Docs #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: rust
rust:
- nightly-2019-04-25
- nightly-2019-05-11

before_script: |
rustup component add rustfmt clippy
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ license = "MIT OR Apache-2.0"
brotli2 = "0.3.2"
bytes = "0.4.12"
flate2 = "1.0.7"
futures-preview = "0.3.0-alpha.15"
futures-preview = "0.3.0-alpha.16"
pin-project = "0.3.2"

[dev-dependencies]
proptest = "0.9.3"
pin-utils = "0.1.0-alpha.4"
proptest-derive = "0.1.1"
rand = "0.6.5"
futures-test-preview = "0.3.0-alpha.16"
33 changes: 19 additions & 14 deletions src/read/deflate.rs → src/bufread/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,38 @@ use core::{
};
use std::io::Result;

pub use flate2::Compression;
use flate2::{Compress, FlushCompress};
use flate2::{Compress, Compression, FlushCompress};
use futures::{
io::{AsyncBufRead, AsyncRead},
ready,
};
use pin_project::unsafe_project;

/// A DEFLATE encoder, or compressor.
///
/// This structure implements an [`AsyncRead`] interface and will read uncompressed data from an
/// underlying stream and emit a stream of compressed data.
#[unsafe_project(Unpin)]
pub struct DeflateRead<R: AsyncBufRead> {
pub struct DeflateEncoder<R: AsyncBufRead> {
#[pin]
inner: R,
flushing: bool,
compress: Compress,
}

impl<R: AsyncBufRead> AsyncRead for DeflateRead<R> {
impl<R: AsyncBufRead> DeflateEncoder<R> {
/// Creates a new encoder which will read uncompressed data from the given stream and emit a
/// compressed stream.
pub fn new(read: R, level: Compression) -> DeflateEncoder<R> {
DeflateEncoder {
inner: read,
flushing: false,
compress: Compress::new(level, false),
}
}
}

impl<R: AsyncBufRead> AsyncRead for DeflateEncoder<R> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -50,13 +65,3 @@ impl<R: AsyncBufRead> AsyncRead for DeflateRead<R> {
}
}
}

impl<R: AsyncBufRead> DeflateRead<R> {
pub fn new(read: R, level: Compression) -> DeflateRead<R> {
DeflateRead {
inner: read,
flushing: false,
compress: Compress::new(level, false),
}
}
}
8 changes: 8 additions & 0 deletions src/bufread/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! Types which operate over [`AsyncBufRead`](futures::io::AsyncBufRead) streams, both encoders and
//! decoders for various formats.

mod deflate;
mod zlib;

pub use deflate::DeflateEncoder;
pub use zlib::ZlibEncoder;
33 changes: 19 additions & 14 deletions src/read/zlib.rs → src/bufread/zlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,38 @@ use core::{
};
use std::io::Result;

pub use flate2::Compression;
use flate2::{Compress, FlushCompress};
use flate2::{Compress, Compression, FlushCompress};
use futures::{
io::{AsyncBufRead, AsyncRead},
ready,
};
use pin_project::unsafe_project;

/// A zlib encoder, or compressor.
///
/// This structure implements an [`AsyncRead`] interface and will read uncompressed data from an
/// underlying stream and emit a stream of compressed data.
#[unsafe_project(Unpin)]
pub struct ZlibRead<R: AsyncBufRead> {
pub struct ZlibEncoder<R: AsyncBufRead> {
#[pin]
inner: R,
flushing: bool,
compress: Compress,
}

impl<R: AsyncBufRead> AsyncRead for ZlibRead<R> {
impl<R: AsyncBufRead> ZlibEncoder<R> {
/// Creates a new encoder which will read uncompressed data from the given stream and emit a
/// compressed stream.
pub fn new(read: R, level: Compression) -> ZlibEncoder<R> {
ZlibEncoder {
inner: read,
flushing: false,
compress: Compress::new(level, true),
}
}
}

impl<R: AsyncBufRead> AsyncRead for ZlibEncoder<R> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -50,13 +65,3 @@ impl<R: AsyncBufRead> AsyncRead for ZlibRead<R> {
}
}
}

impl<R: AsyncBufRead> ZlibRead<R> {
pub fn new(read: R, level: Compression) -> ZlibRead<R> {
ZlibRead {
inner: read,
flushing: false,
compress: Compress::new(level, true),
}
}
}
27 changes: 26 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
pub mod read;
//! Adaptors between compression crates and Rust's modern asynchronous IO types.
//!
//!
//! # Organization
//!
//! This crate is divided up into a number of modules based on the underlying asynchronous IO type
//! that will be wrapped:
//!
//! * [`bufread`] provides types which operate over [`AsyncBufRead`](futures::io::AsyncBufRead)
//! streams
//! * [`stream`] provides types which operate over [`Stream`](futures::stream::Stream)`<Item =
//! `[`io::Result`](std::io::Result)`<`[`Bytes`](bytes::Bytes)`>>` streams

#![warn(missing_docs)]

pub mod bufread;
pub mod stream;

/// Types to configure [`flate2`](::flate2) based encoders.
pub mod flate2 {
pub use flate2::Compression;
}

/// Types to configure [`brotli2`](::brotli2) based encoders.
pub mod brotli2 {
pub use brotli2::CompressParams;
}
38 changes: 0 additions & 38 deletions src/read/brotli.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/read/mod.rs

This file was deleted.

91 changes: 58 additions & 33 deletions src/stream/brotli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,74 @@ use core::{
};
use std::io::{Error, ErrorKind, Result};

use brotli2::raw::{CoStatus, CompressOp, DeStatus, Decompress};
pub use brotli2::{raw::Compress, CompressParams};
use brotli2::{
raw::{CoStatus, Compress, CompressOp, DeStatus, Decompress},
CompressParams,
};
use bytes::{Bytes, BytesMut};
use futures::{ready, stream::Stream};
use pin_project::unsafe_project;

/// A brotli encoder, or compressor.
///
/// This structure implements a [`Stream`] interface and will read uncompressed data from an
/// underlying stream and emit a stream of compressed data.
#[unsafe_project(Unpin)]
pub struct BrotliStream<S: Stream<Item = Result<Bytes>>> {
pub struct BrotliEncoder<S: Stream<Item = Result<Bytes>>> {
#[pin]
inner: S,
flush: bool,
compress: Compress,
}

impl<S: Stream<Item = Result<Bytes>>> Stream for BrotliStream<S> {
/// A brotli decoder, or decompressor.
///
/// This structure implements a [`Stream`] interface and will read compressed data from an
/// underlying stream and emit a stream of uncompressed data.
#[unsafe_project(Unpin)]
pub struct BrotliDecoder<S: Stream<Item = Result<Bytes>>> {
#[pin]
inner: S,
flush: bool,
decompress: Decompress,
}

impl<S: Stream<Item = Result<Bytes>>> BrotliEncoder<S> {
/// Creates a new encoder which will read uncompressed data from the given stream and emit a
/// compressed stream.
///
/// The `level` argument here is typically 0-11.
pub fn new(stream: S, level: u32) -> BrotliEncoder<S> {
let mut params = CompressParams::new();
params.quality(level);
BrotliEncoder::from_params(stream, &params)
}

/// Creates a new encoder with a custom [`CompressParams`].
pub fn from_params(stream: S, params: &CompressParams) -> BrotliEncoder<S> {
let mut compress = Compress::new();
compress.set_params(params);
BrotliEncoder {
inner: stream,
flush: false,
compress,
}
}
}

impl<S: Stream<Item = Result<Bytes>>> BrotliDecoder<S> {
/// Creates a new decoder which will read compressed data from the given stream and emit an
/// uncompressed stream.
pub fn new(stream: S) -> BrotliDecoder<S> {
BrotliDecoder {
inner: stream,
flush: false,
decompress: Decompress::new(),
}
}
}

impl<S: Stream<Item = Result<Bytes>>> Stream for BrotliEncoder<S> {
type Item = Result<Bytes>;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes>>> {
Expand Down Expand Up @@ -63,25 +116,7 @@ impl<S: Stream<Item = Result<Bytes>>> Stream for BrotliStream<S> {
}
}

impl<S: Stream<Item = Result<Bytes>>> BrotliStream<S> {
pub fn new(stream: S, compress: Compress) -> BrotliStream<S> {
BrotliStream {
inner: stream,
flush: false,
compress,
}
}
}

#[unsafe_project(Unpin)]
pub struct DecompressedBrotliStream<S: Stream<Item = Result<Bytes>>> {
#[pin]
inner: S,
flush: bool,
decompress: Decompress,
}

impl<S: Stream<Item = Result<Bytes>>> Stream for DecompressedBrotliStream<S> {
impl<S: Stream<Item = Result<Bytes>>> Stream for BrotliDecoder<S> {
type Item = Result<Bytes>;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes>>> {
Expand Down Expand Up @@ -126,13 +161,3 @@ impl<S: Stream<Item = Result<Bytes>>> Stream for DecompressedBrotliStream<S> {
Poll::Ready(Some(Ok(decompressed_output.freeze())))
}
}

impl<S: Stream<Item = Result<Bytes>>> DecompressedBrotliStream<S> {
pub fn new(stream: S) -> DecompressedBrotliStream<S> {
DecompressedBrotliStream {
inner: stream,
flush: false,
decompress: Decompress::new(),
}
}
}
Loading