@@ -279,14 +279,9 @@ where
279
279
/// ## By-value transmutation
280
280
///
281
281
/// If `Src: Sized` and `Dst: Sized`, then it must be sound to transmute an
282
- /// `SV`-valid `Src` into a `DV`-valid `Dst` by value via union transmute. In
283
- /// particular:
284
- /// - If `size_of::<Src>() >= size_of::<Dst>()`, then the first
285
- /// `size_of::<Dst>()` bytes of any `SV`-valid `Src` must be a `DV`-valid
286
- /// `Dst`.
287
- /// - If `size_of::<Src>() < size_of::<Dst>()`, then any `SV`-valid `Src`
288
- /// followed by `size_of::<Dst>() - size_of::<Src>()` uninitialized bytes must
289
- /// be a `DV`-valid `Dst`.
282
+ /// `SV`-valid `Src` into a `DV`-valid `Dst` by value via size-preserving or
283
+ /// size-shrinking transmute. In particular, the first `size_of::<Dst>()` bytes
284
+ /// of any `SV`-valid `Src` must be a `DV`-valid `Dst`.
290
285
///
291
286
/// If either `Src: !Sized` or `Dst: !Sized`, then this condition does not need
292
287
/// to hold.
@@ -355,12 +350,18 @@ unsafe impl<T: ?Sized> SizeCompat<T> for T {
355
350
}
356
351
}
357
352
358
- // TODO: Update all `TransmuteFrom` safety proofs.
359
-
360
353
/// `Valid<Src: IntoBytes> → Initialized<Dst>`
361
- // SAFETY: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
362
- // initialized bit patterns, which is exactly the set allowed in the referent of
363
- // any `Initialized` `Ptr`.
354
+ // SAFETY:
355
+ // - By-value: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
356
+ // initialized bit patterns, which is exactly the set allowed in the referent
357
+ // of any `Initialized` `Ptr`. This holds for both size-preserving and
358
+ // size-shrinking transmutes.
359
+ // - By-reference:
360
+ // - Shrinking: See above.
361
+ // - Tearing: Let `src` be a `Valid` `Src` and `dst` be an `Initialized`
362
+ // `Dst`. The trailing bytes of `dst` have bit validity `[u8; N]`. `src` has
363
+ // bit validity `[u8; M]`. Thus, `dst' = src + trailing_bytes_of(dst)` has
364
+ // bit validity `[u8; N + M]`, which is a valid `Initialized` value.
364
365
unsafe impl < Src , Dst > TransmuteFrom < Src , Valid , Initialized > for Dst
365
366
where
366
367
Src : IntoBytes + ?Sized ,
@@ -372,6 +373,8 @@ where
372
373
// SAFETY: Since `Dst: FromBytes`, any initialized bit pattern may appear in the
373
374
// referent of a `Ptr<Dst, (_, _, Valid)>`. This is exactly equal to the set of
374
375
// bit patterns which may appear in the referent of any `Initialized` `Ptr`.
376
+ //
377
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
375
378
unsafe impl < Src , Dst > TransmuteFrom < Src , Initialized , Valid > for Dst
376
379
where
377
380
Src : ?Sized ,
@@ -386,6 +389,8 @@ where
386
389
/// `Initialized<Src> → Initialized<Dst>`
387
390
// SAFETY: The set of allowed bit patterns in the referent of any `Initialized`
388
391
// `Ptr` is the same regardless of referent type.
392
+ //
393
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
389
394
unsafe impl < Src , Dst > TransmuteFrom < Src , Initialized , Initialized > for Dst
390
395
where
391
396
Src : ?Sized ,
@@ -400,6 +405,8 @@ where
400
405
/// `V<Src> → Uninit<Dst>`
401
406
// SAFETY: A `Dst` with validity `Uninit` permits any byte sequence, and
402
407
// therefore can be transmuted from any value.
408
+ //
409
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
403
410
unsafe impl < Src , Dst , V > TransmuteFrom < Src , V , Uninit > for Dst
404
411
where
405
412
Src : ?Sized ,
@@ -503,6 +510,8 @@ impl_transitive_transmute_from!(T: ?Sized => UnsafeCell<T> => T => Cell<T>);
503
510
// explicitly guaranteed, but it's obvious from `MaybeUninit`'s documentation
504
511
// that this is the intention:
505
512
// https://doc.rust-lang.org/1.85.0/core/mem/union.MaybeUninit.html
513
+ //
514
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
506
515
unsafe impl < Src , Dst > TransmuteFrom < Src , Uninit , Valid > for MaybeUninit < Dst > { }
507
516
508
517
// SAFETY: `MaybeUninit<T>` has the same size as `T` [1]. Thus, a pointer cast
0 commit comments