Function bytemuck::internal::try_cast_slice

source ·
pub(crate) unsafe fn try_cast_slice<A: Copy, B: Copy>(
    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).