From e29e516bd24842984ddf988541da7ae0979b408c Mon Sep 17 00:00:00 2001 From: Evan Mesterhazy Date: Fri, 16 Feb 2024 09:28:30 -0500 Subject: [PATCH] Implement ContentHash for u32 and u64 This is for completeness and to avoid accidents such as someone calling `ContentHash::hash(1234u32.to_le_bytes())` and expecting it to hash properly as a u32 instead of a 4 byte slice, which produces a different hash due to hashing the length of the slice before its contents. --- lib/src/content_hash.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/src/content_hash.rs b/lib/src/content_hash.rs index 9f9dfc3734..e50a0498a5 100644 --- a/lib/src/content_hash.rs +++ b/lib/src/content_hash.rs @@ -39,12 +39,24 @@ impl ContentHash for u8 { } } +impl ContentHash for u32 { + fn hash(&self, state: &mut impl digest::Update) { + state.update(&self.to_le_bytes()); + } +} + impl ContentHash for i32 { fn hash(&self, state: &mut impl digest::Update) { state.update(&self.to_le_bytes()); } } +impl ContentHash for u64 { + fn hash(&self, state: &mut impl digest::Update) { + state.update(&self.to_le_bytes()); + } +} + impl ContentHash for i64 { fn hash(&self, state: &mut impl digest::Update) { state.update(&self.to_le_bytes());