use std::mem;
use std::mem::MaybeUninit;
use std::slice;
pub(crate) fn vec_spare_capacity_mut<A>(vec: &mut Vec<A>) -> &mut [MaybeUninit<A>] {
unsafe {
slice::from_raw_parts_mut(
vec.as_mut_ptr().add(vec.len()) as *mut MaybeUninit<A>,
vec.capacity() - vec.len(),
)
}
}
pub(crate) fn maybe_uninit_write_slice<'a, T>(
this: &'a mut [MaybeUninit<T>],
src: &[T],
) -> &'a mut [T]
where
T: Copy,
{
let uninit_src: &[MaybeUninit<T>] = unsafe { mem::transmute(src) };
this.copy_from_slice(uninit_src);
unsafe { &mut *(this as *mut [MaybeUninit<T>] as *mut [T]) }
}
#[inline]
pub(crate) unsafe fn maybe_ununit_array_assume_init<T, const N: usize>(
array: [MaybeUninit<T>; N],
) -> [T; N] {
(&array as *const _ as *const [T; N]).read()
}
#[inline]
pub(crate) fn maybe_uninit_write<T>(uninit: &mut MaybeUninit<T>, val: T) -> &mut T {
*uninit = MaybeUninit::new(val);
unsafe { &mut *uninit.as_mut_ptr() }
}