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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use paste::paste;
use crate::{consts, FnvHasher, FnvHashResult};
macro_rules! create_fnvhasher {
($x:literal) => { paste! {
#[doc = "A FNV-1a hasher that produces a " $x "-bit output."]
///
/// # Examples:
///
/// ```
#[doc = "let mut hasher = Fnv" $x "::new();"]
/// hasher.update(bytes);
/// hasher.finalize();
/// ```
///
/// OR
///
/// ```
#[doc = "let hash = Fnv" $x "::hash(bytes);"]
/// ```
#[derive(Debug)]
pub struct [<Fnv $x>]([<u $x>]);
impl Default for [<Fnv $x>] {
#[inline]
fn default() -> [<Fnv $x>] {
[<Fnv $x>](consts::[<FNV_OFFSET_ $x>])
}
}
impl FnvHasher for [<Fnv $x>] {
#[doc = "Creates a new default `Fnv" $x "` object."]
///
/// # Example:
///
/// ```
#[doc = "let hasher = Fnv" $x "::new();"]
/// ```
#[inline]
fn new() -> Self {
[<Fnv $x>]::default()
}
#[doc = "Incrementally update the `Fnv" $x "` object."]
///
/// This method is best used when you need to update the hasher multiple times.
/// If you only need to hash a single piece of data, consider using the [`hash`] method instead.
///
#[doc = "[`hash`]: Fnv" $x "::hash"]
///
/// # Example:
///
/// ```
/// hasher.update(bytes);
/// ```
#[inline]
fn update(&mut self, bytes: &[u8]) {
let mut hash = self.0;
for byte in bytes.into_iter() {
hash = hash ^ (*byte as [<u $x>]);
hash = hash.wrapping_mul(consts::[<FNV_PRIME_ $x>]);
}
self.0 = hash;
}
#[doc = "Finalize the `Fnv" $x "` object."]
///
/// # Example:
///
/// ```
/// hasher.finalize();
/// ```
#[inline]
fn finalize(&self) -> FnvHashResult {
FnvHashResult::[<from_u $x>](self.0)
}
#[doc = "One-time use of the `Fnv" $x "` object."]
///
/// Using this method is shorthand for the following:
///
/// ```
#[doc = "let mut hasher = Fnv" $x "::new();"]
/// hasher.update(bytes);
/// hasher.finalize();
/// ```
/// # Example:
///
/// ```
#[doc = "let hash = Fnv" $x "::hash(bytes);"]
/// ```
#[inline]
fn hash<T: AsRef<[u8]>>(bytes: T) -> FnvHashResult {
let mut hash = consts::[<FNV_OFFSET_ $x>];
for byte in bytes.as_ref().into_iter() {
hash = hash ^ (*byte as [<u $x>]);
hash = hash.wrapping_mul(consts::[<FNV_PRIME_ $x>]);
}
FnvHashResult::[<from_u $x>](hash)
}
}
impl From<[<Fnv $x>]> for [<u $x>] {
fn from(value: [<Fnv $x>]) -> Self {
value.0
}
}
}}
}
macro_rules! create_fnvhasher_bigint {
($x:literal) => { paste! {
#[doc = "A FNV-1a hasher that produces a " $x "-bit output."]
///
/// # Examples:
///
/// ```
#[doc = "let mut hasher = Fnv" $x "::new();"]
/// hasher.update(bytes);
/// hasher.finalize();
/// ```
///
/// OR
///
/// ```
#[doc = "let hash = Fnv" $x "::hash(bytes);"]
/// ```
#[derive(Debug)]
#[cfg(feature = "bigint")]
pub struct [<Fnv $x>]([<U $x>]);
#[cfg(feature = "bigint")]
impl Default for [<Fnv $x>] {
#[inline]
fn default() -> [<Fnv $x>] {
[<Fnv $x>](consts::[<FNV_OFFSET_ $x>])
}
}
#[cfg(feature = "bigint")]
impl FnvHasher for [<Fnv $x>] {
#[doc = "Creates a new default `Fnv" $x "` object."]
///
/// # Example:
///
/// ```
#[doc = "let hasher = Fnv" $x "::new();"]
/// ```
#[inline]
fn new() -> [<Fnv $x>] {
[<Fnv $x>]::default()
}
#[doc = "Incrementally update the `Fnv" $x "` object."]
///
/// This method is best used when you need to update the hasher multiple times.
/// If you only need to hash a single piece of data, consider using the [`hash`] method instead.
///
#[doc = "[`hash`]: Fnv" $x "::hash"]
///
/// # Example:
///
/// ```
/// hasher.update(bytes);
/// ```
#[inline]
fn update(&mut self, bytes: &[u8]) {
let mut hash = self.0;
for byte in bytes.into_iter() {
hash = hash ^ (UInt::<{$x / 64}>::from(*byte));
hash = hash.wrapping_mul(&consts::[<FNV_PRIME_ $x>]);
}
self.0 = hash;
}
#[doc = "Finalize the `Fnv" $x "` object."]
///
/// # Example:
///
/// ```
/// hasher.finalize();
/// ```
#[inline]
fn finalize(&self) -> FnvHashResult {
FnvHashResult::from_bigint(self.0)
}
#[doc = "One-time use of the `Fnv" $x "` object."]
///
/// Using this method is shorthand for the following:
///
/// ```
#[doc = "let mut hasher = Fnv" $x "::new();"]
/// hasher.update(bytes);
/// hasher.finalize();
/// ```
/// # Example:
///
/// ```
#[doc = "let hash = Fnv" $x "::hash(bytes);"]
/// ```
#[inline]
fn hash<T: AsRef<[u8]>>(bytes: T) -> FnvHashResult {
let mut hash = consts::[<FNV_OFFSET_ $x>];
for byte in bytes.as_ref().into_iter() {
hash = hash ^ (UInt::<{$x / 64}>::from(*byte));
hash = hash.wrapping_mul(&consts::[<FNV_PRIME_ $x>]);
}
FnvHashResult::from_bigint(hash)
}
}
}}
}
pub(crate) use create_fnvhasher;
pub(crate) use create_fnvhasher_bigint;
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_macros() {
create_fnvhasher!(32);
create_fnvhasher_bigint!(256);
}
}