Repo of no-std crates for my personal embedded projects

feat: FNV hasher crate

Changed files
+59 -2
.tangled
workflows
sachy-fnv
+1 -1
.tangled/workflows/test.yml
··· 14 14 - name: Format check 15 15 command: cargo fmt --all --check 16 16 - name: Tests 17 - command: cargo test --workspace 17 + command: cargo test --workspace --locked
+4
Cargo.lock
··· 363 363 ] 364 364 365 365 [[package]] 366 + name = "sachy-fnv" 367 + version = "0.1.0" 368 + 369 + [[package]] 366 370 name = "sachy-shtc3" 367 371 version = "0.1.0" 368 372 dependencies = [
+1 -1
Cargo.toml
··· 1 1 [workspace] 2 2 resolver = "3" 3 - members = ["sachy-battery","sachy-bthome", "sachy-fmt", "sachy-shtc3"] 3 + members = ["sachy-battery","sachy-bthome", "sachy-fmt", "sachy-fnv", "sachy-shtc3"] 4 4 5 5 [workspace.package] 6 6 authors = ["Sachymetsu <sachymetsu@tutamail.com>"]
+10
sachy-fnv/Cargo.toml
··· 1 + [package] 2 + name = "sachy-fnv" 3 + authors.workspace = true 4 + edition.workspace = true 5 + repository.workspace = true 6 + license.workspace = true 7 + version.workspace = true 8 + rust-version.workspace = true 9 + 10 + [dependencies]
+43
sachy-fnv/src/lib.rs
··· 1 + //! This crate provides a `const` FNV32 hasher for the purpose of supporting the ESPHome/Home Assistant protocol where required. 2 + //! 3 + //! ``` 4 + //! use sachy_fnv::fnv32_hash; 5 + //! 6 + //! let hash = fnv32_hash("water_pump_1"); 7 + //! 8 + //! assert_eq!(hash, 3557162331); 9 + //! ``` 10 + #![no_std] 11 + 12 + const FNV_OFFSET_BASIS_32: u32 = 0x811c9dc5; 13 + const FNV_PRIME_32: u32 = 0x01000193; 14 + 15 + /// `const` FNV32 hasher. Takes a `&str` and returns a `u32` hash. 16 + pub const fn fnv32_hash(str: &str) -> u32 { 17 + let bytes = str.as_bytes(); 18 + 19 + let mut hash = FNV_OFFSET_BASIS_32; 20 + let mut i = 0; 21 + let len = bytes.len(); 22 + 23 + while i < len { 24 + hash = hash.wrapping_mul(FNV_PRIME_32); 25 + hash ^= bytes[i] as u32; 26 + i += 1; 27 + } 28 + 29 + hash 30 + } 31 + 32 + #[cfg(test)] 33 + mod tests { 34 + use super::*; 35 + 36 + #[test] 37 + fn it_works() { 38 + let result = fnv32_hash("water_pump_1"); 39 + assert_eq!(result, 3557162331); 40 + let result = fnv32_hash("pump_1_duration"); 41 + assert_eq!(result, 931766070); 42 + } 43 + }