macro_rules! transmute { ($e:expr) => { ... }; }
Expand description
Safely transmutes a value of one type to a value of another type of the same size.
This macro behaves like an invocation of this function:
ⓘ
const fn transmute<Src, Dst>(src: Src) -> Dst
where
Src: IntoBytes,
Dst: FromBytes,
size_of::<Src>() == size_of::<Dst>(),
{
...
}
However, unlike a function, this macro can only be invoked when the types of
Src
and Dst
are completely concrete. The types Src
and Dst
are
inferred from the calling context; they cannot be explicitly specified in
the macro invocation.
Note that the Src
produced by the expression $e
will not be dropped.
Semantically, its bits will be copied into a new value of type Dst
, the
original Src
will be forgotten, and the value of type Dst
will be
returned.
§Examples
let one_dimensional: [u8; 8] = [0, 1, 2, 3, 4, 5, 6, 7];
let two_dimensional: [[u8; 4]; 2] = transmute!(one_dimensional);
assert_eq!(two_dimensional, [[0, 1, 2, 3], [4, 5, 6, 7]]);
§Use in const
contexts
This macro can be invoked in const
contexts.