Skip to content

Commit 9b2eca6

Browse files
authored
Merge pull request #540 from MikkelPaulson/add-crc-wrapper
Add a wrapper for the `Crc` struct
2 parents 062b9dc + 585311b commit 9b2eca6

1 file changed

Lines changed: 51 additions & 34 deletions

File tree

src/crc.rs

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,81 @@
33
use std::io;
44
use std::io::prelude::*;
55

6-
#[cfg(not(feature = "zlib-rs"))]
7-
pub use impl_crc32fast::Crc;
6+
/// The CRC calculated by a [`CrcReader`].
7+
#[derive(Debug, Default)]
8+
pub struct Crc {
9+
inner: inner::Crc,
10+
}
811

9-
#[cfg(feature = "zlib-rs")]
10-
pub use impl_zlib_rs::Crc;
12+
impl Crc {
13+
/// Create a new CRC.
14+
pub fn new() -> Self {
15+
Self::default()
16+
}
17+
18+
/// Returns the current crc32 checksum.
19+
pub fn sum(&self) -> u32 {
20+
self.inner.sum()
21+
}
22+
23+
/// The number of bytes that have been used to calculate the CRC.
24+
/// This value is only accurate if the amount is lower than 2<sup>32</sup>.
25+
pub fn amount(&self) -> u32 {
26+
self.inner.amount()
27+
}
28+
29+
/// Update the CRC with the bytes in `data`.
30+
pub fn update(&mut self, data: &[u8]) {
31+
self.inner.update(data);
32+
}
33+
34+
/// Reset the CRC, to start a new hash.
35+
///
36+
/// Do this in favor of creating a new `Crc` instance.
37+
pub fn reset(&mut self) {
38+
self.inner.reset();
39+
}
40+
41+
/// Combine the CRC with the CRC for the subsequent block of bytes.
42+
pub fn combine(&mut self, additional_crc: &Self) {
43+
self.inner.combine(&additional_crc.inner);
44+
}
45+
}
1146

1247
#[cfg(not(feature = "zlib-rs"))]
13-
mod impl_crc32fast {
48+
mod inner {
1449
use crc32fast::Hasher;
1550

16-
/// The CRC calculated by a [`CrcReader`].
17-
///
18-
/// [`CrcReader`]: struct.CrcReader.html
1951
#[derive(Debug, Default)]
2052
pub struct Crc {
2153
amt: u32,
2254
hasher: Hasher,
2355
}
2456

2557
impl Crc {
26-
/// Create a new CRC.
27-
pub fn new() -> Self {
28-
Self::default()
29-
}
30-
31-
/// Returns the current crc32 checksum.
58+
#[inline]
3259
pub fn sum(&self) -> u32 {
3360
self.hasher.clone().finalize()
3461
}
3562

36-
/// The number of bytes that have been used to calculate the CRC.
37-
/// This value is only accurate if the amount is lower than 2<sup>32</sup>.
63+
#[inline]
3864
pub fn amount(&self) -> u32 {
3965
self.amt
4066
}
4167

42-
/// Update the CRC with the bytes in `data`.
68+
#[inline]
4369
pub fn update(&mut self, data: &[u8]) {
4470
self.amt = self.amt.wrapping_add(data.len() as u32);
4571
self.hasher.update(data);
4672
}
4773

48-
/// Reset the CRC.
74+
#[inline]
4975
pub fn reset(&mut self) {
5076
self.amt = 0;
5177
self.hasher.reset();
5278
}
5379

54-
/// Combine the CRC with the CRC for the subsequent block of bytes.
80+
#[inline]
5581
pub fn combine(&mut self, additional_crc: &Self) {
5682
self.amt = self.amt.wrapping_add(additional_crc.amt);
5783
self.hasher.combine(&additional_crc.hasher);
@@ -60,46 +86,37 @@ mod impl_crc32fast {
6086
}
6187

6288
#[cfg(feature = "zlib-rs")]
63-
mod impl_zlib_rs {
64-
/// The CRC calculated by a [`CrcReader`].
65-
///
66-
/// [`CrcReader`]: struct.CrcReader.html
89+
mod inner {
6790
#[derive(Debug, Default)]
6891
pub struct Crc {
6992
consumed: u64,
7093
state: u32,
7194
}
7295

7396
impl Crc {
74-
/// Create a new CRC.
75-
pub fn new() -> Self {
76-
Self::default()
77-
}
78-
79-
/// Returns the current crc32 checksum.
97+
#[inline]
8098
pub fn sum(&self) -> u32 {
8199
self.state
82100
}
83101

84-
/// The number of bytes that have been used to calculate the CRC.
85-
/// This value is only accurate if the amount is lower than 2<sup>32</sup>.
102+
#[inline]
86103
pub fn amount(&self) -> u32 {
87104
self.consumed as u32
88105
}
89106

90-
/// Update the CRC with the bytes in `data`.
107+
#[inline]
91108
pub fn update(&mut self, data: &[u8]) {
92109
self.consumed = self.consumed.wrapping_add(data.len() as u64);
93110
self.state = zlib_rs::crc32::crc32(self.state, data);
94111
}
95112

96-
/// Reset the CRC.
113+
#[inline]
97114
pub fn reset(&mut self) {
98115
self.consumed = 0;
99116
self.state = 0
100117
}
101118

102-
/// Combine the CRC with the CRC for the subsequent block of bytes.
119+
#[inline]
103120
pub fn combine(&mut self, additional_crc: &Self) {
104121
self.consumed = self.consumed.wrapping_add(additional_crc.consumed);
105122
self.state = zlib_rs::crc32::crc32_combine(

0 commit comments

Comments
 (0)