@@ -275,14 +275,9 @@ where
275
275
/// ## By-value transmutation
276
276
///
277
277
/// If `Src: Sized` and `Self: Sized`, then it must be sound to transmute an
278
- /// `SV`-valid `Src` into a `DV`-valid `Dst` by value via union transmute. In
279
- /// particular:
280
- /// - If `size_of::<Src>() > size_of::<Dst>()`, then the first
281
- /// `size_of::<Dst>()` bytes of any `SV`-valid `Src` must be a `DV`-valid
282
- /// `Dst`.
283
- /// - If `size_of::<Src>() < size_of::<Dst>()`, then any `SV`-valid `Src`
284
- /// followed by `size_of::<Dst>() - size_of::<Src>()` uninitialized bytes must
285
- /// be a `DV`-valid `Dst`.
278
+ /// `SV`-valid `Src` into a `DV`-valid `Dst` by value via size-preserving or
279
+ /// size-shrinking transmute. In particular, the first `size_of::<Dst>()` bytes
280
+ /// of any `SV`-valid `Src` must be a `DV`-valid `Dst`.
286
281
///
287
282
/// If either `Src: !Sized` or `Self: !Sized`, then this condition does not need
288
283
/// to hold.
@@ -348,12 +343,18 @@ unsafe impl<T: ?Sized> SizeCompat<T> for T {
348
343
}
349
344
}
350
345
351
- // TODO: Update all `TransmuteFrom` safety proofs.
352
-
353
346
/// `Valid<Src: IntoBytes> → Initialized<Dst>`
354
- // SAFETY: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
355
- // initialized bit patterns, which is exactly the set allowed in the referent of
356
- // any `Initialized` `Ptr`.
347
+ // SAFETY:
348
+ // - By-value: Since `Src: IntoBytes`, the set of valid `Src`'s is the set of
349
+ // initialized bit patterns, which is exactly the set allowed in the referent
350
+ // of any `Initialized` `Ptr`. This holds for both size-preserving and
351
+ // size-shrinking transmutes.
352
+ // - By-reference:
353
+ // - Shrinking: See above.
354
+ // - Tearing: Let `src` be a `Valid` `Src` and `dst` be an `Initialized`
355
+ // `Dst`. The trailing bytes of `dst` have bit validity `[u8; N]`. `src` has
356
+ // bit validity `[u8; M]`. Thus, `dst' = src + trailing_bytes_of(dst)` has
357
+ // bit validity `[u8; N + M]`, which is a valid `Initialized` value.
357
358
unsafe impl < Src , Dst > TransmuteFrom < Src , Valid , Initialized > for Dst
358
359
where
359
360
Src : IntoBytes + ?Sized ,
@@ -365,6 +366,8 @@ where
365
366
// SAFETY: Since `Dst: FromBytes`, any initialized bit pattern may appear in the
366
367
// referent of a `Ptr<Dst, (_, _, Valid)>`. This is exactly equal to the set of
367
368
// bit patterns which may appear in the referent of any `Initialized` `Ptr`.
369
+ //
370
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
368
371
unsafe impl < Src , Dst > TransmuteFrom < Src , Initialized , Valid > for Dst
369
372
where
370
373
Src : ?Sized ,
@@ -379,6 +382,8 @@ where
379
382
/// `Initialized<Src> → Initialized<Dst>`
380
383
// SAFETY: The set of allowed bit patterns in the referent of any `Initialized`
381
384
// `Ptr` is the same regardless of referent type.
385
+ //
386
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
382
387
unsafe impl < Src , Dst > TransmuteFrom < Src , Initialized , Initialized > for Dst
383
388
where
384
389
Src : ?Sized ,
@@ -393,6 +398,8 @@ where
393
398
/// `V<Src> → Uninit<Dst>`
394
399
// SAFETY: A `Dst` with validity `Uninit` permits any byte sequence, and
395
400
// therefore can be transmuted from any value.
401
+ //
402
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
396
403
unsafe impl < Src , Dst , V > TransmuteFrom < Src , V , Uninit > for Dst
397
404
where
398
405
Src : ?Sized ,
@@ -496,6 +503,8 @@ impl_transitive_transmute_from!(T: ?Sized => UnsafeCell<T> => T => Cell<T>);
496
503
// explicitly guaranteed, but it's obvious from `MaybeUninit`'s documentation
497
504
// that this is the intention:
498
505
// https://doc.rust-lang.org/1.85.0/core/mem/union.MaybeUninit.html
506
+ //
507
+ // TODO: Prove `TransmuteFrom` reference transmutation conditions.
499
508
unsafe impl < Src , Dst > TransmuteFrom < Src , Uninit , Valid > for MaybeUninit < Dst > { }
500
509
501
510
// SAFETY: `MaybeUninit<T>` has the same size as `T` [1]. Thus, a pointer cast
0 commit comments