murmur2/
lib.rs

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
//! # murmur2 Hash Functions
//!
//! This is a pure Rust implementation of the murmur2 hash functions.
//! It is tested against the [original C++ implementation](https://github.com/aappleby/smhasher/).
//! To keep this crate `no_std` and dependency free,
//! the tests live in a different crate, which is not published to crates.io.
//! The implementations have not been particularly optimized for performance.
//!
//! The original C++ implementations are architecture/endianness dependent.
//! The only independent function provided is `MurmurHashNeutral2`.
//! This crate does not follow that scheme,
//! all functions are provided in an endianness-dependent and -independent version.
//! The endianness-dependent versions have their name suffixed with a `ne`, for "native endian".
#![cfg_attr(not(test), no_std)]

#[macro_use]
mod hlp;
mod imp;
#[cfg(test)]
mod test;

/// Endianness-independent `MurmurHash2`
///
/// This is the only function where an endianness-independent version is provided by the original C++ code,
/// called `MurmurHashNeutral2` there.
pub fn murmur2(data: &[u8], seed: u32) -> u32 {
    imp::murmur2(data, seed, u32::from_le_bytes)
}

/// `MurmurHash2`
pub fn murmur2ne(data: &[u8], seed: u32) -> u32 {
    imp::murmur2(data, seed, u32::from_ne_bytes)
}

/// Endianness-independent `MurmurHash2A`
pub fn murmur2a(data: &[u8], seed: u32) -> u32 {
    imp::murmur2a(data, seed, u32::from_le_bytes)
}

/// `MurmurHash2A`
pub fn murmur2ane(data: &[u8], seed: u32) -> u32 {
    imp::murmur2a(data, seed, u32::from_ne_bytes)
}

/// Endianness-independent `MurmurHash64A`
pub fn murmur64a(data: &[u8], seed: u64) -> u64 {
    imp::murmur64a(data, seed, u64::from_le_bytes)
}

/// `MurmurHash64A`
pub fn murmur64ane(data: &[u8], seed: u64) -> u64 {
    imp::murmur64a(data, seed, u64::from_ne_bytes)
}

/// Endianness-independent `MurmurHash64B`
pub fn murmur64b(data: &[u8], seed: u64) -> u64 {
    imp::murmur64b(data, seed, u32::from_le_bytes)
}

/// `MurmurHash64B`
pub fn murmur64bne(data: &[u8], seed: u64) -> u64 {
    imp::murmur64b(data, seed, u32::from_ne_bytes)
}

/// Seed found in [Kafka source](https://github.com/apache/kafka/blob/3.1.0/clients/src/main/java/org/apache/kafka/common/utils/Utils.java#L479).
// No idea where they took it from
pub const KAFKA_SEED: u32 = 0x9747b28c;