1use crate::utils::AsBytes;
2use std::hash::{Hash, Hasher};
34/// A structure that implements `Eq` and is hashable even if the wrapped data implements only
5/// `PartialEq`.
6#[derive(PartialEq, Clone, Debug)]
7pub struct HashablePartialEq<T> {
8 value: T,
9}
1011impl<T> HashablePartialEq<T> {
12/// Creates a new `HashablePartialEq`. This is unsafe because you must be sure that you really
13 /// want to transform the wrapped object's partial equality to an equivalence relation.
14pub unsafe fn new(value: T) -> HashablePartialEq<T> {
15 HashablePartialEq { value: value }
16 }
1718/// Gets the wrapped value.
19pub fn unwrap(self) -> T {
20self.value
21 }
22}
2324impl<T: PartialEq> Eq for HashablePartialEq<T> {}
2526impl<T: AsBytes> Hash for HashablePartialEq<T> {
27#[inline]
28fn hash<H: Hasher>(&self, state: &mut H) {
29 state.write(self.value.as_bytes())
30 }
31}