bytemuck::checked

Function try_cast_slice

source
pub fn try_cast_slice<A: NoUninit, B: CheckedBitPattern>(
    a: &[A],
) -> Result<&[B], CheckedCastError>
Expand description

Try to convert &[A] into &[B] (possibly with a change in length).

  • input.as_ptr() as usize == output.as_ptr() as usize
  • input.len() * size_of::<A>() == output.len() * size_of::<B>()

§Failure

  • If the target type has a greater alignment requirement and the input slice isn’t aligned.
  • If the target element type is a different size from the current element type, and the output slice wouldn’t be a whole number of elements when accounting for the size change (eg: 3 u16 values is 1.5 u32 values, so that’s a failure).
  • If any element of the converted slice would contain an invalid bit pattern for B this fails.