ps_ecc/cow/implementations/
hash.rs1use std::hash::{Hash, Hasher};
2
3use crate::Cow;
4
5impl Hash for Cow<'_> {
6 fn hash<H: Hasher>(&self, state: &mut H) {
7 (**self).hash(state);
8 }
9}
10
11#[cfg(test)]
12mod tests {
13 use std::hash::{DefaultHasher, Hash, Hasher};
14
15 use ps_buffer::Buffer;
16
17 use crate::Cow;
18
19 type TestError = Box<dyn std::error::Error>;
20
21 fn hash(value: &Cow) -> u64 {
22 let mut hasher = DefaultHasher::new();
23
24 value.hash(&mut hasher);
25
26 hasher.finish()
27 }
28
29 #[test]
30 fn test_hash_agrees_across_variants() -> Result<(), TestError> {
31 let borrowed = Cow::Borrowed(b"abc");
32 let owned = Cow::Owned(Buffer::from_slice(b"abc")?.share());
33
34 assert_eq!(hash(&borrowed), hash(&owned));
35
36 Ok(())
37 }
38}