Commit bf4ecec
committed
Convert
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.struct FromBytesWithNulError into enum1 parent 4ed8cf4 commit bf4ecec
1 file changed
+19
-31
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
127 | | - | |
| 127 | + | |
128 | 128 | | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | 139 | | |
150 | 140 | | |
151 | 141 | | |
152 | 142 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
158 | 146 | | |
159 | 147 | | |
160 | 148 | | |
| |||
199 | 187 | | |
200 | 188 | | |
201 | 189 | | |
202 | | - | |
| 190 | + | |
203 | 191 | | |
204 | 192 | | |
205 | 193 | | |
| |||
349 | 337 | | |
350 | 338 | | |
351 | 339 | | |
352 | | - | |
| 340 | + | |
353 | 341 | | |
354 | 342 | | |
355 | 343 | | |
356 | 344 | | |
357 | 345 | | |
358 | | - | |
| 346 | + | |
359 | 347 | | |
360 | 348 | | |
361 | | - | |
| 349 | + | |
362 | 350 | | |
363 | 351 | | |
364 | 352 | | |
365 | 353 | | |
366 | 354 | | |
367 | | - | |
| 355 | + | |
368 | 356 | | |
369 | 357 | | |
370 | | - | |
| 358 | + | |
371 | 359 | | |
372 | 360 | | |
373 | 361 | | |
| |||
379 | 367 | | |
380 | 368 | | |
381 | 369 | | |
382 | | - | |
383 | | - | |
| 370 | + | |
| 371 | + | |
384 | 372 | | |
385 | 373 | | |
386 | 374 | | |
| |||
0 commit comments