Skip to main content

sim_incremental_core/
fingerprint.rs

1//! Stable value fingerprints used for cutoff.
2
3use std::{
4    collections::hash_map::DefaultHasher,
5    hash::{Hash, Hasher},
6};
7
8/// A compact fingerprint for a memoized query value.
9#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
10pub struct ValueFingerprint(u64);
11
12impl ValueFingerprint {
13    /// Creates a fingerprint from an already-computed stable integer.
14    #[must_use]
15    pub const fn new(value: u64) -> Self {
16        Self(value)
17    }
18
19    /// Returns the raw fingerprint bits.
20    #[must_use]
21    pub const fn get(self) -> u64 {
22        self.0
23    }
24}
25
26/// Computes the fingerprint an incremental memo uses for cutoff.
27pub trait FingerprintValue {
28    /// Returns a compact value identity for incremental cutoff.
29    fn incremental_fingerprint(&self) -> ValueFingerprint;
30}
31
32impl<T> FingerprintValue for T
33where
34    T: Hash,
35{
36    fn incremental_fingerprint(&self) -> ValueFingerprint {
37        let mut hasher = DefaultHasher::new();
38        self.hash(&mut hasher);
39        ValueFingerprint(hasher.finish())
40    }
41}