Convert struct FromBytesWithNulError
into enum
#134143
Open
+14
−29
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR renames the former
kind
enum fromFromBytesWithNulErrorKind
toFromBytesWithNulError
, and removes the original struct.See rust-lang/libs-team#493
Possible Changes - TBD
enum FromBytesWithNulError
deriveCopy
?Problem
One of
CStr
constructors,CStr::from_bytes_with_nul(bytes: &[u8])
handles 3 cases:bytes
has one NULL as the last value - creates CStrbytes
has no NULL - errorbytes
has a NULL in some other position - errorThe 3rd case is error that may require lossy conversion, but the 2nd case can easily be handled by the user code. Unfortunately, this function returns an opaque
FromBytesWithNulError
error in both 2nd and 3rd case, so the user cannot detect just the 2nd case - having to re-implement the entire function and bring in thememchr
dependency.Motivating examples or use cases
In this code, my FFI code needs to copy user's
&[u8]
into a C-allocated memory blob in a NUL-terminatedCStr
format. My code must first validate if&[u8]
has a trailing NUL (case 1), no NUL (adds one on the fly - case 2), or NUL in the middle (3rd case - error). I had to re-implementfrom_bytes_with_nul
and addmemchr
dependency just to handle the 2nd case.r? @Amanieu