Skip to content

Commit 4887e03

Browse files
committed
[pointer][transmute] Support generic TransmuteFrom
This commit replaces the `TransparentWrapper` trait with a more generic, bidirectional `TransmuteFrom<T>` trait. `U: TransmuteFrom<T>` indicates that `T` and `U` have the same sizes and that `T` can be transmuted into `U` given certain alignment and validity invariant mappings. Makes progress on #1122 gherrit-pr-id: I25973ea67f59671329a920dd39183c1348942a1a
1 parent 33fc89f commit 4887e03

File tree

7 files changed

+930
-422
lines changed

7 files changed

+930
-422
lines changed

src/impls.rs

+39-39
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,13 @@ mod atomics {
443443
use super::*;
444444

445445
macro_rules! impl_traits_for_atomics {
446-
($($atomics:ident),* $(,)?) => {
446+
($($atomics:ident[$repr:ty]),* $(,)?) => {
447447
$(
448448
impl_known_layout!($atomics);
449-
impl_for_transparent_wrapper!(=> TryFromBytes for $atomics);
450-
impl_for_transparent_wrapper!(=> FromZeros for $atomics);
451-
impl_for_transparent_wrapper!(=> FromBytes for $atomics);
452-
impl_for_transparent_wrapper!(=> IntoBytes for $atomics);
449+
impl_for_transmute_from!(=> TryFromBytes for $atomics[UnsafeCell<$repr>]);
450+
impl_for_transmute_from!(=> FromZeros for $atomics[UnsafeCell<$repr>]);
451+
impl_for_transmute_from!(=> FromBytes for $atomics[UnsafeCell<$repr>]);
452+
impl_for_transmute_from!(=> IntoBytes for $atomics[UnsafeCell<$repr>]);
453453
)*
454454
};
455455
}
@@ -461,13 +461,13 @@ mod atomics {
461461

462462
use super::*;
463463

464-
impl_traits_for_atomics!(AtomicU8, AtomicI8);
464+
impl_traits_for_atomics!(AtomicU8[u8], AtomicI8[i8]);
465465

466466
impl_known_layout!(AtomicBool);
467467

468-
impl_for_transparent_wrapper!(=> TryFromBytes for AtomicBool);
469-
impl_for_transparent_wrapper!(=> FromZeros for AtomicBool);
470-
impl_for_transparent_wrapper!(=> IntoBytes for AtomicBool);
468+
impl_for_transmute_from!(=> TryFromBytes for AtomicBool[UnsafeCell<bool>]);
469+
impl_for_transmute_from!(=> FromZeros for AtomicBool[UnsafeCell<bool>]);
470+
impl_for_transmute_from!(=> IntoBytes for AtomicBool[UnsafeCell<bool>]);
471471

472472
safety_comment! {
473473
/// SAFETY:
@@ -497,7 +497,7 @@ mod atomics {
497497
/// SAFETY:
498498
/// All of these pass an atomic type and that type's native equivalent, as
499499
/// required by the macro safety preconditions.
500-
unsafe_impl_transparent_wrapper_for_atomic!(AtomicU8 [u8], AtomicI8 [i8], AtomicBool [bool]);
500+
unsafe_impl_transmute_from_for_atomic!(AtomicU8 [u8], AtomicI8 [i8], AtomicBool [bool]);
501501
}
502502
}
503503

@@ -508,13 +508,13 @@ mod atomics {
508508

509509
use super::*;
510510

511-
impl_traits_for_atomics!(AtomicU16, AtomicI16);
511+
impl_traits_for_atomics!(AtomicU16[u16], AtomicI16[i16]);
512512

513513
safety_comment! {
514514
/// SAFETY:
515515
/// All of these pass an atomic type and that type's native equivalent, as
516516
/// required by the macro safety preconditions.
517-
unsafe_impl_transparent_wrapper_for_atomic!(AtomicU16 [u16], AtomicI16 [i16]);
517+
unsafe_impl_transmute_from_for_atomic!(AtomicU16 [u16], AtomicI16 [i16]);
518518
}
519519
}
520520

@@ -525,13 +525,13 @@ mod atomics {
525525

526526
use super::*;
527527

528-
impl_traits_for_atomics!(AtomicU32, AtomicI32);
528+
impl_traits_for_atomics!(AtomicU32[u32], AtomicI32[i32]);
529529

530530
safety_comment! {
531531
/// SAFETY:
532532
/// All of these pass an atomic type and that type's native equivalent, as
533533
/// required by the macro safety preconditions.
534-
unsafe_impl_transparent_wrapper_for_atomic!(AtomicU32 [u32], AtomicI32 [i32]);
534+
unsafe_impl_transmute_from_for_atomic!(AtomicU32 [u32], AtomicI32 [i32]);
535535
}
536536
}
537537

@@ -542,13 +542,13 @@ mod atomics {
542542

543543
use super::*;
544544

545-
impl_traits_for_atomics!(AtomicU64, AtomicI64);
545+
impl_traits_for_atomics!(AtomicU64[u64], AtomicI64[i64]);
546546

547547
safety_comment! {
548548
/// SAFETY:
549549
/// All of these pass an atomic type and that type's native equivalent, as
550550
/// required by the macro safety preconditions.
551-
unsafe_impl_transparent_wrapper_for_atomic!(AtomicU64 [u64], AtomicI64 [i64]);
551+
unsafe_impl_transmute_from_for_atomic!(AtomicU64 [u64], AtomicI64 [i64]);
552552
}
553553
}
554554

@@ -559,21 +559,21 @@ mod atomics {
559559

560560
use super::*;
561561

562-
impl_traits_for_atomics!(AtomicUsize, AtomicIsize);
562+
impl_traits_for_atomics!(AtomicUsize[usize], AtomicIsize[isize]);
563563

564564
impl_known_layout!(T => AtomicPtr<T>);
565565

566566
// TODO(#170): Implement `FromBytes` and `IntoBytes` once we implement
567567
// those traits for `*mut T`.
568-
impl_for_transparent_wrapper!(T => TryFromBytes for AtomicPtr<T>);
569-
impl_for_transparent_wrapper!(T => FromZeros for AtomicPtr<T>);
568+
impl_for_transmute_from!(T => TryFromBytes for AtomicPtr<T>[UnsafeCell<*mut T>]);
569+
impl_for_transmute_from!(T => FromZeros for AtomicPtr<T>[UnsafeCell<*mut T>]);
570570

571571
safety_comment! {
572572
/// SAFETY:
573573
/// This passes an atomic type and that type's native equivalent, as
574574
/// required by the macro safety preconditions.
575-
unsafe_impl_transparent_wrapper_for_atomic!(AtomicUsize [usize], AtomicIsize [isize]);
576-
unsafe_impl_transparent_wrapper_for_atomic!(T => AtomicPtr<T> [*mut T]);
575+
unsafe_impl_transmute_from_for_atomic!(AtomicUsize [usize], AtomicIsize [isize]);
576+
unsafe_impl_transmute_from_for_atomic!(T => AtomicPtr<T> [*mut T]);
577577
}
578578
}
579579
}
@@ -603,12 +603,12 @@ safety_comment! {
603603
assert_unaligned!(PhantomData<()>, PhantomData<u8>, PhantomData<u64>);
604604
}
605605

606-
impl_for_transparent_wrapper!(T: Immutable => Immutable for Wrapping<T>);
607-
impl_for_transparent_wrapper!(T: TryFromBytes => TryFromBytes for Wrapping<T>);
608-
impl_for_transparent_wrapper!(T: FromZeros => FromZeros for Wrapping<T>);
609-
impl_for_transparent_wrapper!(T: FromBytes => FromBytes for Wrapping<T>);
610-
impl_for_transparent_wrapper!(T: IntoBytes => IntoBytes for Wrapping<T>);
611-
impl_for_transparent_wrapper!(T: Unaligned => Unaligned for Wrapping<T>);
606+
impl_for_transmute_from!(T: Immutable => Immutable for Wrapping<T>[T]);
607+
impl_for_transmute_from!(T: TryFromBytes => TryFromBytes for Wrapping<T>[T]);
608+
impl_for_transmute_from!(T: FromZeros => FromZeros for Wrapping<T>[T]);
609+
impl_for_transmute_from!(T: FromBytes => FromBytes for Wrapping<T>[T]);
610+
impl_for_transmute_from!(T: IntoBytes => IntoBytes for Wrapping<T>[T]);
611+
impl_for_transmute_from!(T: Unaligned => Unaligned for Wrapping<T>[T]);
612612
assert_unaligned!(Wrapping<()>, Wrapping<u8>);
613613

614614
safety_comment! {
@@ -620,22 +620,22 @@ safety_comment! {
620620
unsafe_impl!(T => FromBytes for MaybeUninit<T>);
621621
}
622622

623-
impl_for_transparent_wrapper!(T: Immutable => Immutable for MaybeUninit<T>);
624-
impl_for_transparent_wrapper!(T: Unaligned => Unaligned for MaybeUninit<T>);
623+
impl_for_transmute_from!(T: Immutable => Immutable for MaybeUninit<T>[T]);
624+
impl_for_transmute_from!(T: Unaligned => Unaligned for MaybeUninit<T>[T]);
625625
assert_unaligned!(MaybeUninit<()>, MaybeUninit<u8>);
626626

627-
impl_for_transparent_wrapper!(T: ?Sized + Immutable => Immutable for ManuallyDrop<T>);
628-
impl_for_transparent_wrapper!(T: ?Sized + TryFromBytes => TryFromBytes for ManuallyDrop<T>);
629-
impl_for_transparent_wrapper!(T: ?Sized + FromZeros => FromZeros for ManuallyDrop<T>);
630-
impl_for_transparent_wrapper!(T: ?Sized + FromBytes => FromBytes for ManuallyDrop<T>);
631-
impl_for_transparent_wrapper!(T: ?Sized + IntoBytes => IntoBytes for ManuallyDrop<T>);
632-
impl_for_transparent_wrapper!(T: ?Sized + Unaligned => Unaligned for ManuallyDrop<T>);
627+
impl_for_transmute_from!(T: ?Sized + Immutable => Immutable for ManuallyDrop<T>[T]);
628+
impl_for_transmute_from!(T: ?Sized + TryFromBytes => TryFromBytes for ManuallyDrop<T>[T]);
629+
impl_for_transmute_from!(T: ?Sized + FromZeros => FromZeros for ManuallyDrop<T>[T]);
630+
impl_for_transmute_from!(T: ?Sized + FromBytes => FromBytes for ManuallyDrop<T>[T]);
631+
impl_for_transmute_from!(T: ?Sized + IntoBytes => IntoBytes for ManuallyDrop<T>[T]);
632+
impl_for_transmute_from!(T: ?Sized + Unaligned => Unaligned for ManuallyDrop<T>[T]);
633633
assert_unaligned!(ManuallyDrop<()>, ManuallyDrop<u8>);
634634

635-
impl_for_transparent_wrapper!(T: ?Sized + FromZeros => FromZeros for UnsafeCell<T>);
636-
impl_for_transparent_wrapper!(T: ?Sized + FromBytes => FromBytes for UnsafeCell<T>);
637-
impl_for_transparent_wrapper!(T: ?Sized + IntoBytes => IntoBytes for UnsafeCell<T>);
638-
impl_for_transparent_wrapper!(T: ?Sized + Unaligned => Unaligned for UnsafeCell<T>);
635+
impl_for_transmute_from!(T: ?Sized + FromZeros => FromZeros for UnsafeCell<T>[T]);
636+
impl_for_transmute_from!(T: ?Sized + FromBytes => FromBytes for UnsafeCell<T>[T]);
637+
impl_for_transmute_from!(T: ?Sized + IntoBytes => IntoBytes for UnsafeCell<T>[T]);
638+
impl_for_transmute_from!(T: ?Sized + Unaligned => Unaligned for UnsafeCell<T>[T]);
639639
assert_unaligned!(UnsafeCell<()>, UnsafeCell<u8>);
640640

641641
// SAFETY: See safety comment in `is_bit_valid` impl.

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2720,7 +2720,7 @@ unsafe fn try_read_from<S, T: TryFromBytes>(
27202720
// We use `from_mut` despite not mutating via `c_ptr` so that we don't need
27212721
// to add a `T: Immutable` bound.
27222722
let c_ptr = Ptr::from_mut(&mut candidate);
2723-
let c_ptr = c_ptr.transparent_wrapper_into_inner();
2723+
let c_ptr = c_ptr.transmute_old::<_, pointer::transmute::BecauseBidirectional>();
27242724
// SAFETY: `c_ptr` has no uninitialized sub-ranges because it derived from
27252725
// `candidate`, which the caller promises is entirely initialized.
27262726
let c_ptr = unsafe { c_ptr.assume_validity::<invariant::Initialized>() };

0 commit comments

Comments
 (0)