1 | use crate::{ImgRef, ImgRefMut, ImgVec}; |
2 | use core::hash::{Hash, Hasher}; |
3 | |
4 | impl<'a, T: Hash> Hash for ImgRef<'a, T> { |
5 | #[allow (deprecated)] |
6 | #[inline ] |
7 | fn hash<H: Hasher>(&self, state: &mut H) { |
8 | self.width.hash(state); |
9 | self.height.hash(state); |
10 | for row: &[T] in self.rows() { |
11 | Hash::hash_slice(data:row, state); |
12 | } |
13 | } |
14 | } |
15 | |
16 | impl<'a, T: Hash> Hash for ImgRefMut<'a, T> { |
17 | #[allow (deprecated)] |
18 | #[inline ] |
19 | fn hash<H: Hasher>(&self, state: &mut H) { |
20 | self.as_ref().hash(state); |
21 | } |
22 | } |
23 | |
24 | impl<T: Hash> Hash for ImgVec<T> { |
25 | #[inline (always)] |
26 | fn hash<H: Hasher>(&self, state: &mut H) { |
27 | self.as_ref().hash(state); |
28 | } |
29 | } |
30 | |
31 | impl<'a, 'b, T, U> PartialEq<ImgRef<'b, U>> for ImgRef<'a, T> where T: PartialEq<U> { |
32 | #[allow (deprecated)] |
33 | #[inline ] |
34 | fn eq(&self, other: &ImgRef<'b, U>) -> bool { |
35 | self.width == other.width && |
36 | self.height == other.height && |
37 | self.rows().zip(other.rows()).all(|(a: &[T],b: &[U])| a == b) |
38 | } |
39 | } |
40 | |
41 | impl<'a, 'b, T, U> PartialEq<ImgRefMut<'b, U>> for ImgRefMut<'a, T> where T: PartialEq<U> { |
42 | #[allow (deprecated)] |
43 | #[inline ] |
44 | fn eq(&self, other: &ImgRefMut<'b, U>) -> bool { |
45 | self.as_ref().eq(&other.as_ref()) |
46 | } |
47 | } |
48 | |
49 | |
50 | impl<T, U> PartialEq<ImgVec<U>> for ImgVec<T> where T: PartialEq<U> { |
51 | #[allow (deprecated)] |
52 | #[inline (always)] |
53 | fn eq(&self, other: &ImgVec<U>) -> bool { |
54 | self.as_ref().eq(&other.as_ref()) |
55 | } |
56 | } |
57 | |
58 | impl<'a, T, U> PartialEq<ImgRef<'a, U>> for ImgVec<T> where T: PartialEq<U> { |
59 | #[allow (deprecated)] |
60 | #[inline (always)] |
61 | fn eq(&self, other: &ImgRef<'a, U>) -> bool { |
62 | self.as_ref().eq(other) |
63 | } |
64 | } |
65 | |
66 | impl<'a, T, U> PartialEq<ImgVec<U>> for ImgRef<'a, T> where T: PartialEq<U> { |
67 | #[allow (deprecated)] |
68 | #[inline (always)] |
69 | fn eq(&self, other: &ImgVec<U>) -> bool { |
70 | self.eq(&other.as_ref()) |
71 | } |
72 | } |
73 | |
74 | impl<'a, 'b, T, U> PartialEq<ImgRef<'b, U>> for ImgRefMut<'a, T> where T: PartialEq<U> { |
75 | #[allow (deprecated)] |
76 | #[inline (always)] |
77 | fn eq(&self, other: &ImgRef<'b, U>) -> bool { |
78 | self.as_ref().eq(other) |
79 | } |
80 | } |
81 | |
82 | impl<'a, 'b, T, U> PartialEq<ImgRefMut<'b, U>> for ImgRef<'a, T> where T: PartialEq<U> { |
83 | #[allow (deprecated)] |
84 | #[inline (always)] |
85 | fn eq(&self, other: &ImgRefMut<'b, U>) -> bool { |
86 | self.eq(&other.as_ref()) |
87 | } |
88 | } |
89 | |
90 | impl<'a, T: Eq> Eq for ImgRefMut<'a, T> { |
91 | } |
92 | |
93 | impl<'a, T: Eq> Eq for ImgRef<'a, T> { |
94 | } |
95 | |
96 | impl<T: Eq> Eq for ImgVec<T> { |
97 | } |
98 | |
99 | #[test ] |
100 | fn test_eq_hash() { |
101 | use alloc::vec; |
102 | |
103 | #[derive (Debug)] |
104 | struct Comparable(u16); |
105 | impl PartialEq<u8> for Comparable { |
106 | fn eq(&self, other: &u8) -> bool { self.0 == *other as u16 } |
107 | } |
108 | |
109 | let newtype = ImgVec::new(vec![Comparable(0), Comparable(1), Comparable(2), Comparable(3)], 2, 2); |
110 | let mut img1 = ImgVec::new(vec![0u8, 1, 2, 3], 2, 2); |
111 | let img_ne = ImgVec::new(vec![0u8, 1, 2, 3], 4, 1); |
112 | let img2 = ImgVec::new_stride(vec![0u8, 1, 255, 2, 3, 255], 2, 2, 3); |
113 | let mut img3 = ImgVec::new_stride(vec![0u8, 1, 255, 2, 3], 2, 2, 3); |
114 | |
115 | assert_eq!(newtype, img1); |
116 | equiv(&img1, &img2); |
117 | equiv(&img2, &img3); |
118 | equiv(&img1, &img3); |
119 | |
120 | assert_ne!(img1, img_ne); |
121 | assert_eq!(img1.as_ref(), img2); |
122 | assert_eq!(img2, img3.as_ref()); |
123 | equiv(&img1.as_ref(), &img3.as_ref()); |
124 | equiv(&img1.as_mut(), &img3.as_mut()); |
125 | assert_eq!(img2.as_ref(), img3.as_mut()); |
126 | |
127 | let mut map = HashSet::new(); |
128 | img3[(0usize,0usize)] = 100; |
129 | assert_ne!(img1, img3); |
130 | assert!(map.insert(img1)); |
131 | assert!(map.insert(img3)); |
132 | assert!(map.insert(img_ne)); |
133 | assert!(!map.insert(img2)); |
134 | } |
135 | |
136 | #[cfg (test)] |
137 | use std::fmt::Debug; |
138 | #[cfg (test)] |
139 | use std::collections::HashSet; |
140 | |
141 | #[cfg (test)] |
142 | fn equiv<A>(a: &A, b: &A) where A: Eq + PartialEq + Hash + Debug { |
143 | assert_eq!(a, b); |
144 | let mut map = HashSet::new(); |
145 | assert!(map.insert(a)); |
146 | assert!(!map.insert(b)); |
147 | assert!(!map.insert(a)); |
148 | assert!(map.remove(b)); |
149 | assert!(map.is_empty()); |
150 | } |
151 | |