Skip to main content

gix_ref/
compare.rs

1use crate::{
2    FullName, FullNameRef, Namespace, PartialName, PartialNameRef, Reference,
3    bstr::{BStr, BString, ByteSlice},
4    file, packed,
5};
6
7macro_rules! impl_partial_eq {
8    ($left_type:ty, $right_type:ty, $left:ident => $left_bytes:expr, $right:ident => $right_bytes:expr) => {
9        impl PartialEq<$right_type> for $left_type {
10            fn eq(&self, other: &$right_type) -> bool {
11                let $left = self;
12                let $right = other;
13                $left_bytes == $right_bytes
14            }
15        }
16    };
17}
18
19macro_rules! impl_partial_eq_pair {
20    ($left_type:ty, $right_type:ty, $left:ident => $left_bytes:expr, $right:ident => $right_bytes:expr) => {
21        impl_partial_eq!($left_type, $right_type, $left => $left_bytes, $right => $right_bytes);
22        impl_partial_eq!($right_type, $left_type, $right => $right_bytes, $left => $left_bytes);
23    };
24}
25
26macro_rules! impl_partial_eq_bytes {
27    ($type:ty, $value:ident => $bytes:expr) => {
28        impl_partial_eq_pair!($type, str, $value => $bytes.as_bytes(), other => other.as_bytes());
29        impl_partial_eq_pair!($type, &str, $value => $bytes.as_bytes(), other => other.as_bytes());
30        impl_partial_eq_pair!($type, String, $value => $bytes.as_bytes(), other => other.as_bytes());
31        impl_partial_eq_pair!($type, BStr, $value => $bytes.as_bytes(), other => other.as_bytes());
32        impl_partial_eq_pair!($type, &BStr, $value => $bytes.as_bytes(), other => other.as_bytes());
33        impl_partial_eq_pair!($type, BString, $value => $bytes.as_bytes(), other => other.as_bytes());
34    };
35}
36
37macro_rules! impl_partial_eq_reference {
38    ($type:ty, $value:ident => $name:expr) => {
39        impl_partial_eq!($type, str, $value => $name.as_bytes(), other => other.as_bytes());
40        impl_partial_eq!($type, &str, $value => $name.as_bytes(), other => other.as_bytes());
41        impl_partial_eq!($type, String, $value => $name.as_bytes(), other => other.as_bytes());
42        impl_partial_eq!($type, BStr, $value => $name.as_bytes(), other => other.as_bytes());
43        impl_partial_eq!($type, &BStr, $value => $name.as_bytes(), other => other.as_bytes());
44        impl_partial_eq!($type, BString, $value => $name.as_bytes(), other => other.as_bytes());
45        impl_partial_eq!(
46            $type,
47            FullName,
48            $value => $name.as_bytes(),
49            other => other.as_bstr().as_bytes()
50        );
51        impl_partial_eq!(
52            $type,
53            FullNameRef,
54            $value => $name.as_bytes(),
55            other => other.as_bstr().as_bytes()
56        );
57        impl_partial_eq!(
58            $type,
59            &FullNameRef,
60            $value => $name.as_bytes(),
61            other => other.as_bstr().as_bytes()
62        );
63    };
64}
65
66impl_partial_eq_bytes!(FullName, value => value.as_bstr());
67impl_partial_eq_bytes!(FullNameRef, value => value.as_bstr());
68impl_partial_eq_bytes!(PartialName, value => value.as_ref().as_bstr());
69impl_partial_eq_bytes!(PartialNameRef, value => value.as_bstr());
70impl_partial_eq_bytes!(Namespace, value => value.as_bstr());
71
72impl_partial_eq_pair!(
73    &FullNameRef,
74    String,
75    name => name.as_bstr().as_bytes(),
76    text => text.as_bytes()
77);
78impl_partial_eq_pair!(
79    &FullNameRef,
80    BString,
81    name => name.as_bstr().as_bytes(),
82    text => text.as_bytes()
83);
84impl_partial_eq_pair!(
85    &PartialNameRef,
86    String,
87    name => name.as_bstr().as_bytes(),
88    text => text.as_bytes()
89);
90impl_partial_eq_pair!(
91    &PartialNameRef,
92    BString,
93    name => name.as_bstr().as_bytes(),
94    text => text.as_bytes()
95);
96
97impl_partial_eq_pair!(
98    FullName,
99    FullNameRef,
100    owned => owned.as_bstr().as_bytes(),
101    borrowed => borrowed.as_bstr().as_bytes()
102);
103impl_partial_eq_pair!(
104    FullName,
105    &FullNameRef,
106    owned => owned.as_bstr().as_bytes(),
107    borrowed => borrowed.as_bstr().as_bytes()
108);
109impl_partial_eq_pair!(
110    PartialName,
111    PartialNameRef,
112    owned => owned.as_ref().as_bstr().as_bytes(),
113    borrowed => borrowed.as_bstr().as_bytes()
114);
115impl_partial_eq_pair!(
116    PartialName,
117    &PartialNameRef,
118    owned => owned.as_ref().as_bstr().as_bytes(),
119    borrowed => borrowed.as_bstr().as_bytes()
120);
121
122// Keep these comparisons one-way: same-type reference equality is structural, so reverse
123// implementations would let references with different targets form a non-transitive chain through their name.
124impl_partial_eq_reference!(Reference, value => value.name.as_bstr());
125impl_partial_eq_reference!(file::loose::Reference, value => value.name.as_bstr());
126impl_partial_eq_reference!(packed::Reference<'_>, value => value.name.as_bstr());