Skip to content

Commit a36090e

Browse files
authored
Merge pull request #14 from Nemo157/docs
Docs
2 parents ce66a92 + f6e0cba commit a36090e

18 files changed

+358
-368
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: rust
22
rust:
3-
- nightly-2019-04-25
3+
- nightly-2019-05-11
44

55
before_script: |
66
rustup component add rustfmt clippy

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ license = "MIT OR Apache-2.0"
99
brotli2 = "0.3.2"
1010
bytes = "0.4.12"
1111
flate2 = "1.0.7"
12-
futures-preview = "0.3.0-alpha.15"
12+
futures-preview = "0.3.0-alpha.16"
1313
pin-project = "0.3.2"
1414

1515
[dev-dependencies]
1616
proptest = "0.9.3"
1717
pin-utils = "0.1.0-alpha.4"
1818
proptest-derive = "0.1.1"
1919
rand = "0.6.5"
20+
futures-test-preview = "0.3.0-alpha.16"

src/read/deflate.rs src/bufread/deflate.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,38 @@ use core::{
44
};
55
use std::io::Result;
66

7-
pub use flate2::Compression;
8-
use flate2::{Compress, FlushCompress};
7+
use flate2::{Compress, Compression, FlushCompress};
98
use futures::{
109
io::{AsyncBufRead, AsyncRead},
1110
ready,
1211
};
1312
use pin_project::unsafe_project;
1413

14+
/// A DEFLATE encoder, or compressor.
15+
///
16+
/// This structure implements an [`AsyncRead`] interface and will read uncompressed data from an
17+
/// underlying stream and emit a stream of compressed data.
1518
#[unsafe_project(Unpin)]
16-
pub struct DeflateRead<R: AsyncBufRead> {
19+
pub struct DeflateEncoder<R: AsyncBufRead> {
1720
#[pin]
1821
inner: R,
1922
flushing: bool,
2023
compress: Compress,
2124
}
2225

23-
impl<R: AsyncBufRead> AsyncRead for DeflateRead<R> {
26+
impl<R: AsyncBufRead> DeflateEncoder<R> {
27+
/// Creates a new encoder which will read uncompressed data from the given stream and emit a
28+
/// compressed stream.
29+
pub fn new(read: R, level: Compression) -> DeflateEncoder<R> {
30+
DeflateEncoder {
31+
inner: read,
32+
flushing: false,
33+
compress: Compress::new(level, false),
34+
}
35+
}
36+
}
37+
38+
impl<R: AsyncBufRead> AsyncRead for DeflateEncoder<R> {
2439
fn poll_read(
2540
self: Pin<&mut Self>,
2641
cx: &mut Context<'_>,
@@ -50,13 +65,3 @@ impl<R: AsyncBufRead> AsyncRead for DeflateRead<R> {
5065
}
5166
}
5267
}
53-
54-
impl<R: AsyncBufRead> DeflateRead<R> {
55-
pub fn new(read: R, level: Compression) -> DeflateRead<R> {
56-
DeflateRead {
57-
inner: read,
58-
flushing: false,
59-
compress: Compress::new(level, false),
60-
}
61-
}
62-
}

src/bufread/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//! Types which operate over [`AsyncBufRead`](futures::io::AsyncBufRead) streams, both encoders and
2+
//! decoders for various formats.
3+
4+
mod deflate;
5+
mod zlib;
6+
7+
pub use deflate::DeflateEncoder;
8+
pub use zlib::ZlibEncoder;

src/read/zlib.rs src/bufread/zlib.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,38 @@ use core::{
44
};
55
use std::io::Result;
66

7-
pub use flate2::Compression;
8-
use flate2::{Compress, FlushCompress};
7+
use flate2::{Compress, Compression, FlushCompress};
98
use futures::{
109
io::{AsyncBufRead, AsyncRead},
1110
ready,
1211
};
1312
use pin_project::unsafe_project;
1413

14+
/// A zlib encoder, or compressor.
15+
///
16+
/// This structure implements an [`AsyncRead`] interface and will read uncompressed data from an
17+
/// underlying stream and emit a stream of compressed data.
1518
#[unsafe_project(Unpin)]
16-
pub struct ZlibRead<R: AsyncBufRead> {
19+
pub struct ZlibEncoder<R: AsyncBufRead> {
1720
#[pin]
1821
inner: R,
1922
flushing: bool,
2023
compress: Compress,
2124
}
2225

23-
impl<R: AsyncBufRead> AsyncRead for ZlibRead<R> {
26+
impl<R: AsyncBufRead> ZlibEncoder<R> {
27+
/// Creates a new encoder which will read uncompressed data from the given stream and emit a
28+
/// compressed stream.
29+
pub fn new(read: R, level: Compression) -> ZlibEncoder<R> {
30+
ZlibEncoder {
31+
inner: read,
32+
flushing: false,
33+
compress: Compress::new(level, true),
34+
}
35+
}
36+
}
37+
38+
impl<R: AsyncBufRead> AsyncRead for ZlibEncoder<R> {
2439
fn poll_read(
2540
self: Pin<&mut Self>,
2641
cx: &mut Context<'_>,
@@ -50,13 +65,3 @@ impl<R: AsyncBufRead> AsyncRead for ZlibRead<R> {
5065
}
5166
}
5267
}
53-
54-
impl<R: AsyncBufRead> ZlibRead<R> {
55-
pub fn new(read: R, level: Compression) -> ZlibRead<R> {
56-
ZlibRead {
57-
inner: read,
58-
flushing: false,
59-
compress: Compress::new(level, true),
60-
}
61-
}
62-
}

src/lib.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
1-
pub mod read;
1+
//! Adaptors between compression crates and Rust's modern asynchronous IO types.
2+
//!
3+
//!
4+
//! # Organization
5+
//!
6+
//! This crate is divided up into a number of modules based on the underlying asynchronous IO type
7+
//! that will be wrapped:
8+
//!
9+
//! * [`bufread`] provides types which operate over [`AsyncBufRead`](futures::io::AsyncBufRead)
10+
//! streams
11+
//! * [`stream`] provides types which operate over [`Stream`](futures::stream::Stream)`<Item =
12+
//! `[`io::Result`](std::io::Result)`<`[`Bytes`](bytes::Bytes)`>>` streams
13+
14+
#![warn(missing_docs)]
15+
16+
pub mod bufread;
217
pub mod stream;
18+
19+
/// Types to configure [`flate2`](::flate2) based encoders.
20+
pub mod flate2 {
21+
pub use flate2::Compression;
22+
}
23+
24+
/// Types to configure [`brotli2`](::brotli2) based encoders.
25+
pub mod brotli2 {
26+
pub use brotli2::CompressParams;
27+
}

src/read/brotli.rs

-38
This file was deleted.

src/read/mod.rs

-3
This file was deleted.

src/stream/brotli.rs

+58-33
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,74 @@ use core::{
44
};
55
use std::io::{Error, ErrorKind, Result};
66

7-
use brotli2::raw::{CoStatus, CompressOp, DeStatus, Decompress};
8-
pub use brotli2::{raw::Compress, CompressParams};
7+
use brotli2::{
8+
raw::{CoStatus, Compress, CompressOp, DeStatus, Decompress},
9+
CompressParams,
10+
};
911
use bytes::{Bytes, BytesMut};
1012
use futures::{ready, stream::Stream};
1113
use pin_project::unsafe_project;
1214

15+
/// A brotli encoder, or compressor.
16+
///
17+
/// This structure implements a [`Stream`] interface and will read uncompressed data from an
18+
/// underlying stream and emit a stream of compressed data.
1319
#[unsafe_project(Unpin)]
14-
pub struct BrotliStream<S: Stream<Item = Result<Bytes>>> {
20+
pub struct BrotliEncoder<S: Stream<Item = Result<Bytes>>> {
1521
#[pin]
1622
inner: S,
1723
flush: bool,
1824
compress: Compress,
1925
}
2026

21-
impl<S: Stream<Item = Result<Bytes>>> Stream for BrotliStream<S> {
27+
/// A brotli decoder, or decompressor.
28+
///
29+
/// This structure implements a [`Stream`] interface and will read compressed data from an
30+
/// underlying stream and emit a stream of uncompressed data.
31+
#[unsafe_project(Unpin)]
32+
pub struct BrotliDecoder<S: Stream<Item = Result<Bytes>>> {
33+
#[pin]
34+
inner: S,
35+
flush: bool,
36+
decompress: Decompress,
37+
}
38+
39+
impl<S: Stream<Item = Result<Bytes>>> BrotliEncoder<S> {
40+
/// Creates a new encoder which will read uncompressed data from the given stream and emit a
41+
/// compressed stream.
42+
///
43+
/// The `level` argument here is typically 0-11.
44+
pub fn new(stream: S, level: u32) -> BrotliEncoder<S> {
45+
let mut params = CompressParams::new();
46+
params.quality(level);
47+
BrotliEncoder::from_params(stream, &params)
48+
}
49+
50+
/// Creates a new encoder with a custom [`CompressParams`].
51+
pub fn from_params(stream: S, params: &CompressParams) -> BrotliEncoder<S> {
52+
let mut compress = Compress::new();
53+
compress.set_params(params);
54+
BrotliEncoder {
55+
inner: stream,
56+
flush: false,
57+
compress,
58+
}
59+
}
60+
}
61+
62+
impl<S: Stream<Item = Result<Bytes>>> BrotliDecoder<S> {
63+
/// Creates a new decoder which will read compressed data from the given stream and emit an
64+
/// uncompressed stream.
65+
pub fn new(stream: S) -> BrotliDecoder<S> {
66+
BrotliDecoder {
67+
inner: stream,
68+
flush: false,
69+
decompress: Decompress::new(),
70+
}
71+
}
72+
}
73+
74+
impl<S: Stream<Item = Result<Bytes>>> Stream for BrotliEncoder<S> {
2275
type Item = Result<Bytes>;
2376

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

66-
impl<S: Stream<Item = Result<Bytes>>> BrotliStream<S> {
67-
pub fn new(stream: S, compress: Compress) -> BrotliStream<S> {
68-
BrotliStream {
69-
inner: stream,
70-
flush: false,
71-
compress,
72-
}
73-
}
74-
}
75-
76-
#[unsafe_project(Unpin)]
77-
pub struct DecompressedBrotliStream<S: Stream<Item = Result<Bytes>>> {
78-
#[pin]
79-
inner: S,
80-
flush: bool,
81-
decompress: Decompress,
82-
}
83-
84-
impl<S: Stream<Item = Result<Bytes>>> Stream for DecompressedBrotliStream<S> {
119+
impl<S: Stream<Item = Result<Bytes>>> Stream for BrotliDecoder<S> {
85120
type Item = Result<Bytes>;
86121

87122
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes>>> {
@@ -126,13 +161,3 @@ impl<S: Stream<Item = Result<Bytes>>> Stream for DecompressedBrotliStream<S> {
126161
Poll::Ready(Some(Ok(decompressed_output.freeze())))
127162
}
128163
}
129-
130-
impl<S: Stream<Item = Result<Bytes>>> DecompressedBrotliStream<S> {
131-
pub fn new(stream: S) -> DecompressedBrotliStream<S> {
132-
DecompressedBrotliStream {
133-
inner: stream,
134-
flush: false,
135-
decompress: Decompress::new(),
136-
}
137-
}
138-
}

0 commit comments

Comments
 (0)