-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement Borrow<CStr> for CString and ToOwned for CStr #26928
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
Conversation
r? @pcwalton (rust_highfive has picked a reviewer for you, use r? to override) |
type Owned = CString; | ||
|
||
fn to_owned(&self) -> CString { | ||
unsafe { CString::from_vec_unchecked(self.to_bytes_with_nul().to_vec()) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you want to_bytes
here instead of to_bytes_with_nul
because the from_vec_unchecked
function pushes a nul byte on the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, updated.
Double-check due to |
@bluss adding a test to check this. |
This allows CString and CStr to be used with the Cow type, which is extremely useful when interfacing with C libraries that make extensive use of C-style strings.
Confirmed that they have equivalent hashes with a new test. |
The test only checks bytes in the trivial range (0-127) where i8 and u8 overlap. I looked into i8's hash and I guess they should hash the same, but for the test to be real it needs to test byte values outside the common range. |
Updated the test to include some higher values, still passes. |
great, it's a good test to have in the codebase |
@bors r+ |
📌 Commit 69579e4 has been approved by |
This allows CString and CStr to be used with the Cow type, which is extremely useful when interfacing with C libraries that make extensive use of C-style strings.
This allows CString and CStr to be used with the Cow type,
which is extremely useful when interfacing with C libraries
that make extensive use of C-style strings.