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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#![cfg_attr(not(any(feature = "std", test)), no_std)]
#[doc(hidden)]
pub mod baseline;
#[doc(hidden)]
pub mod specialized;
#[cfg(not(feature = "std"))]
use core::hash::Hasher;
#[cfg(feature = "std")]
use std::hash::Hasher;
const DEFAULT_INIT_STATE: u32 = 1;
#[derive(Copy, Clone, Debug)]
enum State {
Baseline(baseline::State),
Specialized(specialized::State),
}
#[derive(Copy, Clone, Debug)]
pub struct Adler32 {
state: State,
}
impl Adler32 {
pub fn new() -> Self {
Self::from(DEFAULT_INIT_STATE)
}
pub fn as_u32(&self) -> u32 {
match self.state {
State::Baseline(state) => state.finalize(),
State::Specialized(state) => state.finalize(),
}
}
pub fn is_simd_enabled(&self) -> bool {
match self.state {
State::Specialized(_) => true,
_ => false,
}
}
pub fn reset(&mut self) {
match self.state {
State::Baseline(ref mut state) => state.reset(),
State::Specialized(ref mut state) => state.reset(),
}
}
pub fn update(&mut self, buf: &[u8]) {
match self.state {
State::Baseline(ref mut state) => state.update(buf),
State::Specialized(ref mut state) => state.update(buf),
}
}
fn internal_new_baseline(initial: u32) -> Self {
Self {
state: State::Baseline(baseline::State::new(initial)),
}
}
#[doc(hidden)]
fn internal_new_specialized(initial: u32) -> Option<Self> {
specialized::State::new(initial).map(|state| Self {
state: State::Specialized(state),
})
}
}
impl Default for Adler32 {
fn default() -> Self {
Self::new()
}
}
impl From<u32> for Adler32 {
fn from(initial: u32) -> Self {
Self::internal_new_specialized(initial)
.unwrap_or_else(|| Self::internal_new_baseline(initial))
}
}
impl Hasher for Adler32 {
fn finish(&self) -> u64 {
u64::from(self.as_u32())
}
fn write(&mut self, bytes: &[u8]) {
self.update(bytes);
}
}
impl PartialEq<u32> for Adler32 {
fn eq(&self, &other: &u32) -> bool {
self.as_u32() == other
}
}