77// This file may not be copied, modified, or distributed except according to
88// those terms.
99
10- use core:: mem:: MaybeUninit as CoreMaybeUninit ;
10+ use core:: { cell :: UnsafeCell , mem:: MaybeUninit as CoreMaybeUninit , ptr :: NonNull } ;
1111
1212use super :: * ;
1313
@@ -223,16 +223,32 @@ safety_comment! {
223223//
224224// String slices are a UTF-8 representation of characters that have the same
225225// layout as slices of type `[u8]`.
226- unsafe impl pointer:: SizeEq < str > for [ u8 ] { }
226+ unsafe impl pointer:: SizeEq < str > for [ u8 ] {
227+ fn cast_from_raw ( s : NonNull < str > ) -> NonNull < [ u8 ] > {
228+ cast ! ( s)
229+ }
230+ }
227231// SAFETY: See previous safety comment.
228- unsafe impl pointer:: SizeEq < [ u8 ] > for str { }
232+ unsafe impl pointer:: SizeEq < [ u8 ] > for str {
233+ fn cast_from_raw ( bytes : NonNull < [ u8 ] > ) -> NonNull < str > {
234+ cast ! ( bytes)
235+ }
236+ }
229237
230238macro_rules! unsafe_impl_try_from_bytes_for_nonzero {
231239 ( $( $nonzero: ident[ $prim: ty] ) ,* ) => {
232240 $(
233241 unsafe_impl!( => TryFromBytes for $nonzero; |n| {
234- unsafe impl pointer:: SizeEq <$nonzero> for Unalign <$prim> { }
235- unsafe impl pointer:: SizeEq <Unalign <$prim>> for $nonzero { }
242+ unsafe impl pointer:: SizeEq <$nonzero> for Unalign <$prim> {
243+ fn cast_from_raw( n: NonNull <$nonzero>) -> NonNull <Unalign <$prim>> {
244+ cast!( n)
245+ }
246+ }
247+ unsafe impl pointer:: SizeEq <Unalign <$prim>> for $nonzero {
248+ fn cast_from_raw( p: NonNull <Unalign <$prim>>) -> NonNull <$nonzero> {
249+ cast!( p)
250+ }
251+ }
236252
237253 let n = n. transmute:: <Unalign <$prim>, invariant:: Valid , _>( ) ;
238254 $nonzero:: new( n. read_unaligned( ) . into_inner( ) ) . is_some( )
@@ -470,6 +486,7 @@ mod atomics {
470486 macro_rules! unsafe_impl_transmute_from_for_atomic {
471487 ( $( $( $tyvar: ident) ? => $atomic: ty [ $prim: ty] ) ,* ) => {
472488 const _: ( ) = {
489+ use core:: { cell:: UnsafeCell , ptr:: NonNull } ;
473490 use crate :: pointer:: { TransmuteFrom , SizeEq , invariant:: Valid } ;
474491
475492 $(
@@ -485,10 +502,18 @@ mod atomics {
485502
486503 // SAFETY: THe caller promised that `$atomic` and `$prim`
487504 // have the same size.
488- unsafe impl <$( $tyvar) ?> SizeEq <$atomic> for $prim { }
505+ unsafe impl <$( $tyvar) ?> SizeEq <$atomic> for $prim {
506+ fn cast_from_raw( a: NonNull <$atomic>) -> NonNull <$prim> {
507+ cast!( a)
508+ }
509+ }
489510 // SAFETY: THe caller promised that `$atomic` and `$prim`
490511 // have the same size.
491- unsafe impl <$( $tyvar) ?> SizeEq <$prim> for $atomic { }
512+ unsafe impl <$( $tyvar) ?> SizeEq <$prim> for $atomic {
513+ fn cast_from_raw( p: NonNull <$prim>) -> NonNull <$atomic> {
514+ cast!( p)
515+ }
516+ }
492517 // SAFETY: The caller promised that `$atomic` and `$prim`
493518 // have the same size. `UnsafeCell<T>` has the same size as
494519 // `T` [1].
@@ -498,9 +523,17 @@ mod atomics {
498523 // `UnsafeCell<T>` has the same in-memory representation as
499524 // its inner type `T`. A consequence of this guarantee is that
500525 // it is possible to convert between `T` and `UnsafeCell<T>`.
501- unsafe impl <$( $tyvar) ?> SizeEq <$atomic> for core:: cell:: UnsafeCell <$prim> { }
526+ unsafe impl <$( $tyvar) ?> SizeEq <$atomic> for UnsafeCell <$prim> {
527+ fn cast_from_raw( a: NonNull <$atomic>) -> NonNull <UnsafeCell <$prim>> {
528+ cast!( a)
529+ }
530+ }
502531 // SAFETY: See previous safety comment.
503- unsafe impl <$( $tyvar) ?> SizeEq <core:: cell:: UnsafeCell <$prim>> for $atomic { }
532+ unsafe impl <$( $tyvar) ?> SizeEq <UnsafeCell <$prim>> for $atomic {
533+ fn cast_from_raw( p: NonNull <UnsafeCell <$prim>>) -> NonNull <$atomic> {
534+ cast!( p)
535+ }
536+ }
504537
505538 // SAFETY: The caller promised that `$atomic` and `$prim`
506539 // have the same bit validity. `UnsafeCell<T>` has the same
@@ -812,36 +845,7 @@ safety_comment! {
812845 unsafe_impl!( T : ?Sized + Immutable => Immutable for ManuallyDrop <T >) ;
813846}
814847
815- // SAFETY: See inline safety comment justifying that the implementation of
816- // `is_bit_valid`is sound.
817- unsafe impl < T : ?Sized + TryFromBytes > TryFromBytes for ManuallyDrop < T > {
818- #[ allow( clippy:: missing_inline_in_public_items) ]
819- fn only_derive_is_allowed_to_implement_this_trait ( ) { }
820-
821- #[ inline( always) ]
822- fn is_bit_valid < A : crate :: pointer:: invariant:: Reference > (
823- candidate : Maybe < ' _ , Self , A > ,
824- ) -> bool {
825- // SAFETY: `ManuallyDrop<T>` and `T` have the same size [1], so this
826- // cast preserves size. It also preserves provenance.
827- //
828- // [1] Per https://doc.rust-lang.org/1.85.0/std/mem/struct.ManuallyDrop.html:
829- //
830- // `ManuallyDrop<T>` is guaranteed to have the same layout and bit
831- // validity as `T`
832- let c: Maybe < ' _ , T , A > = unsafe { candidate. cast_unsized ( cast ! ( ) ) } ;
833-
834- // SAFETY: `ManuallyDrop<T>` and `T` have the same bit validity [1], so
835- // this is a sound implementation of `ManuallyDrop::is_bit_valid`.
836- //
837- // [1] Per https://doc.rust-lang.org/1.85.0/std/mem/struct.ManuallyDrop.html:
838- //
839- // `ManuallyDrop<T>` is guaranteed to have the same layout and bit
840- // validity as `T`
841- <T as TryFromBytes >:: is_bit_valid ( c)
842- }
843- }
844-
848+ impl_for_transmute_from ! ( T : ?Sized + TryFromBytes => TryFromBytes for ManuallyDrop <T >[ <T >] ) ;
845849impl_for_transmute_from ! ( T : ?Sized + FromZeros => FromZeros for ManuallyDrop <T >[ <T >] ) ;
846850impl_for_transmute_from ! ( T : ?Sized + FromBytes => FromBytes for ManuallyDrop <T >[ <T >] ) ;
847851impl_for_transmute_from ! ( T : ?Sized + IntoBytes => IntoBytes for ManuallyDrop <T >[ <T >] ) ;
0 commit comments