Thanks to visit codestin.com
Credit goes to docs.rs

uapi/ustr/
eq.rs

1use crate::{Bstr, Ustr, Ustring};
2use std::{
3    ffi::{CStr, CString, OsStr, OsString},
4    os::unix::ffi::OsStrExt,
5    path::{Path, PathBuf},
6};
7
8macro_rules! foreign_eq {
9    ($ty:path) => {
10        impl PartialEq<[u8]> for $ty {
11            fn eq(&self, other: &[u8]) -> bool {
12                Bstr::as_bytes(self) == other
13            }
14        }
15
16        impl PartialEq<$ty> for [u8] {
17            fn eq(&self, other: &$ty) -> bool {
18                Bstr::as_bytes(other) == self
19            }
20        }
21
22        impl PartialEq<str> for $ty {
23            fn eq(&self, other: &str) -> bool {
24                Bstr::as_bytes(self) == str::as_bytes(other)
25            }
26        }
27
28        impl PartialEq<$ty> for str {
29            fn eq(&self, other: &$ty) -> bool {
30                Bstr::as_bytes(other) == str::as_bytes(self)
31            }
32        }
33
34        impl PartialEq<CStr> for $ty {
35            fn eq(&self, other: &CStr) -> bool {
36                Bstr::as_bytes(self) == CStr::to_bytes(other)
37            }
38        }
39
40        impl PartialEq<$ty> for CStr {
41            fn eq(&self, other: &$ty) -> bool {
42                Bstr::as_bytes(other) == CStr::to_bytes(self)
43            }
44        }
45
46        impl PartialEq<OsStr> for $ty {
47            fn eq(&self, other: &OsStr) -> bool {
48                Bstr::as_bytes(self) == OsStr::as_bytes(other)
49            }
50        }
51
52        impl PartialEq<$ty> for OsStr {
53            fn eq(&self, other: &$ty) -> bool {
54                Bstr::as_bytes(other) == OsStr::as_bytes(self)
55            }
56        }
57
58        impl PartialEq<Path> for $ty {
59            fn eq(&self, other: &Path) -> bool {
60                Bstr::as_bytes(self) == OsStr::as_bytes(Path::as_os_str(other))
61            }
62        }
63
64        impl PartialEq<$ty> for Path {
65            fn eq(&self, other: &$ty) -> bool {
66                Bstr::as_bytes(other) == OsStr::as_bytes(Path::as_os_str(self))
67            }
68        }
69
70        impl PartialEq<Vec<u8>> for $ty {
71            fn eq(&self, other: &Vec<u8>) -> bool {
72                Bstr::as_bytes(self) == Vec::as_slice(other)
73            }
74        }
75
76        impl PartialEq<$ty> for Vec<u8> {
77            fn eq(&self, other: &$ty) -> bool {
78                Bstr::as_bytes(other) == Vec::as_slice(self)
79            }
80        }
81
82        impl PartialEq<String> for $ty {
83            fn eq(&self, other: &String) -> bool {
84                Bstr::as_bytes(self) == str::as_bytes(other)
85            }
86        }
87
88        impl PartialEq<$ty> for String {
89            fn eq(&self, other: &$ty) -> bool {
90                Bstr::as_bytes(other) == str::as_bytes(self)
91            }
92        }
93
94        impl PartialEq<CString> for $ty {
95            fn eq(&self, other: &CString) -> bool {
96                Bstr::as_bytes(self) == CStr::to_bytes(other)
97            }
98        }
99
100        impl PartialEq<$ty> for CString {
101            fn eq(&self, other: &$ty) -> bool {
102                Bstr::as_bytes(other) == CStr::to_bytes(self)
103            }
104        }
105
106        impl PartialEq<OsString> for $ty {
107            fn eq(&self, other: &OsString) -> bool {
108                Bstr::as_bytes(self) == OsStr::as_bytes(other)
109            }
110        }
111
112        impl PartialEq<$ty> for OsString {
113            fn eq(&self, other: &$ty) -> bool {
114                Bstr::as_bytes(other) == OsStr::as_bytes(self)
115            }
116        }
117
118        impl PartialEq<PathBuf> for $ty {
119            fn eq(&self, other: &PathBuf) -> bool {
120                Bstr::as_bytes(self) == OsStr::as_bytes(Path::as_os_str(other))
121            }
122        }
123
124        impl PartialEq<$ty> for PathBuf {
125            fn eq(&self, other: &$ty) -> bool {
126                Bstr::as_bytes(other) == OsStr::as_bytes(Path::as_os_str(self))
127            }
128        }
129    };
130}
131
132foreign_eq!(Bstr);
133foreign_eq!(Ustr);
134foreign_eq!(Ustring);
135
136macro_rules! mutual_eq {
137    ($a:ident, $b:ident) => {
138        impl PartialEq<$a> for $b {
139            fn eq(&self, other: &$a) -> bool {
140                Bstr::as_bytes(self) == Bstr::as_bytes(other)
141            }
142        }
143
144        impl PartialEq<$b> for $a {
145            fn eq(&self, other: &$b) -> bool {
146                Bstr::as_bytes(self) == Bstr::as_bytes(other)
147            }
148        }
149    };
150}
151
152mutual_eq!(Bstr, Ustr);
153mutual_eq!(Bstr, Ustring);
154mutual_eq!(Ustr, Ustring);