Description
Proposal
Problem statement
The str
and slice
primitives both have is_empty()
to check if a value is empty, but CStr
currently doesn't. This is unfortunate because the CStr::to_bytes()
function is documented as performing a length calculation in the future, which would make cstr_val.to_bytes().is_empty()
into an O(N) operation.
Motivation, use-cases
There's many reasons a user might want to check if a C string is empty, for example when writing FFI bindings to code written in C that crashes if given an empty string.
Solution sketches
pub const fn is_empty(&self) -> bool {
(unsafe { self.inner.get_unchecked(0) }) == &0
}
Links and related work
https://rust-for-linux.github.io/docs/kernel/str/struct.CStr.html#method.is_empty
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.