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 , packed) ]
80+ union Transmute <Src , Dst > {
81+ src: ManuallyDrop <Src >,
82+ dst: ManuallyDrop <Dst >,
83+ }
5984
85+ // SAFETY: `Transmute` is a `reper(C)` union whose `src` field has
86+ // type `ManuallyDrop<Src>`. Thus, the `src` field starts at byte
87+ // offset 0 within `Transmute` [1]. `ManuallyDrop<T>` has the same
88+ // layout and bit validity as `T`, so it is sound to transmute `Src`
89+ // to `Transmute`.
90+ //
91+ // [1] https://doc.rust-lang.org/1.85.0/reference/type-layout.html#reprc-unions
92+ //
93+ // [2] Per https://doc.rust-lang.org/1.85.0/std/mem/struct.ManuallyDrop.html:
94+ //
95+ // `ManuallyDrop<T>` is guaranteed to have the same layout and bit
96+ // validity as `T`
97+ let u: Transmute <_, _> = unsafe {
98+ // Clippy: We can't annotate the types; this macro is designed
99+ // to infer the types from the calling context.
100+ #[ allow( clippy:: missing_transmute_annotations) ]
101+ $crate:: util:: macro_util:: core_reexport:: mem:: transmute( e)
102+ } ;
103+
104+ if false {
105+ // SAFETY: This code is never executed.
106+ e = ManuallyDrop :: into_inner( unsafe { u. src } ) ;
107+ // Suppress the `unused_assignments` lint on the previous line.
108+ let _ = e;
109+ loop { }
110+ } else {
111+ // Per the safety comment on `let u` above, the `dst` field in
112+ // `Transmute` starts at byte offset 0, and has the same layout
113+ // and bit validity as `Dst`.
114+ //
115+ // Transmuting `Src` to `Transmute<Src, Dst>` above using
116+ // `core::mem::transmute` ensures that `size_of::<Src>() ==
117+ // size_of::<Transmute<Src, Dst>>()`. A `#[repr(C, packed)]`
118+ // union has the maximum size of all of its fields [1], so this
119+ // is equivalent to `size_of::<Src>() >= size_of::<Dst>()`.
120+ //
121+ // The outer `if`'s `false` branch ensures that `Src: IntoBytes`
122+ // and `Dst: FromBytes`. This, combined with the size bound,
123+ // ensures that this transmute is sound.
124+ //
125+ // Per https://doc.rust-lang.org/1.85.0/reference/type-layout.html#reprc-unions:
126+ //
127+ // The union will have a size of the maximum size of all of
128+ // its fields rounded to its alignment
129+ let dst = unsafe { u. dst } ;
130+ $crate:: util:: macro_util:: must_use( ManuallyDrop :: into_inner( dst) )
131+ }
132+ }
133+ } } ;
134+ ( $e: expr) => { {
60135 let e = $e;
61136 if false {
62137 // 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`.
138+ // `IntoBytes` and that the type of the outer macro invocation
139+ // expression is `FromBytes`.
65140
66- struct AssertIsIntoBytes <T : $crate:: IntoBytes >( T ) ;
67- let _ = AssertIsIntoBytes ( e) ;
68-
69- struct AssertIsFromBytes <U : $crate:: FromBytes >( U ) ;
70- #[ allow( unused, unreachable_code) ]
71- let u = AssertIsFromBytes ( loop { } ) ;
72- u. 0
141+ fn transmute<Src , Dst >( src: Src ) -> Dst
142+ where
143+ Src : $crate:: IntoBytes ,
144+ Dst : $crate:: FromBytes ,
145+ {
146+ let _ = src;
147+ loop { }
148+ }
149+ loop { }
150+ #[ allow( unreachable_code) ]
151+ transmute( e)
73152 } else {
74153 // SAFETY: `core::mem::transmute` ensures that the type of `e` and
75154 // the type of this macro invocation expression have the same size.
76155 // We know this transmute is safe thanks to the `IntoBytes` and
77156 // `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.
87157 let u = unsafe {
88158 // Clippy: We can't annotate the types; this macro is designed
89159 // to infer the types from the calling context.
@@ -92,7 +162,7 @@ macro_rules! transmute {
92162 } ;
93163 $crate:: util:: macro_util:: must_use( u)
94164 }
95- } }
165+ } } ;
96166}
97167
98168/// Safely transmutes a mutable or immutable reference of one type to an
@@ -1046,6 +1116,10 @@ mod tests {
10461116 let x: [ u8 ; 8 ] = transmute ! ( array_of_arrays) ;
10471117 assert_eq ! ( x, array_of_u8s) ;
10481118
1119+ // Test that memory is transmuted as expected when shrinking.
1120+ let x: [ [ u8 ; 2 ] ; 3 ] = transmute ! ( #![ allow( shrink) ] array_of_u8s) ;
1121+ assert_eq ! ( x, [ [ 0u8 , 1 ] , [ 2 , 3 ] , [ 4 , 5 ] ] ) ;
1122+
10491123 // Test that the source expression's value is forgotten rather than
10501124 // dropped.
10511125 #[ derive( IntoBytes ) ]
@@ -1058,12 +1132,16 @@ mod tests {
10581132 }
10591133 #[ allow( clippy:: let_unit_value) ]
10601134 let _: ( ) = transmute ! ( PanicOnDrop ( ( ) ) ) ;
1135+ #[ allow( clippy:: let_unit_value) ]
1136+ let _: ( ) = transmute ! ( #![ allow( shrink) ] PanicOnDrop ( ( ) ) ) ;
10611137
10621138 // Test that `transmute!` is legal in a const context.
10631139 const ARRAY_OF_U8S : [ u8 ; 8 ] = [ 0u8 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ;
10641140 const ARRAY_OF_ARRAYS : [ [ u8 ; 2 ] ; 4 ] = [ [ 0 , 1 ] , [ 2 , 3 ] , [ 4 , 5 ] , [ 6 , 7 ] ] ;
10651141 const X : [ [ u8 ; 2 ] ; 4 ] = transmute ! ( ARRAY_OF_U8S ) ;
10661142 assert_eq ! ( X , ARRAY_OF_ARRAYS ) ;
1143+ const X_SHRINK : [ [ u8 ; 2 ] ; 3 ] = transmute ! ( #![ allow( shrink) ] ARRAY_OF_U8S ) ;
1144+ assert_eq ! ( X_SHRINK , [ [ 0u8 , 1 ] , [ 2 , 3 ] , [ 4 , 5 ] ] ) ;
10671145
10681146 // Test that `transmute!` works with `!Immutable` types.
10691147 let x: usize = transmute ! ( UnsafeCell :: new( 1usize ) ) ;
0 commit comments