Commit 6f6f8d4
committed
Convert
## Description
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.
## Solution
This PR renames the former `kind` enum from `FromBytesWithNulErrorKind` to `FromBytesWithNulError`, and removes the original struct.struct FromBytesWithNulError into enum1 parent 33c245b commit 6f6f8d4
1 file changed
+14
-29
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
| 129 | + | |
| 130 | + | |
135 | 131 | | |
| 132 | + | |
136 | 133 | | |
137 | 134 | | |
138 | 135 | | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | 136 | | |
150 | 137 | | |
151 | 138 | | |
152 | 139 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
158 | 143 | | |
159 | 144 | | |
160 | 145 | | |
| |||
199 | 184 | | |
200 | 185 | | |
201 | 186 | | |
202 | | - | |
| 187 | + | |
203 | 188 | | |
204 | 189 | | |
205 | 190 | | |
| |||
349 | 334 | | |
350 | 335 | | |
351 | 336 | | |
352 | | - | |
| 337 | + | |
353 | 338 | | |
354 | 339 | | |
355 | 340 | | |
356 | 341 | | |
357 | 342 | | |
358 | | - | |
| 343 | + | |
359 | 344 | | |
360 | 345 | | |
361 | | - | |
| 346 | + | |
362 | 347 | | |
363 | 348 | | |
364 | 349 | | |
365 | 350 | | |
366 | 351 | | |
367 | | - | |
| 352 | + | |
368 | 353 | | |
369 | 354 | | |
370 | | - | |
| 355 | + | |
371 | 356 | | |
372 | 357 | | |
373 | 358 | | |
| |||
379 | 364 | | |
380 | 365 | | |
381 | 366 | | |
382 | | - | |
383 | | - | |
| 367 | + | |
| 368 | + | |
384 | 369 | | |
385 | 370 | | |
386 | 371 | | |
| |||
0 commit comments