@@ -296,14 +296,9 @@ where
296
296
/// ## By-value transmutation
297
297
///
298
298
/// If `Src: Sized` and `Dst: Sized`, then it must be sound to transmute an
299
- /// `SV`-valid `Src` into a `DV`-valid `Dst` by value via union transmute. In
300
- /// particular:
301
- /// - If `size_of::<Src>() >= size_of::<Dst>()`, then the first
302
- /// `size_of::<Dst>()` bytes of any `SV`-valid `Src` must be a `DV`-valid
303
- /// `Dst`.
304
- /// - If `size_of::<Src>() < size_of::<Dst>()`, then any `SV`-valid `Src`
305
- /// followed by `size_of::<Dst>() - size_of::<Src>()` uninitialized bytes must
306
- /// be a `DV`-valid `Dst`.
299
+ /// `SV`-valid `Src` into a `DV`-valid `Dst` by value via size-preserving or
300
+ /// size-shrinking transmute. In particular, the first `size_of::<Dst>()` bytes
301
+ /// of any `SV`-valid `Src` must be a `DV`-valid `Dst`.
307
302
///
308
303
/// If either `Src: !Sized` or `Dst: !Sized`, then this condition does not need
309
304
/// to hold.
@@ -372,12 +367,18 @@ unsafe impl<T: ?Sized> SizeCompat<T> for T {
372
367
}
373
368
}
374
369
375
- // TODO: Update all `TransmuteFrom` safety proofs.
376
-
377
370
/// `Valid<Src: IntoBytes> → Initialized<Dst>`
378
- // SAFETY: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
379
- // initialized bit patterns, which is exactly the set allowed in the referent of
380
- // any `Initialized` `Ptr`.
371
+ // SAFETY:
372
+ // - By-value: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
373
+ // initialized bit patterns, which is exactly the set allowed in the referent
374
+ // of any `Initialized` `Ptr`. This holds for both size-preserving and
375
+ // size-shrinking transmutes.
376
+ // - By-reference:
377
+ // - Shrinking: See above.
378
+ // - Tearing: Let `src` be a `Valid` `Src` and `dst` be an `Initialized`
379
+ // `Dst`. The trailing bytes of `dst` have bit validity `[u8; N]`. `src` has
380
+ // bit validity `[u8; M]`. Thus, `dst' = src + trailing_bytes_of(dst)` has
381
+ // bit validity `[u8; N + M]`, which is a valid `Initialized` value.
381
382
unsafe impl < Src , Dst > TransmuteFrom < Src , Valid , Initialized > for Dst
382
383
where
383
384
Src : IntoBytes + ?Sized ,
@@ -389,6 +390,8 @@ where
389
390
// SAFETY: Since `Dst: FromBytes`, any initialized bit pattern may appear in the
390
391
// referent of a `Ptr<Dst, (_, _, Valid)>`. This is exactly equal to the set of
391
392
// bit patterns which may appear in the referent of any `Initialized` `Ptr`.
393
+ //
394
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
392
395
unsafe impl < Src , Dst > TransmuteFrom < Src , Initialized , Valid > for Dst
393
396
where
394
397
Src : ?Sized ,
@@ -403,6 +406,8 @@ where
403
406
/// `Initialized<Src> → Initialized<Dst>`
404
407
// SAFETY: The set of allowed bit patterns in the referent of any `Initialized`
405
408
// `Ptr` is the same regardless of referent type.
409
+ //
410
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
406
411
unsafe impl < Src , Dst > TransmuteFrom < Src , Initialized , Initialized > for Dst
407
412
where
408
413
Src : ?Sized ,
@@ -417,6 +422,8 @@ where
417
422
/// `V<Src> → Uninit<Dst>`
418
423
// SAFETY: A `Dst` with validity `Uninit` permits any byte sequence, and
419
424
// therefore can be transmuted from any value.
425
+ //
426
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
420
427
unsafe impl < Src , Dst , V > TransmuteFrom < Src , V , Uninit > for Dst
421
428
where
422
429
Src : ?Sized ,
@@ -520,6 +527,8 @@ impl_transitive_transmute_from!(T: ?Sized => UnsafeCell<T> => T => Cell<T>);
520
527
// explicitly guaranteed, but it's obvious from `MaybeUninit`'s documentation
521
528
// that this is the intention:
522
529
// https://doc.rust-lang.org/1.85.0/core/mem/union.MaybeUninit.html
530
+ //
531
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
523
532
unsafe impl < Src , Dst > TransmuteFrom < Src , Uninit , Valid > for MaybeUninit < Dst > { }
524
533
525
534
// SAFETY: `MaybeUninit<T>` has the same size as `T` [1]. Thus, a pointer cast
0 commit comments