//! This crate provides a `const` FNV32 hasher for the purpose of supporting the ESPHome/Home Assistant protocol where required. //! //! ``` //! use sachy_fnv::fnv32_hash; //! //! let hash = fnv32_hash("water_pump_1"); //! //! assert_eq!(hash, 3557162331); //! ``` #![no_std] const FNV_OFFSET_BASIS_32: u32 = 0x811c9dc5; const FNV_PRIME_32: u32 = 0x01000193; /// `const` FNV32 hasher. Takes a `&str` and returns a `u32` hash. pub const fn fnv32_hash(str: &str) -> u32 { let bytes = str.as_bytes(); let mut hash = FNV_OFFSET_BASIS_32; let mut i = 0; let len = bytes.len(); while i < len { hash = hash.wrapping_mul(FNV_PRIME_32); hash ^= bytes[i] as u32; i += 1; } hash } #[cfg(test)] mod tests { use super::*; #[test] fn it_works() { let result = fnv32_hash("water_pump_1"); assert_eq!(result, 3557162331); let result = fnv32_hash("pump_1_duration"); assert_eq!(result, 931766070); } }