-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert struct FromBytesWithNulError
into enum
#134143
base: master
Are you sure you want to change the base?
Conversation
@tgross35 I thought this proposal has already passed - see rust-lang/libs-team#493 |
The meeting was a preliminary thumbs up, but the actual FCP process still needs to happen for anything that changes stable API. |
@tgross35 thx, any links on anything I need to do? Or is this rust-team internal? |
Nothing internal and nothing for you to do, a lot of rust-lang just slows down around this time of year. Somebody from the libs-api team will need to propose FCP, Amanieu can do that and is already assigned. |
This is insta-stable since this is turned into a @rfcbot merge |
Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
This comment has been minimized.
This comment has been minimized.
One of `CStr` constructors, `CStr::from_bytes_with_nul(bytes: &[u8])` handles 3 cases: 1. `bytes` has one NULL as the last value - creates CStr 2. `bytes` has no NULL - error 3. `bytes` has a NULL in some other position - error The 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 the `memchr` dependency. In [this code](https://github.com/gquintard/varnish-rs/blob/f86d7a87683b08d2e634d63e77d9dc1d24ed4a13/varnish-sys/src/vcl/ws.rs#L158), my FFI code needs to copy user's `&[u8]` into a C-allocated memory blob in a NUL-terminated `CStr` 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-implement `from_bytes_with_nul` and add `memchr`dependency just to handle the 2nd case. This PR renames the former `kind` enum from `FromBytesWithNulErrorKind` to `FromBytesWithNulError`, and removes the original struct.
🔔 This is now entering its final comment period, as per the review above. 🔔 |
I believe that this can potentially break existing code, so this needs at the very least a crater run. See rust-lang/rfcs#3753 |
@bors try |
Convert `struct FromBytesWithNulError` into enum This PR renames the former `kind` enum from `FromBytesWithNulErrorKind` to `FromBytesWithNulError`, and removes the original struct. See rust-lang/libs-team#493 ## Possible Changes - TBD * [x] should the new `enum FromBytesWithNulError` derive `Copy`? * [ ] should there be any new/changed attributes? * [x] add some more tests ## Problem One of `CStr` constructors, `CStr::from_bytes_with_nul(bytes: &[u8])` handles 3 cases: 1. `bytes` has one NULL as the last value - creates CStr 2. `bytes` has no NULL - error 3. `bytes` has a NULL in some other position - error The 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 the `memchr` dependency. ## Motivating examples or use cases In [this code](https://github.com/gquintard/varnish-rs/blob/f86d7a87683b08d2e634d63e77d9dc1d24ed4a13/varnish-sys/src/vcl/ws.rs#L158), my FFI code needs to copy user's `&[u8]` into a C-allocated memory blob in a NUL-terminated `CStr` 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-implement `from_bytes_with_nul` and add `memchr`dependency just to handle the 2nd case. r? `@Amanieu`
☀️ Try build successful - checks-actions |
I was about to start a crater run but checked for use in the wild first, from a quick search I don't see any cases of the potentially breaking pattern mentioned in the ACP https://github.com/search?q=lang%3Arust+%2FFromBytesWithNulError+%3F%5C%7B+%3F%5C.%5C.%2F&type=code. So the beta crater runs are probably sufficient to cover anything here, unless there are other patterns that also break. |
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