Skip to content

Add CStr::from_bytes #1264

@nagisa

Description

@nagisa

Add a CStr::from_bytes function with a following API (or something along the lines):

enum CStrFromBytesError {
     InteriorNullBytes,
     NoEndingNullByte,
};
impl CStr {
    fn from_bytes(bytes: &[u8]) -> Result<&CStr, CStrFromBytesError>;
}

Which converts a bytestring with a trailing null byte to a C string slice. If the slice does not have a trailing null byte or has a null byte in any position that’s not the last byte appropriate error is returned.

This is useful in APIs where a &CStr is expected and you can/want to pass in a static string (e.g. b"helloworld\0").

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions