1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crc32fast::Hasher;
use digest::{impl_write, FixedOutput, Input, Reset};
use generic_array::typenum::U4;
use generic_array::GenericArray;
pub use digest::Digest;
#[derive(Clone, Default)]
pub struct Crc32(Hasher);
impl Crc32 {
#[inline]
pub fn new() -> Self {
Self(Hasher::new())
}
#[inline]
pub fn from_state(state: u32) -> Self {
Self(Hasher::new_with_initial(state))
}
}
impl FixedOutput for Crc32 {
type OutputSize = U4;
#[inline]
fn fixed_result(self) -> GenericArray<u8, Self::OutputSize> {
let result = self.0.finalize();
GenericArray::clone_from_slice(&result.to_be_bytes())
}
}
impl Input for Crc32 {
#[inline]
fn input<B: AsRef<[u8]>>(&mut self, data: B) {
self.0.update(data.as_ref());
}
}
impl Reset for Crc32 {
#[inline]
fn reset(&mut self) {
self.0.reset();
}
}
impl_write!(Crc32);