xapi_rs/data/
fingerprint.rs

1// SPDX-License-Identifier: GPL-3.0-or-later
2
3use std::hash::{DefaultHasher, Hasher};
4
5/// To assert _Equivalence_ of two instances of an xAPI Data Type we rely on
6/// this Trait to help us compute a _fingerprint_ for each instance. The
7/// computation of this _fingerprint_ uses a _recursive descent_ mechanism
8/// not unlike what the standard [Hash] does.
9pub trait Fingerprint {
10    /// Feed this value into the given [Hasher].
11    fn fingerprint<H: Hasher>(&self, state: &mut H);
12
13    /// Feeds a slice of this type into the given [Hasher].
14    fn fingerprint_slice<H: Hasher>(data: &[Self], state: &mut H)
15    where
16        Self: Sized,
17    {
18        for piece in data {
19            piece.fingerprint(state)
20        }
21    }
22}
23
24/// Compute and return a _fingerprint_ value uniquely identifying the
25/// implementing Data Type's immutable properties.
26pub(crate) fn fingerprint_it<T: Fingerprint>(x: &T) -> u64 {
27    let mut state = DefaultHasher::new();
28    x.fingerprint(&mut state);
29    state.finish()
30}