Skip to content

ACP: CStr::is_empty() #106

Closed
Closed
@jmillikin

Description

@jmillikin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions