checkpoint
This commit is contained in:
@@ -37,7 +37,7 @@ pub fn derive_hash2(input: TokenStream) -> TokenStream {
|
||||
Data::Enum(data) => {
|
||||
// Hash the discriminant first to ensure different variants hash differently
|
||||
let discriminant_hash = quote! {
|
||||
core::mem::discriminant(self).hash(state);
|
||||
core::hash::Hash::hash(&core::mem::discriminant(self), state);
|
||||
};
|
||||
|
||||
let variants = data.variants.iter().map(|variant| {
|
||||
|
||||
17
src/lib.rs
17
src/lib.rs
@@ -11,7 +11,7 @@ pub trait Hash2 {
|
||||
#[cfg(feature = "uuid")]
|
||||
impl Hash2 for uuid::Uuid {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
let (a,b) = self.as_u64_pair();
|
||||
let (a, b) = self.as_u64_pair();
|
||||
state.write_u64(a);
|
||||
state.write_u64(b);
|
||||
}
|
||||
@@ -118,6 +118,15 @@ impl Hash2 for String {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Hash2 for std::borrow::Cow<'_, T>
|
||||
where
|
||||
T: Hash2 + ToOwned,
|
||||
{
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
Hash2::hash(&**self, state)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash2 + ?Sized> Hash2 for &T {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
(*self).hash(state);
|
||||
@@ -153,6 +162,12 @@ impl<T: Hash2> Hash2 for Vec<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash2> Hash2 for Box<T> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
T::hash(&**self, state)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash2> Hash2 for Option<T> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
match self {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use std::hash::{Hash, Hasher};
|
||||
use hash2::Hash2;
|
||||
|
||||
#[derive(Hash2)]
|
||||
@@ -185,3 +184,33 @@ fn test_generic_enum() {
|
||||
let _ = hash_value(&e2);
|
||||
let _ = hash_value(&e3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cow_tuple_slice() {
|
||||
use std::borrow::Cow;
|
||||
|
||||
// Test Cow<[T]> where T is a tuple
|
||||
let data = vec![("key1", 1u32), ("key2", 2u32)];
|
||||
let cow_borrowed: Cow<[(&str, u32)]> = Cow::Borrowed(&data);
|
||||
let cow_owned: Cow<[(&str, u32)]> = Cow::Owned(data.clone());
|
||||
|
||||
let hash1 = hash_value(&cow_borrowed);
|
||||
let hash2 = hash_value(&cow_owned);
|
||||
assert_eq!(hash1, hash2, "Borrowed and owned Cow should hash the same");
|
||||
|
||||
// Test with nested Option
|
||||
let with_option: Option<Cow<[(&str, u32)]>> = Some(Cow::Borrowed(&data));
|
||||
let _ = hash_value(&with_option);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cow_str() {
|
||||
use std::borrow::Cow;
|
||||
|
||||
let borrowed: Cow<str> = Cow::Borrowed("hello");
|
||||
let owned: Cow<str> = Cow::Owned(String::from("hello"));
|
||||
|
||||
let hash1 = hash_value(&borrowed);
|
||||
let hash2 = hash_value(&owned);
|
||||
assert_eq!(hash1, hash2, "Borrowed and owned str Cow should hash the same");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user