Function constmuck::cast_ref_alt [−][src]
pub const fn cast_ref_alt<T, U>(
from: &T,
bounds: (ImplsPod<T>, ImplsPod<U>)
) -> &U
Expand description
Cast a &T
to &U
Requires both T
and U
to implement Pod
.
Panics
This function panics in these cases:
- The alignment of
T
is larger thanU
- The size of
T
is not equal toU
Difference with bytemuck
This function requires T
to have an alignment larger or equal to U
,
while bytemuck::cast_ref
only requires from
to happen to be aligned
to U
.
Example
use constmuck::{cast_ref_alt, infer};
const U8: &[u8; 2] = cast_ref_alt(&100u16.to_le(), infer!());
assert_eq!(U8[0], 100u8);
assert_eq!(U8[1], 0);