@@ -288,14 +288,9 @@ where
288
288
/// ## By-value transmutation
289
289
///
290
290
/// If `Src: Sized` and `Dst: Sized`, then it must be sound to transmute an
291
- /// `SV`-valid `Src` into a `DV`-valid `Dst` by value via union transmute. In
292
- /// particular:
293
- /// - If `size_of::<Src>() >= size_of::<Dst>()`, then the first
294
- /// `size_of::<Dst>()` bytes of any `SV`-valid `Src` must be a `DV`-valid
295
- /// `Dst`.
296
- /// - If `size_of::<Src>() < size_of::<Dst>()`, then any `SV`-valid `Src`
297
- /// followed by `size_of::<Dst>() - size_of::<Src>()` uninitialized bytes must
298
- /// be a `DV`-valid `Dst`.
291
+ /// `SV`-valid `Src` into a `DV`-valid `Dst` by value via size-preserving or
292
+ /// size-shrinking transmute. In particular, the first `size_of::<Dst>()` bytes
293
+ /// of any `SV`-valid `Src` must be a `DV`-valid `Dst`.
299
294
///
300
295
/// If either `Src: !Sized` or `Dst: !Sized`, then this condition does not need
301
296
/// to hold.
@@ -364,12 +359,18 @@ unsafe impl<T: ?Sized> SizeCompat<T> for T {
364
359
}
365
360
}
366
361
367
- // TODO: Update all `TransmuteFrom` safety proofs.
368
-
369
362
/// `Valid<Src: IntoBytes> → Initialized<Dst>`
370
- // SAFETY: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
371
- // initialized bit patterns, which is exactly the set allowed in the referent of
372
- // any `Initialized` `Ptr`.
363
+ // SAFETY:
364
+ // - By-value: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
365
+ // initialized bit patterns, which is exactly the set allowed in the referent
366
+ // of any `Initialized` `Ptr`. This holds for both size-preserving and
367
+ // size-shrinking transmutes.
368
+ // - By-reference:
369
+ // - Shrinking: See above.
370
+ // - Tearing: Let `src` be a `Valid` `Src` and `dst` be an `Initialized`
371
+ // `Dst`. The trailing bytes of `dst` have bit validity `[u8; N]`. `src` has
372
+ // bit validity `[u8; M]`. Thus, `dst' = src + trailing_bytes_of(dst)` has
373
+ // bit validity `[u8; N + M]`, which is a valid `Initialized` value.
373
374
unsafe impl < Src , Dst > TransmuteFrom < Src , Valid , Initialized > for Dst
374
375
where
375
376
Src : IntoBytes + ?Sized ,
@@ -381,6 +382,8 @@ where
381
382
// SAFETY: Since `Dst: FromBytes`, any initialized bit pattern may appear in the
382
383
// referent of a `Ptr<Dst, (_, _, Valid)>`. This is exactly equal to the set of
383
384
// bit patterns which may appear in the referent of any `Initialized` `Ptr`.
385
+ //
386
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
384
387
unsafe impl < Src , Dst > TransmuteFrom < Src , Initialized , Valid > for Dst
385
388
where
386
389
Src : ?Sized ,
@@ -395,6 +398,8 @@ where
395
398
/// `Initialized<Src> → Initialized<Dst>`
396
399
// SAFETY: The set of allowed bit patterns in the referent of any `Initialized`
397
400
// `Ptr` is the same regardless of referent type.
401
+ //
402
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
398
403
unsafe impl < Src , Dst > TransmuteFrom < Src , Initialized , Initialized > for Dst
399
404
where
400
405
Src : ?Sized ,
@@ -409,6 +414,8 @@ where
409
414
/// `V<Src> → Uninit<Dst>`
410
415
// SAFETY: A `Dst` with validity `Uninit` permits any byte sequence, and
411
416
// therefore can be transmuted from any value.
417
+ //
418
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
412
419
unsafe impl < Src , Dst , V > TransmuteFrom < Src , V , Uninit > for Dst
413
420
where
414
421
Src : ?Sized ,
@@ -512,6 +519,8 @@ impl_transitive_transmute_from!(T: ?Sized => UnsafeCell<T> => T => Cell<T>);
512
519
// explicitly guaranteed, but it's obvious from `MaybeUninit`'s documentation
513
520
// that this is the intention:
514
521
// https://doc.rust-lang.org/1.85.0/core/mem/union.MaybeUninit.html
522
+ //
523
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
515
524
unsafe impl < Src , Dst > TransmuteFrom < Src , Uninit , Valid > for MaybeUninit < Dst > { }
516
525
517
526
// SAFETY: `MaybeUninit<T>` has the same size as `T` [1]. Thus, a pointer cast
0 commit comments