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