bytemuck

Function must_cast_ref

Source
pub const fn must_cast_ref<A: NoUninit, B: AnyBitPattern>(a: &A) -> &B
Expand description

Convert &A into &B if infalliable, or fail to compile.

§Failure

  • If the target type has a greater alignment requirement.
  • If the source type and target type aren’t the same size.

§Examples

// compiles:
let bytes: &[u8; 2] = bytemuck::must_cast_ref(&12_u16);
// fails to compile (size mismatch):
let bytes : &[u8; 3] = bytemuck::must_cast_ref(&12_u16);
// fails to compile (alignment requirements increased):
let bytes : &u16 = bytemuck::must_cast_ref(&[1u8, 2u8]);