Commit f4870fd
authored
Rollup merge of #160563 - Ddystopia:improve-buffer-cursor, r=clarfonthey
Make `BorrowedCursor<'a, T>` covariant in `'a` and drop an indirection
This is a solution to #117693 (comment), with some improvements.
Currently `'a` in `BorrowedCursor` is invariant, though people seem to talk about it as if it were covariant, and the feature is in FCP right now. The previous version with `'buf` and `'data` lifetimes had the same flaw: `'data` was invariant.
A later PR landed that merged them and said that `BorrowedCursor` manually ensures that `'data` won't be ever overwritten thus invariance should not be needed. But unfortunately the lifetime is still left invariant. You can see it here, and the error spells it out exactly:
```rust
#![feature(core_io_borrowed_buf)]
use std::io::{BorrowedBuf, BorrowedCursor};
// Accepted.
fn buf_covariant<'short, 'long: 'short>(buf: BorrowedBuf<'long, u8>) -> BorrowedBuf<'short, u8> {
buf
}
// Rejected.
fn cursor_covariant<'short, 'long: 'short>(
cursor: BorrowedCursor<'long, u8>,
) -> BorrowedCursor<'short, u8> {
cursor
}
// Rejected.
fn cursor_contravariant<'short, 'long: 'short>(
cursor: BorrowedCursor<'short, u8>,
) -> BorrowedCursor<'long, u8> {
cursor
}
fn main() {}
```
And the errors (also say that `BorrowedCursor` is invariant over `'a`):
```
error: lifetime may not live long enough
--> src/main.rs:14:5
|
11 | fn cursor_covariant<'short, 'long: 'short>(
| ------ ----- lifetime `'long` defined here
| |
| lifetime `'short` defined here
...
14 | cursor
| ^^^^^^ function was supposed to return data with lifetime `'long` but it is returning data with lifetime `'short`
|
= help: consider adding the following bound: `'short: 'long`
= note: requirement occurs because of the type `BorrowedCursor<'_, u8>`, which makes the generic argument `'_` invariant
= note: the struct `BorrowedCursor<'a, T>` is invariant over the parameter `'a`
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
error: lifetime may not live long enough
--> src/main.rs:21:5
|
18 | fn cursor_contravariant<'short, 'long: 'short>(
| ------ ----- lifetime `'long` defined here
| |
| lifetime `'short` defined here
...
21 | cursor
| ^^^^^^ function was supposed to return data with lifetime `'long` but it is returning data with lifetime `'short`
|
= help: consider adding the following bound: `'short: 'long`
= note: requirement occurs because of the type `BorrowedCursor<'_, u8>`, which makes the generic argument `'_` invariant
= note: the struct `BorrowedCursor<'a, T>` is invariant over the parameter `'a`
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
error: could not compile `play` (bin "play") due to 2 previous errors
```
This also removes the two `mem::transmute` calls that `unfilled` and `reborrow` used to shorten `&'this mut BorrowedBuf<'data, T>` into `&'this mut BorrowedBuf<'this, T>`.
---
Additionally I noticed that `BorrowedCursor` is not really as efficient as it could be, for a standard library: it contained a reference to the `BorrowedBuf`, which in turn contains a slice to the data. Without this, the fix is just replacing `&'a mut BorrowedBuf<'a, T>` with `NonNull<BorrowedBuf<'a, T>>`, plus some convenience helpers.
To fix this, I also stored a reborrowed pointer to the first element of the array, with the provenance to access the whole array. `filled` and `init` are still read from the pointer to `BorrowedBuf`, the buffer length is also read from it but carefully, in order to not create a retag which will trigger a foreign access to the pointer stored in `BorrowedCursor`, making it disabled. It increased the size of `BorrowedCursor` from one `usize` to two of them.
It is stored as the pointer rather than `&mut [MaybeUninit<T>]` to save a `usize` from the `BorrowedCursor` size.1 file changed
Lines changed: 136 additions & 48 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
| |||
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
37 | 51 | | |
38 | 52 | | |
39 | 53 | | |
40 | 54 | | |
41 | | - | |
| 55 | + | |
42 | 56 | | |
43 | 57 | | |
44 | 58 | | |
| |||
70 | 84 | | |
71 | 85 | | |
72 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
73 | 90 | | |
74 | | - | |
75 | | - | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
76 | 94 | | |
77 | | - | |
| 95 | + | |
78 | 96 | | |
79 | 97 | | |
80 | 98 | | |
| |||
144 | 162 | | |
145 | 163 | | |
146 | 164 | | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
| 165 | + | |
| 166 | + | |
156 | 167 | | |
157 | 168 | | |
158 | 169 | | |
| |||
193 | 204 | | |
194 | 205 | | |
195 | 206 | | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
201 | 223 | | |
202 | 224 | | |
203 | 225 | | |
204 | 226 | | |
205 | | - | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
206 | 299 | | |
207 | 300 | | |
208 | 301 | | |
| |||
213 | 306 | | |
214 | 307 | | |
215 | 308 | | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
| 309 | + | |
225 | 310 | | |
226 | 311 | | |
227 | 312 | | |
228 | 313 | | |
229 | 314 | | |
230 | | - | |
| 315 | + | |
231 | 316 | | |
232 | 317 | | |
233 | 318 | | |
234 | 319 | | |
235 | 320 | | |
236 | 321 | | |
237 | 322 | | |
238 | | - | |
| 323 | + | |
239 | 324 | | |
240 | 325 | | |
241 | 326 | | |
242 | 327 | | |
243 | 328 | | |
244 | 329 | | |
245 | | - | |
| 330 | + | |
246 | 331 | | |
247 | 332 | | |
248 | 333 | | |
| |||
253 | 338 | | |
254 | 339 | | |
255 | 340 | | |
256 | | - | |
| 341 | + | |
| 342 | + | |
257 | 343 | | |
258 | 344 | | |
259 | 345 | | |
| |||
263 | 349 | | |
264 | 350 | | |
265 | 351 | | |
266 | | - | |
267 | | - | |
| 352 | + | |
268 | 353 | | |
269 | 354 | | |
270 | 355 | | |
| |||
283 | 368 | | |
284 | 369 | | |
285 | 370 | | |
286 | | - | |
| 371 | + | |
287 | 372 | | |
288 | 373 | | |
289 | | - | |
| 374 | + | |
| 375 | + | |
290 | 376 | | |
291 | 377 | | |
292 | 378 | | |
| |||
301 | 387 | | |
302 | 388 | | |
303 | 389 | | |
304 | | - | |
| 390 | + | |
| 391 | + | |
305 | 392 | | |
306 | 393 | | |
307 | 394 | | |
| |||
319 | 406 | | |
320 | 407 | | |
321 | 408 | | |
322 | | - | |
| 409 | + | |
| 410 | + | |
323 | 411 | | |
324 | 412 | | |
325 | 413 | | |
| |||
349 | 437 | | |
350 | 438 | | |
351 | 439 | | |
352 | | - | |
353 | | - | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
354 | 444 | | |
355 | 445 | | |
356 | 446 | | |
| |||
362 | 452 | | |
363 | 453 | | |
364 | 454 | | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
371 | 459 | | |
372 | 460 | | |
373 | 461 | | |
374 | | - | |
| 462 | + | |
375 | 463 | | |
376 | 464 | | |
0 commit comments