Skip to main content

sim_incremental_core/
fingerprint.rs

1//! Process-local value fingerprints used only for recomputation cutoff.
2
3use std::{
4    collections::hash_map::DefaultHasher,
5    hash::{Hash, Hasher},
6};
7
8/// A compact process-local hint for a memoized query value.
9///
10/// This value must never cross a persistence boundary or authorize an effect,
11/// replay, omission, or durable reuse decision. A collision may only affect
12/// cutoff behavior inside one live engine.
13#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
14pub struct ValueFingerprint(u64);
15
16impl ValueFingerprint {
17    /// Creates a process-local hint from an already-computed integer.
18    #[must_use]
19    pub const fn new(value: u64) -> Self {
20        Self(value)
21    }
22
23    /// Returns the raw fingerprint bits.
24    #[must_use]
25    pub const fn get(self) -> u64 {
26        self.0
27    }
28}
29
30/// Computes the process-local fingerprint an incremental memo uses for cutoff.
31pub trait FingerprintValue {
32    /// Returns a compact value identity for incremental cutoff.
33    fn incremental_fingerprint(&self) -> ValueFingerprint;
34}
35
36impl<T> FingerprintValue for T
37where
38    T: Hash,
39{
40    fn incremental_fingerprint(&self) -> ValueFingerprint {
41        let mut hasher = DefaultHasher::new();
42        self.hash(&mut hasher);
43        ValueFingerprint(hasher.finish())
44    }
45}