chksum_sha1/reader.rs
1//! This module is optional and can be enabled using the `reader` Cargo feature.
2//!
3//! The [`Reader`] allows on-the-fly calculation of the digest while reading the data.
4//!
5//! # Enabling
6//!
7//! Add the following entry to your `Cargo.toml` file to enable the `reader` feature:
8//!
9//! ```toml
10//! [dependencies]
11//! chksum-sha1 = { version = "0.1.0", features = ["reader"] }
12//! ```
13//!
14//! Alternatively, use the [`cargo add`](https://doc.rust-lang.org/cargo/commands/cargo-add.html) subcommand:
15//!
16//! ```shell
17//! cargo add chksum-sha1 --features reader
18//! ```
19//!
20//! # Example
21//!
22//! ```rust
23//! # use std::path::Path;
24//! use std::fs::File;
25//! use std::io::Read; // required by reader
26//!
27//! # use chksum_sha1::Result;
28//! use chksum_sha1 as sha1;
29//!
30//! # fn wrapper(path: &Path) -> Result<()> {
31//! let file = File::open(path)?;
32//! let mut reader = sha1::reader::new(file);
33//!
34//! let mut buffer = Vec::new();
35//! reader.read_to_end(&mut buffer)?;
36//! assert_eq!(buffer, b"example data");
37//!
38//! let digest = reader.digest();
39//! assert_eq!(
40//! digest.to_hex_lowercase(),
41//! "9fc42adac31303d68b444e6129f13f6093a0e045"
42//! );
43//! # Ok(())
44//! # }
45//! ```
46
47use std::io::Read;
48
49use chksum_reader as reader;
50#[cfg(feature = "async-runtime-tokio")]
51use tokio::io::AsyncRead;
52
53use crate::SHA1;
54
55/// A specialized [`Reader`](reader::Reader) type with the [`SHA1`] hash algorithm.
56pub type Reader<R> = reader::Reader<R, SHA1>;
57
58#[cfg(feature = "async-runtime-tokio")]
59/// A specialized [`AsyncReader`](reader::AsyncReader) type with the [`SHA1`] hash algorithm.
60pub type AsyncReader<R> = reader::AsyncReader<R, SHA1>;
61
62/// Creates new [`Reader`].
63pub fn new(inner: impl Read) -> Reader<impl Read> {
64 reader::new(inner)
65}
66
67/// Creates new [`Reader`] with provided hash.
68pub fn with_hash(inner: impl Read, hash: SHA1) -> Reader<impl Read> {
69 reader::with_hash(inner, hash)
70}
71
72#[cfg(feature = "async-runtime-tokio")]
73/// Creates new [`AsyncReader`].
74pub fn async_new(inner: impl AsyncRead) -> AsyncReader<impl AsyncRead> {
75 reader::async_new(inner)
76}
77
78#[cfg(feature = "async-runtime-tokio")]
79/// Creates new [`AsyncReader`] with provided hash.
80pub fn async_with_hash(inner: impl AsyncRead, hash: SHA1) -> AsyncReader<impl AsyncRead> {
81 reader::async_with_hash(inner, hash)
82}