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
···
- name: Format check
command: cargo fmt --all --check
- name: Tests
-
command: cargo test --workspace
+
command: cargo test --workspace --locked
+4
Cargo.lock
···
]
[[package]]
+
name = "sachy-fnv"
+
version = "0.1.0"
+
+
[[package]]
name = "sachy-shtc3"
version = "0.1.0"
dependencies = [
+1 -1
Cargo.toml
···
[workspace]
resolver = "3"
-
members = ["sachy-battery","sachy-bthome", "sachy-fmt", "sachy-shtc3"]
+
members = ["sachy-battery","sachy-bthome", "sachy-fmt", "sachy-fnv", "sachy-shtc3"]
[workspace.package]
authors = ["Sachymetsu <sachymetsu@tutamail.com>"]
+10
sachy-fnv/Cargo.toml
···
+
[package]
+
name = "sachy-fnv"
+
authors.workspace = true
+
edition.workspace = true
+
repository.workspace = true
+
license.workspace = true
+
version.workspace = true
+
rust-version.workspace = true
+
+
[dependencies]
+43
sachy-fnv/src/lib.rs
···
+
//! 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);
+
}
+
}