-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: 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.Relevant to the library API team, which will review and decide on the PR/issue.
Description
When dealing with FFI (fixed char buffers in file formats that represent strings and passing fixed char buffers to C code to write chars to them up to a given max len) I often find myself doing this:
let end = s.iter().position(|&b| b == 0).map_or(0, |i| i + 1);
CStr::from_bytes_with_nul(&s[..end])
Because if I pass the whole slice to it, it returns a FromBytesWithNulError
.
There should be a function that combines both lines and read until the first '\0'
byte in the slice.
Something like:
CStr::from_bytes_until_nul(slice)
And yes, I did write my own function for my use case but I find myself copying these two lines around into different projects dealing with FFI and I think it would make sense to have this (arguably) "basic functionality" in std's CStr
:)
JHennebergJHenneberg
Metadata
Metadata
Assignees
Labels
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: 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.Relevant to the library API team, which will review and decide on the PR/issue.