Skip to content

Commit 8ad8fd7

Browse files
authored
Merge pull request #512 from jongiddy/explain-module-choice
Explain rationale for choosing bufread,read,write modules
2 parents b45e5f7 + 5020778 commit 8ad8fd7

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ crc32fast = "1.2.0"
3131
miniz_oxide = { version = "0.8.5", default-features = false, features = ["with-alloc", "simd"] }
3232

3333
[dev-dependencies]
34+
futures = { version = "0.3", default-features = false }
3435
rand = "0.9"
3536
quickcheck = { version = "1.0", default-features = false }
3637

src/lib.rs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,46 @@
3232
//!
3333
//! # Organization
3434
//!
35-
//! This crate consists mainly of three modules, [`read`], [`write`], and
36-
//! [`bufread`]. Each module contains a number of types used to encode and
37-
//! decode various streams of data.
35+
//! This crate consists of three main modules: `bufread`, `read`, and `write`. Each module
36+
//! implements DEFLATE, zlib, and gzip for [`std::io::BufRead`] input types, [`std::io::Read`] input
37+
//! types, and [`std::io::Write`] output types respectively.
3838
//!
39-
//! All types in the [`write`] module work on instances of [`Write`][write],
40-
//! whereas all types in the [`read`] module work on instances of
41-
//! [`Read`][read] and [`bufread`] works with [`BufRead`][bufread]. If you
42-
//! are decoding directly from a `&[u8]`, use the [`bufread`] types.
39+
//! Use the [`mod@bufread`] implementations if you can provide a `BufRead` type for the input.
40+
//! The `&[u8]` slice type implements the `BufRead` trait.
41+
//!
42+
//! The [`mod@read`] implementations conveniently wrap a `Read` type in a `BufRead` implementation.
43+
//! However, the `read` implementations may
44+
//! [read past the end of the input data](https://github.com/rust-lang/flate2-rs/issues/338),
45+
//! making the `Read` type useless for subsequent reads of the input. If you need to re-use the
46+
//! `Read` type, wrap it in a [`std::io::BufReader`], use the `bufread` implementations,
47+
//! and perform subsequent reads on the `BufReader`.
48+
//!
49+
//! The [`mod@write`] implementations are most useful when there is no way to create a `BufRead`
50+
//! type, notably when reading async iterators (streams).
4351
//!
4452
//! ```
45-
//! use flate2::write::GzEncoder;
46-
//! use flate2::Compression;
47-
//! use std::io;
48-
//! use std::io::prelude::*;
49-
//!
50-
//! # fn main() { let _ = run(); }
51-
//! # fn run() -> io::Result<()> {
52-
//! let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
53-
//! encoder.write_all(b"Example")?;
54-
//! # Ok(())
55-
//! # }
53+
//! use futures::{Stream, StreamExt};
54+
//! use std::io::{Result, Write as _};
55+
//!
56+
//! async fn decompress_gzip_stream<S, I>(stream: S) -> Result<Vec<u8>>
57+
//! where
58+
//! S: Stream<Item = I>,
59+
//! I: AsRef<[u8]>
60+
//! {
61+
//! let mut stream = std::pin::pin!(stream);
62+
//! let mut w = Vec::<u8>::new();
63+
//! let mut decoder = flate2::write::GzDecoder::new(w);
64+
//! while let Some(input) = stream.next().await {
65+
//! decoder.write_all(input.as_ref())?;
66+
//! }
67+
//! decoder.finish()
68+
//! }
5669
//! ```
5770
//!
5871
//!
59-
//! Other various types are provided at the top-level of the crate for
60-
//! management and dealing with encoders/decoders. Also note that types which
61-
//! operate over a specific trait often implement the mirroring trait as well.
62-
//! For example a `flate2::read::DeflateDecoder<T>` *also* implements the
63-
//! `Write` trait if `T: Write`. That is, the "dual trait" is forwarded directly
72+
//! Note that types which operate over a specific trait often implement the mirroring trait as well.
73+
//! For example a `bufread::DeflateDecoder<T>` *also* implements the
74+
//! [`Write`] trait if `T: Write`. That is, the "dual trait" is forwarded directly
6475
//! to the underlying object if available.
6576
//!
6677
//! # About multi-member Gzip files
@@ -79,14 +90,12 @@
7990
//! emit an error after decoding the gzip data. This behavior matches the `gzip`,
8091
//! `gunzip`, and `zcat` command line tools.
8192
//!
82-
//! [`read`]: read/index.html
83-
//! [`bufread`]: bufread/index.html
84-
//! [`write`]: write/index.html
85-
//! [read]: https://doc.rust-lang.org/std/io/trait.Read.html
86-
//! [write]: https://doc.rust-lang.org/std/io/trait.Write.html
87-
//! [bufread]: https://doc.rust-lang.org/std/io/trait.BufRead.html
88-
//! [`GzDecoder`]: read/struct.GzDecoder.html
89-
//! [`MultiGzDecoder`]: read/struct.MultiGzDecoder.html
93+
//! [`Bufread`]: std::io::BufRead
94+
//! [`BufReader`]: std::io::BufReader
95+
//! [`Read`]: std::io::Read
96+
//! [`Write`]: std::io::Write
97+
//! [`GzDecoder`]: bufread::GzDecoder
98+
//! [`MultiGzDecoder`]: bufread::MultiGzDecoder
9099
#![doc(html_root_url = "https://docs.rs/flate2/0.2")]
91100
#![deny(missing_docs)]
92101
#![deny(missing_debug_implementations)]

0 commit comments

Comments
 (0)