Skip to content

Allow comparisons between CStr, CString, and Cow<CStr>. #137268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions library/alloc/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,46 @@ impl From<&CStr> for CString {
}
}

#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
impl PartialEq<CStr> for CString {
#[inline]
fn eq(&self, other: &CStr) -> bool {
**self == *other
}

#[inline]
fn ne(&self, other: &CStr) -> bool {
**self != *other
}
}

#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
impl PartialEq<&CStr> for CString {
#[inline]
fn eq(&self, other: &&CStr) -> bool {
**self == **other
}

#[inline]
fn ne(&self, other: &&CStr) -> bool {
**self != **other
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
impl PartialEq<Cow<'_, CStr>> for CString {
#[inline]
fn eq(&self, other: &Cow<'_, CStr>) -> bool {
**self == **other
}

#[inline]
fn ne(&self, other: &Cow<'_, CStr>) -> bool {
**self != **other
}
}

#[stable(feature = "cstring_asref", since = "1.7.0")]
impl ops::Index<ops::RangeFull> for CString {
type Output = CStr;
Expand Down Expand Up @@ -1178,6 +1218,75 @@ impl CStr {
}
}

#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
impl PartialEq<CString> for CStr {
#[inline]
fn eq(&self, other: &CString) -> bool {
*self == **other
}

#[inline]
fn ne(&self, other: &CString) -> bool {
*self != **other
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
impl PartialEq<Cow<'_, Self>> for CStr {
#[inline]
fn eq(&self, other: &Cow<'_, Self>) -> bool {
*self == **other
}

#[inline]
fn ne(&self, other: &Cow<'_, Self>) -> bool {
*self != **other
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
impl PartialEq<CStr> for Cow<'_, CStr> {
#[inline]
fn eq(&self, other: &CStr) -> bool {
**self == *other
}

#[inline]
fn ne(&self, other: &CStr) -> bool {
**self != *other
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
impl PartialEq<&CStr> for Cow<'_, CStr> {
#[inline]
fn eq(&self, other: &&CStr) -> bool {
**self == **other
}

#[inline]
fn ne(&self, other: &&CStr) -> bool {
**self != **other
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
impl PartialEq<CString> for Cow<'_, CStr> {
#[inline]
fn eq(&self, other: &CString) -> bool {
**self == **other
}

#[inline]
fn ne(&self, other: &CString) -> bool {
**self != **other
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl core::error::Error for NulError {
#[allow(deprecated)]
Expand Down
14 changes: 14 additions & 0 deletions library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,19 @@ impl CStr {
}
}

#[stable(feature = "c_string_eq_c_str", since = "CURRENT_RUSTC_VERSION")]
impl PartialEq<&Self> for CStr {
#[inline]
fn eq(&self, other: &&Self) -> bool {
*self == **other
}

#[inline]
fn ne(&self, other: &&Self) -> bool {
*self != **other
}
}

// `.to_bytes()` representations are compared instead of the inner `[c_char]`s,
// because `c_char` is `i8` (not `u8`) on some platforms.
// That is why this is implemented manually and not derived.
Expand All @@ -670,6 +683,7 @@ impl PartialOrd for CStr {
self.to_bytes().partial_cmp(&other.to_bytes())
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for CStr {
#[inline]
Expand Down