Function bytemuck::try_cast_slice

source ·
pub fn try_cast_slice<A: NoUninit, B: AnyBitPattern>(
    a: &[A]
) -> Result<&[B], PodCastError>
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).
  • Similarly, you can’t convert between a ZST and a non-ZST.