Skip to main content

hdds_gen/codegen/
type_hash.rs

1// SPDX-License-Identifier: Apache-2.0 OR MIT
2// Copyright (c) 2025-2026 naskel.com
3
4/// Compute 32-bit FNV-1a hash for fully qualified type names.
5///
6/// This matches the algorithm used in RTPS/XTypes for deterministic type IDs.
7#[must_use]
8pub fn compute_type_id(fqn: &str) -> u32 {
9    const FNV_PRIME: u32 = 16_777_619;
10    const FNV_OFFSET: u32 = 2_166_136_261;
11
12    let mut hash = FNV_OFFSET;
13    for byte in fqn.as_bytes() {
14        hash ^= u32::from(*byte);
15        hash = hash.wrapping_mul(FNV_PRIME);
16    }
17    hash
18}
19
20#[cfg(test)]
21mod tests {
22    use super::compute_type_id;
23
24    #[test]
25    fn test_fnv_reproducible() {
26        let name = "sensor::Temperature";
27        let id1 = compute_type_id(name);
28        let id2 = compute_type_id(name);
29        assert_eq!(id1, id2);
30    }
31}