Skip to content

Commit 817b093

Browse files
committed
Implement 'PartialEq<{&Self, CString, Cow<Self>}>' for 'CStr'; Implement 'PartialEq<{CStr, &CStr, Cow<CStr>}>' for 'CString'; Implement 'PartialEq<{CStr, &CStr, CString}>' for 'Cow<CStr>';
1 parent 5986ff0 commit 817b093

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

library/alloc/src/ffi/c_str.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,46 @@ impl From<&CStr> for CString {
10951095
}
10961096
}
10971097

1098+
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
1099+
impl PartialEq<CStr> for CString {
1100+
#[inline]
1101+
fn eq(&self, other: &CStr) -> bool {
1102+
**self == *other
1103+
}
1104+
1105+
#[inline]
1106+
fn ne(&self, other: &CStr) -> bool {
1107+
**self != *other
1108+
}
1109+
}
1110+
1111+
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
1112+
impl PartialEq<&CStr> for CString {
1113+
#[inline]
1114+
fn eq(&self, other: &&CStr) -> bool {
1115+
**self == **other
1116+
}
1117+
1118+
#[inline]
1119+
fn ne(&self, other: &&CStr) -> bool {
1120+
**self != **other
1121+
}
1122+
}
1123+
1124+
#[cfg(not(no_global_oom_handling))]
1125+
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
1126+
impl PartialEq<Cow<'_, CStr>> for CString {
1127+
#[inline]
1128+
fn eq(&self, other: &Cow<'_, CStr>) -> bool {
1129+
**self == **other
1130+
}
1131+
1132+
#[inline]
1133+
fn ne(&self, other: &Cow<'_, CStr>) -> bool {
1134+
**self != **other
1135+
}
1136+
}
1137+
10981138
#[stable(feature = "cstring_asref", since = "1.7.0")]
10991139
impl ops::Index<ops::RangeFull> for CString {
11001140
type Output = CStr;
@@ -1178,6 +1218,75 @@ impl CStr {
11781218
}
11791219
}
11801220

1221+
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
1222+
impl PartialEq<CString> for CStr {
1223+
#[inline]
1224+
fn eq(&self, other: &CString) -> bool {
1225+
*self == **other
1226+
}
1227+
1228+
#[inline]
1229+
fn ne(&self, other: &CString) -> bool {
1230+
*self != **other
1231+
}
1232+
}
1233+
1234+
#[cfg(not(no_global_oom_handling))]
1235+
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
1236+
impl PartialEq<Cow<'_, Self>> for CStr {
1237+
#[inline]
1238+
fn eq(&self, other: &Cow<'_, Self>) -> bool {
1239+
*self == **other
1240+
}
1241+
1242+
#[inline]
1243+
fn ne(&self, other: &Cow<'_, Self>) -> bool {
1244+
*self != **other
1245+
}
1246+
}
1247+
1248+
#[cfg(not(no_global_oom_handling))]
1249+
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
1250+
impl PartialEq<CStr> for Cow<'_, CStr> {
1251+
#[inline]
1252+
fn eq(&self, other: &CStr) -> bool {
1253+
**self == *other
1254+
}
1255+
1256+
#[inline]
1257+
fn ne(&self, other: &CStr) -> bool {
1258+
**self != *other
1259+
}
1260+
}
1261+
1262+
#[cfg(not(no_global_oom_handling))]
1263+
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
1264+
impl PartialEq<&CStr> for Cow<'_, CStr> {
1265+
#[inline]
1266+
fn eq(&self, other: &&CStr) -> bool {
1267+
**self == **other
1268+
}
1269+
1270+
#[inline]
1271+
fn ne(&self, other: &&CStr) -> bool {
1272+
**self != **other
1273+
}
1274+
}
1275+
1276+
#[cfg(not(no_global_oom_handling))]
1277+
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
1278+
impl PartialEq<CString> for Cow<'_, CStr> {
1279+
#[inline]
1280+
fn eq(&self, other: &CString) -> bool {
1281+
**self == **other
1282+
}
1283+
1284+
#[inline]
1285+
fn ne(&self, other: &CString) -> bool {
1286+
**self != **other
1287+
}
1288+
}
1289+
11811290
#[stable(feature = "rust1", since = "1.0.0")]
11821291
impl core::error::Error for NulError {
11831292
#[allow(deprecated)]

library/core/src/ffi/c_str.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,19 @@ impl CStr {
660660
}
661661
}
662662

663+
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
664+
impl PartialEq<&Self> for CStr {
665+
#[inline]
666+
fn eq(&self, other: &&Self) -> bool {
667+
*self == **other
668+
}
669+
670+
#[inline]
671+
fn ne(&self, other: &&Self) -> bool {
672+
*self != **other
673+
}
674+
}
675+
663676
// `.to_bytes()` representations are compared instead of the inner `[c_char]`s,
664677
// because `c_char` is `i8` (not `u8`) on some platforms.
665678
// That is why this is implemented manually and not derived.
@@ -670,6 +683,7 @@ impl PartialOrd for CStr {
670683
self.to_bytes().partial_cmp(&other.to_bytes())
671684
}
672685
}
686+
673687
#[stable(feature = "rust1", since = "1.0.0")]
674688
impl Ord for CStr {
675689
#[inline]

0 commit comments

Comments
 (0)