Skip to content

CStr unchecked constructor can be a const fn #54678

Closed
@abonander

Description

@abonander

The current implementation of CStr::from_bytes_with_nul_unchecked() is not valid in a const context, but it can easily be tweaked so that it is:

pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &Self {
    union BytesToCStr<'a> {
        bytes: &'a [u8],
        cstr: &'a CStr,
    }
    
    BytesToCStr { bytes }.cstr
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions