Implement a function that creates a Cow<'_, CStr> from a slice of bytes #71279
Labels
C-feature-request
Category: A feature request, i.e: not implemented / a PR.
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
I've been working on FFI-bindings for a C library and I ran into an issue where
CString::new()
would fail if there's a 0-byte.I was left with three options:
NulError
, which makes it less ergonomic, IMO;CString::new()
fails, callCStr::from_bytes_with_nul()
usingNulError::nul_position()
, wrap bothOk
andErr
branches inCow::from()
.I found the third option to be the most ergonomic and that's what I went with. Of course you could argue that it's sort of a leaky abstraction because users are expected to understand that C-libraries usually read until the 0-byte. But an interior 0-byte in a Rust string is not really a common case, is it?
So, what do you guys think, is this something worthy of being a part of
std::ffi
?I already made the changes locally and I'd be happy to make a PR. However, I can't come up with a good name, so far I called it
CString::from_slice()
but I don't think it's fitting.The text was updated successfully, but these errors were encountered: