Skip to content

Commit 6780c96

Browse files
weihangloUrgau
authored andcommitted
feat: derive Clone for StableHasher
Due to the orphan rule, deriving `Clone` must happen inside this crate
1 parent cc85b7a commit 6780c96

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/stable_hasher.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub trait ExtendedHasher: Hasher {
8888
/// let hash: Hash128 = hasher.finish();
8989
/// ```
9090
#[must_use]
91+
#[derive(Clone)]
9192
pub struct StableHasher<H: ExtendedHasher> {
9293
state: H,
9394
}

src/stable_hasher/tests.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,24 @@ fn test_isize_compression() {
112112
check_hash(0xFF, 0xFFFFFFFFFFFFFFFF);
113113
check_hash(u64::MAX /* -1 */, 1);
114114
}
115+
116+
#[test]
117+
fn test_cloned_hasher_output() {
118+
// Test that integers are handled consistently across platforms.
119+
let test_u8 = 0xAB_u8;
120+
let test_u16 = 0xFFEE_u16;
121+
let test_u32 = 0x445577AA_u32;
122+
123+
let mut h1 = StableSipHasher128::new();
124+
test_u8.hash(&mut h1);
125+
test_u16.hash(&mut h1);
126+
127+
let h2 = h1.clone();
128+
let mut h3 = h1.clone();
129+
// Make sure the cloned hasher can be fed more values.
130+
test_u32.hash(&mut h3);
131+
132+
let h1_hash: TestHash = h1.finish();
133+
assert_eq!(h1_hash, h2.finish());
134+
assert_ne!(h1_hash, h3.finish());
135+
}

0 commit comments

Comments
 (0)