Skip to content
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

ACP: Allow comparisons between CString and CStr. #517

Closed
bjoernager opened this issue Jan 2, 2025 · 1 comment
Closed

ACP: Allow comparisons between CString and CStr. #517

bjoernager opened this issue Jan 2, 2025 · 1 comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@bjoernager
Copy link

bjoernager commented Jan 2, 2025

Proposal

Problem statement

alloc::ffi::CString is very similar to alloc::string::String, and the same goes for core::ffi::CStr when compared to str. But the FFI types do differ a bit with regard to the relationship between the owned and borrowed types.

Currently, str, String, and alloc::borrow::Cow<'a, str> all implement PartialEq in the following variants:

  • PartialEq<str>
  • PartialEq<&'b str>
  • PartialEq<String>
  • PartialEq<Cow<'b, str>>

CString and CStr, on the other hand, only implement the default PartialEq<Self>.

Motivating examples or use cases

Comparing a CString with a CStr currently requires converting either of the objects to the other's type:

let s0: CString = todo!();
let s1: &CStr   = todo!();

assert_eq!(s0,          s1.to_owned());
assert_eq!(s0.as_ref(), s1);
// Etc.

Implementing this proposal would eliminate the need for the ToOwned::to_owned or AsRef::as_ref call, as well as increase the overall parity with String / str.

Solution sketch

Add the following implementations to the standard library:

// core::ffi

impl PartialEq<&Self> for CStr;

impl PartialEq<CString> for CStr;

impl PartialEq<Cow<'_, Self>> for CStr;

// alloc::ffi

impl PartialEq<CStr> for CString;

impl PartialEq<&CStr> for CString;

impl PartialEq<Cow<'_, Self>> for CString;

// alloc::borrow

impl PartialEq<CStr> for Cow<'_, CStr>;

impl<'a, 'b> PartialEq<&'b CStr> for Cow<'a, CStr>;

impl PartialEq<CString> for Cow<'_, CStr>;
@bjoernager bjoernager added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Jan 2, 2025
@joshtriplett joshtriplett added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Jan 7, 2025
@joshtriplett
Copy link
Member

We discussed this in today's @rust-lang/libs-api meeting. We're happy to see these added, but they'll need a crater run to make sure they don't cause too many inference issues in the ecosystem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

2 participants