Skip to content

CStr from fixed c_char array #146836

@pronebird

Description

@pronebird

Hi,

Often times I see a fixed array of C chars returned via FFI boundary which cannot be easily converted into CStr because:

  1. CStr::from_ptr() uses strlen() which can walk outside of boundaries of a fixed array
  2. CStr::from_bytes_until_nul(&[u8]) takes u8 slice.

Each time I stumble upon [c_char; N] it's always a struggle.

As such I think CStr should have an initializer from &[c_char] slice. This would work identical to CStr::from_ptr() except it wouldn't need to use strlen. It could flip the slice into &[u8] and then pass it into from_bytes_until_nul.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)C-enhancementCategory: An issue proposing an enhancement or a PR with one.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions