use cauchy::*;
use std::mem::MaybeUninit;
pub(crate) trait AsPtr: Sized {
type Elem;
fn as_ptr(vec: &[Self]) -> *const Self::Elem;
fn as_mut_ptr(vec: &mut [Self]) -> *mut Self::Elem;
}
macro_rules! impl_as_ptr {
($target:ty, $elem:ty) => {
impl AsPtr for $target {
type Elem = $elem;
fn as_ptr(vec: &[Self]) -> *const Self::Elem {
vec.as_ptr() as *const _
}
fn as_mut_ptr(vec: &mut [Self]) -> *mut Self::Elem {
vec.as_mut_ptr() as *mut _
}
}
};
}
impl_as_ptr!(i32, i32);
impl_as_ptr!(f32, f32);
impl_as_ptr!(f64, f64);
impl_as_ptr!(c32, lapack_sys::__BindgenComplex<f32>);
impl_as_ptr!(c64, lapack_sys::__BindgenComplex<f64>);
impl_as_ptr!(MaybeUninit<i32>, i32);
impl_as_ptr!(MaybeUninit<f32>, f32);
impl_as_ptr!(MaybeUninit<f64>, f64);
impl_as_ptr!(MaybeUninit<c32>, lapack_sys::__BindgenComplex<f32>);
impl_as_ptr!(MaybeUninit<c64>, lapack_sys::__BindgenComplex<f64>);
pub(crate) trait VecAssumeInit {
type Elem;
unsafe fn assume_init(self) -> Vec<Self::Elem>;
unsafe fn slice_assume_init_ref(&self) -> &[Self::Elem];
unsafe fn slice_assume_init_mut(&mut self) -> &mut [Self::Elem];
}
impl<T> VecAssumeInit for Vec<MaybeUninit<T>> {
type Elem = T;
unsafe fn assume_init(self) -> Vec<T> {
let mut me = std::mem::ManuallyDrop::new(self);
Vec::from_raw_parts(me.as_mut_ptr() as *mut T, me.len(), me.capacity())
}
unsafe fn slice_assume_init_ref(&self) -> &[T] {
std::slice::from_raw_parts(self.as_ptr() as *const T, self.len())
}
unsafe fn slice_assume_init_mut(&mut self) -> &mut [T] {
std::slice::from_raw_parts_mut(self.as_mut_ptr() as *mut T, self.len())
}
}
pub(crate) fn vec_uninit<T: Sized>(n: usize) -> Vec<MaybeUninit<T>> {
let mut v = Vec::with_capacity(n);
unsafe {
v.set_len(n);
}
v
}