Function constmuck::cast_ref_alt

source ·
pub const fn cast_ref_alt<T, U>(from: &T) -> &U
where T: NoUninit, U: AnyBitPattern,
Expand description

Cast a &T to &U

Panics

This function panics in these cases:

  • The alignment of T is larger than U
  • The size of T is not equal to U

Difference with bytemuck

This function requires T to have an alignment larger than or equal to U. bytemuck::cast_ref allows T to have to have a lower alignment than U, so long as the from reference happens to be aligned to U.

Example

use constmuck::cast_ref_alt;

const U8: &[u8; 2] = cast_ref_alt(&100u16.to_le());

assert_eq!(U8[0], 100u8);
assert_eq!(U8[1], 0);