aligners/bytes/
cmp.rs

1use crate::alignment::Alignment;
2use crate::bytes::AlignedBytes;
3use crate::slice::AlignedSlice;
4
5impl<A: Alignment> PartialEq for AlignedBytes<A> {
6    #[inline]
7    fn eq(&self, other: &Self) -> bool {
8        let slice: &AlignedSlice<A> = self;
9        let other_slice: &AlignedSlice<A> = other;
10
11        slice.eq(other_slice)
12    }
13}
14
15impl<A: Alignment> Eq for AlignedBytes<A> {}
16
17impl<A: Alignment> PartialEq<AlignedBytes<A>> for Vec<u8> {
18    #[inline]
19    fn eq(&self, other: &AlignedBytes<A>) -> bool {
20        other.eq(self)
21    }
22}
23
24impl<A: Alignment> PartialEq<Vec<u8>> for AlignedBytes<A> {
25    #[inline]
26    fn eq(&self, other: &Vec<u8>) -> bool {
27        let slice: &AlignedSlice<A> = self;
28        let other_slice: &[u8] = other;
29
30        slice.eq(other_slice)
31    }
32}
33
34impl<A: Alignment> PartialEq<AlignedBytes<A>> for [u8] {
35    #[inline]
36    fn eq(&self, other: &AlignedBytes<A>) -> bool {
37        other.eq(self)
38    }
39}
40
41impl<A: Alignment> PartialEq<[u8]> for AlignedBytes<A> {
42    #[inline]
43    fn eq(&self, other: &[u8]) -> bool {
44        let slice: &AlignedSlice<A> = self;
45
46        slice.eq(other)
47    }
48}
49
50impl<A: Alignment, const N: usize> PartialEq<AlignedBytes<A>> for [u8; N] {
51    #[inline]
52    fn eq(&self, other: &AlignedBytes<A>) -> bool {
53        other.eq(self)
54    }
55}
56
57impl<A: Alignment, const N: usize> PartialEq<[u8; N]> for AlignedBytes<A> {
58    #[inline]
59    fn eq(&self, other: &[u8; N]) -> bool {
60        let slice: &AlignedSlice<A> = self;
61
62        slice.eq(other)
63    }
64}
65impl<A: Alignment> PartialOrd for AlignedBytes<A> {
66    #[inline]
67    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
68        let slice: &AlignedSlice<A> = self;
69        let other_slice: &AlignedSlice<A> = other;
70
71        slice.partial_cmp(other_slice)
72    }
73}
74
75impl<A: Alignment> Ord for AlignedBytes<A> {
76    #[inline]
77    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
78        let slice: &AlignedSlice<A> = self;
79        let other_slice: &AlignedSlice<A> = other;
80
81        slice.cmp(other_slice)
82    }
83}
84
85impl<A: Alignment> std::hash::Hash for AlignedBytes<A> {
86    #[inline]
87    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
88        let slice: &[u8] = self;
89        std::hash::Hash::hash(slice, state)
90    }
91}
92
93#[cfg(test)]
94mod test {
95    use super::*;
96    use crate::alignment::*;
97
98    #[test]
99    fn hash_of_two_equal_structures_is_equal() {
100        use std::{
101            collections::hash_map::DefaultHasher,
102            hash::{Hash, Hasher},
103        };
104        let bytes1: AlignedBytes<One> = AlignedBytes::new_zeroed(4);
105        let bytes2: AlignedBytes<One> = AlignedBytes::new_zeroed(4);
106
107        let mut s1 = DefaultHasher::new();
108        bytes1.hash(&mut s1);
109        let h1 = s1.finish();
110
111        let mut s2 = DefaultHasher::new();
112        bytes2.hash(&mut s2);
113        let h2 = s2.finish();
114
115        assert_eq!(h1, h2);
116    }
117}