devela::_dep::bytemuck

Function must_cast_ref

pub const fn must_cast_ref<A, B>(a: &A) -> &B
where A: NoUninit, B: AnyBitPattern,
Available on crate feature dep_bytemuck only.
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]);