Skip to content

Commit 4acd142

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

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

library/alloc/src/ffi/c_str.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,32 @@ 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+
10981124
#[stable(feature = "cstring_asref", since = "1.7.0")]
10991125
impl ops::Index<ops::RangeFull> for CString {
11001126
type Output = CStr;
@@ -1178,6 +1204,19 @@ impl CStr {
11781204
}
11791205
}
11801206

1207+
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
1208+
impl PartialEq<CString> for CStr {
1209+
#[inline]
1210+
fn eq(&self, other: &CString) -> bool {
1211+
*self == **other
1212+
}
1213+
1214+
#[inline]
1215+
fn ne(&self, other: &CString) -> bool {
1216+
*self != **other
1217+
}
1218+
}
1219+
11811220
#[stable(feature = "rust1", since = "1.0.0")]
11821221
impl core::error::Error for NulError {
11831222
#[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)