|
32 | 32 | //! |
33 | 33 | //! # Organization |
34 | 34 | //! |
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. |
38 | 38 | //! |
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). |
43 | 51 | //! |
44 | 52 | //! ``` |
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 | +//! } |
56 | 69 | //! ``` |
57 | 70 | //! |
58 | 71 | //! |
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 |
64 | 75 | //! to the underlying object if available. |
65 | 76 | //! |
66 | 77 | //! # About multi-member Gzip files |
|
79 | 90 | //! emit an error after decoding the gzip data. This behavior matches the `gzip`, |
80 | 91 | //! `gunzip`, and `zcat` command line tools. |
81 | 92 | //! |
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 |
90 | 99 | #![doc(html_root_url = "https://docs.rs/flate2/0.2")] |
91 | 100 | #![deny(missing_docs)] |
92 | 101 | #![deny(missing_debug_implementations)] |
|
0 commit comments