Skip to content

Instantly share code, notes, and snippets.

@fizyk20
Created November 17, 2017 16:51
Show Gist options
  • Save fizyk20/e0c541bf63d56cc8b343e9961c824242 to your computer and use it in GitHub Desktop.
Save fizyk20/e0c541bf63d56cc8b343e9961c824242 to your computer and use it in GitHub Desktop.
/// Consumes a generic array, splitting it at a given position.
pub fn split<M: ArrayLength<T>>(self) -> (GenericArray<T, M>, GenericArray<T, Diff<N, M>>)
where
N: Sub<M>,
Diff<N, M>: ArrayLength<T>,
{
unsafe {
let array_ptr = &self as *const GenericArray<T, N>;
let ptr1 = mem::transmute::<_, *const GenericArray<T, M>>(array_ptr);
let tmp = mem::transmute::<_, *const T>(array_ptr);
let ptr2 = mem::transmute::<_, *const GenericArray<T, Diff<N, M>>>(
tmp.offset(M::to_usize() as isize),
);
(ptr::read(ptr1), ptr::read(ptr2))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment