Skip to content

Commit

Permalink
Make Clippy happier
Browse files Browse the repository at this point in the history
Address a few simple clippy suggestions, and added a few of them to the "allowed" list.
  • Loading branch information
nyurik committed Dec 10, 2023
1 parent d908ffa commit 9b016ff
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ use core::default::Default;
#[cfg(not(feature = "std"))]
use core::hash::{Hasher, BuildHasherDefault};

const INITIAL_STATE: u64 = 0xcbf29ce484222325;
const PRIME: u64 = 0x100000001b3;
const INITIAL_STATE: u64 = 0xcbf2_9ce4_8422_2325;
const PRIME: u64 = 0x0100_0000_01b3;

/// An implementation of the Fowler–Noll–Vo hash function.
///
Expand All @@ -103,6 +103,7 @@ impl FnvHasher {
/// Create an FNV hasher starting with a state corresponding
/// to the hash `key`.
#[inline]
#[must_use]
pub fn with_key(key: u64) -> FnvHasher {
FnvHasher(key)
}
Expand All @@ -118,8 +119,8 @@ impl Hasher for FnvHasher {
fn write(&mut self, bytes: &[u8]) {
let FnvHasher(mut hash) = *self;

for byte in bytes.iter() {
hash = hash ^ (*byte as u64);
for byte in bytes {
hash ^= u64::from(*byte);
hash = hash.wrapping_mul(PRIME);
}

Expand All @@ -141,11 +142,12 @@ pub type FnvHashSet<T> = HashSet<T, FnvBuildHasher>;

/// Const version of FNV hash.
#[inline]
#[must_use]
pub const fn fnv_hash(bytes: &[u8]) -> u64 {
let mut hash = INITIAL_STATE;
let mut i = 0;
while i < bytes.len() {
hash = hash ^ (bytes[i] as u64);
hash ^= bytes[i] as u64;
hash = hash.wrapping_mul(PRIME);
i += 1;
}
Expand All @@ -168,14 +170,15 @@ mod test {
}

fn repeat_10(bytes: &[u8]) -> Vec<u8> {
(0..10).flat_map(|_| bytes.iter().cloned()).collect()
(0..10).flat_map(|_| bytes.iter().copied()).collect()
}

fn repeat_500(bytes: &[u8]) -> Vec<u8> {
(0..500).flat_map(|_| bytes.iter().cloned()).collect()
(0..500).flat_map(|_| bytes.iter().copied()).collect()
}

#[test]
#[allow(clippy::unreadable_literal, clippy::too_many_lines)]
fn basic_tests() {
assert_eq!(fnv1a(b""), 0xcbf29ce484222325);
assert_eq!(fnv1a(b"a"), 0xaf63dc4c8601ec8c);
Expand Down

0 comments on commit 9b016ff

Please sign in to comment.