parry3d_f64/utils/hashable_partial_eq.rs
1use crate::utils::AsBytes;
2use core::hash::{Hash, Hasher};
3
4/// 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}
10
11impl<T> HashablePartialEq<T> {
12 /// Creates a new `HashablePartialEq`. Please make sure that you really
13 /// want to transform the wrapped object's partial equality to an equivalence relation.
14 pub fn new(value: T) -> HashablePartialEq<T> {
15 HashablePartialEq { value }
16 }
17
18 /// Gets the wrapped value.
19 pub fn unwrap(self) -> T {
20 self.value
21 }
22}
23
24impl<T: PartialEq> Eq for HashablePartialEq<T> {}
25
26impl<T: AsBytes> Hash for HashablePartialEq<T> {
27 #[inline]
28 fn hash<H: Hasher>(&self, state: &mut H) {
29 state.write(self.value.as_bytes())
30 }
31}