Commit bc20681
Teach transmute_{ref,mut}! to handle slice DSTs (#2428)
We generalize our existing support in `util::macro_util`, now relying on
PMEs to detect when transmutes cannot be supported. In order to continue
to support sized reference transmutation in a `const` context, we use
the autoref specialization trick to fall back to our old (`const`-safe)
implementation when both the source and destination type are `Sized`.
The main challenge in generalizing this support is making a trait
implementation available conditional on a PME. We do this using the
`unsafe_with_size_eq!` helper macro, which introduces
`#[repr(transparent)]` wrapper types, `Src` and `Dst`. Internal to this
macro, we implement `SizeEq<Src<T>> for Dst<U>`, with the implementation
of `SizeEq::cast_from_raw` containing the PME logic for size equality.
The macro's caller must promise to only use the `Src` and `Dst` types
to wrap the `T` and `U` types for which this PME logic has been applied
(or else the `SizeEq` impl could "leak" to types for which it is
unsound).
In addition, we generalize our prior support for transmuting between
unsized types. In particular, we previously used the `SizeEq` trait to
denote that two types have equal sizes in the face of a cast operation
(in particular, that `*const T as *const U` preserves referent size). In
this commit, we add support for metadata fix-up, which means that we
support casts for which `*const T as *const U` does *not* preserve
referent size. Instead, we compute an affine function at compile time
and apply it at runtime - computing the destination type's metadata as a
function of the source metadata, `dst_meta = A + src_meta * B`. `A` and
`B` are computed at compile time.
We generalize `SizeEq` to permit its `cast_from_raw` method to perform a
runtime metadata fix-up operation.
Makes progress on #1817
gherrit-pr-id: Ib4bc62202e0b3b09d155333b525087f7aa8f02c2
Co-authored-by: Jack Wrenn <[email protected]>1 parent f9c9703 commit bc20681
File tree
154 files changed
+1455
-4619
lines changed- src
- pointer
- util
- tests
- ui-msrv
- ui-nightly
- ui-stable
- zerocopy-derive
- src
- tests
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
154 files changed
+1455
-4619
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
| 176 | + | |
193 | 177 | | |
194 | 178 | | |
195 | 179 | | |
196 | 180 | | |
197 | 181 | | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
| 182 | + | |
210 | 183 | | |
211 | 184 | | |
212 | 185 | | |
| |||
423 | 396 | | |
424 | 397 | | |
425 | 398 | | |
426 | | - | |
427 | | - | |
| 399 | + | |
| 400 | + | |
428 | 401 | | |
429 | 402 | | |
430 | 403 | | |
| |||
437 | 410 | | |
438 | 411 | | |
439 | 412 | | |
440 | | - | |
441 | | - | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
442 | 419 | | |
443 | 420 | | |
444 | | - | |
445 | | - | |
| 421 | + | |
446 | 422 | | |
447 | | - | |
448 | | - | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
449 | 427 | | |
450 | 428 | | |
451 | 429 | | |
| |||
457 | 435 | | |
458 | 436 | | |
459 | 437 | | |
460 | | - | |
461 | | - | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
462 | 442 | | |
463 | 443 | | |
464 | 444 | | |
465 | 445 | | |
466 | | - | |
467 | | - | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
468 | 450 | | |
469 | 451 | | |
470 | 452 | | |
| |||
0 commit comments