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