/rust/registry/src/index.crates.io-1949cf8c6b5b557f/imgref-1.12.0/src/traits.rs
Line | Count | Source |
1 | | use crate::{ImgRef, ImgRefMut, ImgVec}; |
2 | | use core::hash::{Hash, Hasher}; |
3 | | |
4 | | impl<T: Hash> Hash for ImgRef<'_, T> { |
5 | | #[allow(deprecated)] |
6 | | #[inline] |
7 | 0 | fn hash<H: Hasher>(&self, state: &mut H) { |
8 | 0 | self.width.hash(state); |
9 | 0 | self.height.hash(state); |
10 | 0 | for row in self.rows() { |
11 | 0 | Hash::hash_slice(row, state); |
12 | 0 | } |
13 | 0 | } |
14 | | } |
15 | | |
16 | | impl<T: Hash> Hash for ImgRefMut<'_, T> { |
17 | | #[allow(deprecated)] |
18 | | #[inline] |
19 | 0 | fn hash<H: Hasher>(&self, state: &mut H) { |
20 | 0 | self.as_ref().hash(state); |
21 | 0 | } |
22 | | } |
23 | | |
24 | | impl<T: Hash> Hash for ImgVec<T> { |
25 | | #[inline(always)] |
26 | 0 | fn hash<H: Hasher>(&self, state: &mut H) { |
27 | 0 | self.as_ref().hash(state); |
28 | 0 | } |
29 | | } |
30 | | |
31 | | impl<'b, T, U> PartialEq<ImgRef<'b, U>> for ImgRef<'_, T> where T: PartialEq<U> { |
32 | | #[allow(deprecated)] |
33 | | #[inline] |
34 | 0 | fn eq(&self, other: &ImgRef<'b, U>) -> bool { |
35 | 0 | self.width == other.width && |
36 | 0 | self.height == other.height && |
37 | 0 | self.rows().zip(other.rows()).all(|(a,b)| a == b) |
38 | 0 | } |
39 | | } |
40 | | |
41 | | impl<'b, T, U> PartialEq<ImgRefMut<'b, U>> for ImgRefMut<'_, T> where T: PartialEq<U> { |
42 | | #[allow(deprecated)] |
43 | | #[inline] |
44 | 0 | fn eq(&self, other: &ImgRefMut<'b, U>) -> bool { |
45 | 0 | self.as_ref().eq(&other.as_ref()) |
46 | 0 | } |
47 | | } |
48 | | |
49 | | |
50 | | impl<T, U> PartialEq<ImgVec<U>> for ImgVec<T> where T: PartialEq<U> { |
51 | | #[allow(deprecated)] |
52 | | #[inline(always)] |
53 | 0 | fn eq(&self, other: &ImgVec<U>) -> bool { |
54 | 0 | self.as_ref().eq(&other.as_ref()) |
55 | 0 | } |
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 | 0 | fn eq(&self, other: &ImgRef<'a, U>) -> bool { |
62 | 0 | self.as_ref().eq(other) |
63 | 0 | } |
64 | | } |
65 | | |
66 | | impl<T, U> PartialEq<ImgVec<U>> for ImgRef<'_, T> where T: PartialEq<U> { |
67 | | #[allow(deprecated)] |
68 | | #[inline(always)] |
69 | 0 | fn eq(&self, other: &ImgVec<U>) -> bool { |
70 | 0 | self.eq(&other.as_ref()) |
71 | 0 | } |
72 | | } |
73 | | |
74 | | impl<'b, T, U> PartialEq<ImgRef<'b, U>> for ImgRefMut<'_, T> where T: PartialEq<U> { |
75 | | #[allow(deprecated)] |
76 | | #[inline(always)] |
77 | 0 | fn eq(&self, other: &ImgRef<'b, U>) -> bool { |
78 | 0 | self.as_ref().eq(other) |
79 | 0 | } |
80 | | } |
81 | | |
82 | | impl<'b, T, U> PartialEq<ImgRefMut<'b, U>> for ImgRef<'_, T> where T: PartialEq<U> { |
83 | | #[allow(deprecated)] |
84 | | #[inline(always)] |
85 | 0 | fn eq(&self, other: &ImgRefMut<'b, U>) -> bool { |
86 | 0 | self.eq(&other.as_ref()) |
87 | 0 | } |
88 | | } |
89 | | |
90 | | impl<T: Eq> Eq for ImgRefMut<'_, T> { |
91 | | } |
92 | | |
93 | | impl<T: Eq> Eq for ImgRef<'_, 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 == u16::from(*other) } |
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::collections::HashSet; |
138 | | #[cfg(test)] |
139 | | use std::fmt::Debug; |
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 | | } |