Skip to content

Commit f8e4835

Browse files
authored
feat: extend Finterprint to support raw bytes and optimize for Hash (#1338)
1 parent 9781ea6 commit f8e4835

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

rust/utils/src/fingerprint.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl serde::ser::Error for FingerprinterError {
3030
}
3131
}
3232

33-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
33+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3434
pub struct Fingerprint(pub [u8; 16]);
3535

3636
impl Fingerprint {
@@ -63,6 +63,14 @@ impl AsRef<[u8]> for Fingerprint {
6363
}
6464
}
6565

66+
impl std::hash::Hash for Fingerprint {
67+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
68+
// Fingerprint is already evenly distributed, so we can just use the first few bytes.
69+
const N: usize = size_of::<usize>();
70+
state.write(&self.0[..N]);
71+
}
72+
}
73+
6674
impl Serialize for Fingerprint {
6775
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
6876
where
@@ -101,6 +109,10 @@ impl Fingerprinter {
101109
value.serialize(self)
102110
}
103111

112+
pub fn write_raw_bytes(&mut self, bytes: &[u8]) {
113+
self.hasher.update(bytes);
114+
}
115+
104116
fn write_type_tag(&mut self, tag: &str) {
105117
self.hasher.update(tag.as_bytes());
106118
self.hasher.update(b";");

0 commit comments

Comments
 (0)