5151/// This macro can be invoked in `const` contexts.
5252#[ macro_export]
5353macro_rules! transmute {
54- ( $e: expr) => { {
55- // NOTE: This must be a macro (rather than a function with trait bounds)
56- // because there's no way, in a generic context, to enforce that two
57- // types have the same size. `core::mem::transmute` uses compiler magic
58- // to enforce this so long as the types are concrete.
54+ // NOTE: This must be a macro (rather than a function with trait bounds)
55+ // because there's no way, in a generic context, to enforce that two types
56+ // have the same size. `core::mem::transmute` uses compiler magic to enforce
57+ // this so long as the types are concrete.
58+ ( #![ allow( shrink) ] $e: expr) => { {
59+ let mut e = $e;
60+ if false {
61+ // This branch, though never taken, ensures that the type of `e` is
62+ // `IntoBytes` and that the type of the outer macro invocation
63+ // expression is `FromBytes`.
64+
65+ fn transmute<Src , Dst >( src: Src ) -> Dst
66+ where
67+ Src : $crate:: IntoBytes ,
68+ Dst : $crate:: FromBytes ,
69+ {
70+ let _ = src;
71+ loop { }
72+ }
73+ loop { }
74+ #[ allow( unreachable_code) ]
75+ transmute( e)
76+ } else {
77+ use $crate:: util:: macro_util:: core_reexport:: mem:: ManuallyDrop ;
78+
79+ #[ repr( C ) ]
80+ union Transmute <Src , Dst > {
81+ src: ManuallyDrop <Src >,
82+ dst: ManuallyDrop <Dst >,
83+ }
84+
85+ // TODO: Update this safety comment.
86+ //
87+ // SAFETY: `core::mem::transmute` ensures that the type of `e` and
88+ // the type of this macro invocation expression have the same size.
89+ // We know this transmute is safe thanks to the `IntoBytes` and
90+ // `FromBytes` bounds enforced by the `false` branch.
91+ let u: Transmute <_, _> = unsafe {
92+ // Clippy: We can't annotate the types; this macro is designed
93+ // to infer the types from the calling context.
94+ #[ allow( clippy:: missing_transmute_annotations, unnecessary_transmutes) ]
95+ $crate:: util:: macro_util:: core_reexport:: mem:: transmute( e)
96+ } ;
5997
98+ if false {
99+ // SAFETY: This code is never executed.
100+ e = ManuallyDrop :: into_inner( unsafe { u. src } ) ;
101+ // Suppress the `unused_assignments` lint on the previous line.
102+ let _ = e;
103+ loop { }
104+ } else {
105+ // TODO: Safety comment
106+ let dst = unsafe { u. dst } ;
107+ $crate:: util:: macro_util:: must_use( ManuallyDrop :: into_inner( dst) )
108+ }
109+ }
110+ } } ;
111+ ( $e: expr) => { {
60112 let e = $e;
61113 if false {
62114 // This branch, though never taken, ensures that the type of `e` is
63- // `IntoBytes` and that the type of this macro invocation expression
64- // is `FromBytes`.
65-
66- struct AssertIsIntoBytes <T : $crate:: IntoBytes >( T ) ;
67- let _ = AssertIsIntoBytes ( e) ;
115+ // `IntoBytes` and that the type of the outer macro invocation
116+ // expression is `FromBytes`.
68117
69- struct AssertIsFromBytes <U : $crate:: FromBytes >( U ) ;
70- #[ allow( unused, unreachable_code) ]
71- let u = AssertIsFromBytes ( loop { } ) ;
72- u. 0
118+ fn transmute<Src , Dst >( src: Src ) -> Dst
119+ where
120+ Src : $crate:: IntoBytes ,
121+ Dst : $crate:: FromBytes ,
122+ {
123+ let _ = src;
124+ loop { }
125+ }
126+ loop { }
127+ #[ allow( unreachable_code) ]
128+ transmute( e)
73129 } else {
74130 // SAFETY: `core::mem::transmute` ensures that the type of `e` and
75131 // the type of this macro invocation expression have the same size.
76132 // We know this transmute is safe thanks to the `IntoBytes` and
77133 // `FromBytes` bounds enforced by the `false` branch.
78- //
79- // We use this reexport of `core::mem::transmute` because we know it
80- // will always be available for crates which are using the 2015
81- // edition of Rust. By contrast, if we were to use
82- // `std::mem::transmute`, this macro would not work for such crates
83- // in `no_std` contexts, and if we were to use
84- // `core::mem::transmute`, this macro would not work in `std`
85- // contexts in which `core` was not manually imported. This is not a
86- // problem for 2018 edition crates.
87134 let u = unsafe {
88135 // Clippy: We can't annotate the types; this macro is designed
89136 // to infer the types from the calling context.
@@ -92,7 +139,7 @@ macro_rules! transmute {
92139 } ;
93140 $crate:: util:: macro_util:: must_use( u)
94141 }
95- } }
142+ } } ;
96143}
97144
98145/// Safely transmutes a mutable or immutable reference of one type to an
@@ -1046,6 +1093,10 @@ mod tests {
10461093 let x: [ u8 ; 8 ] = transmute ! ( array_of_arrays) ;
10471094 assert_eq ! ( x, array_of_u8s) ;
10481095
1096+ // Test that memory is transmuted as expected when shrinking.
1097+ let x: [ [ u8 ; 2 ] ; 3 ] = transmute ! ( #![ allow( shrink) ] array_of_u8s) ;
1098+ assert_eq ! ( x, [ [ 0u8 , 1 ] , [ 2 , 3 ] , [ 4 , 5 ] ] ) ;
1099+
10491100 // Test that the source expression's value is forgotten rather than
10501101 // dropped.
10511102 #[ derive( IntoBytes ) ]
@@ -1058,12 +1109,16 @@ mod tests {
10581109 }
10591110 #[ allow( clippy:: let_unit_value) ]
10601111 let _: ( ) = transmute ! ( PanicOnDrop ( ( ) ) ) ;
1112+ #[ allow( clippy:: let_unit_value) ]
1113+ let _: ( ) = transmute ! ( #![ allow( shrink) ] PanicOnDrop ( ( ) ) ) ;
10611114
10621115 // Test that `transmute!` is legal in a const context.
10631116 const ARRAY_OF_U8S : [ u8 ; 8 ] = [ 0u8 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ;
10641117 const ARRAY_OF_ARRAYS : [ [ u8 ; 2 ] ; 4 ] = [ [ 0 , 1 ] , [ 2 , 3 ] , [ 4 , 5 ] , [ 6 , 7 ] ] ;
10651118 const X : [ [ u8 ; 2 ] ; 4 ] = transmute ! ( ARRAY_OF_U8S ) ;
10661119 assert_eq ! ( X , ARRAY_OF_ARRAYS ) ;
1120+ const X_SHRINK : [ [ u8 ; 2 ] ; 3 ] = transmute ! ( #![ allow( shrink) ] ARRAY_OF_U8S ) ;
1121+ assert_eq ! ( X_SHRINK , [ [ 0u8 , 1 ] , [ 2 , 3 ] , [ 4 , 5 ] ] ) ;
10671122
10681123 // Test that `transmute!` works with `!Immutable` types.
10691124 let x: usize = transmute ! ( UnsafeCell :: new( 1usize ) ) ;
0 commit comments