-#[repr(C)]
-pub struct CVecTempl<T> {
- pub data: *mut T,
- pub datalen: usize
-}
-impl<T> CVecTempl<T> {
- pub(crate) fn into_rust(&mut self) -> Vec<T> {
- if self.datalen == 0 { return Vec::new(); }
- let ret = unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) }.into();
- self.data = std::ptr::null_mut();
- self.datalen = 0;
- ret
- }
- pub(crate) fn as_slice(&self) -> &[T] {
- unsafe { std::slice::from_raw_parts_mut(self.data, self.datalen) }
- }
-}
-impl<T> From<Vec<T>> for CVecTempl<T> {
- fn from(v: Vec<T>) -> Self {
- let datalen = v.len();
- let data = Box::into_raw(v.into_boxed_slice());
- CVecTempl { datalen, data: unsafe { (*data).as_mut_ptr() } }
- }
-}
-pub extern "C" fn CVecTempl_free<T>(_res: CVecTempl<T>) { }
-impl<T> Drop for CVecTempl<T> {
- fn drop(&mut self) {
- if self.datalen == 0 { return; }
- unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) };
- }
-}
-impl<T: Clone> Clone for CVecTempl<T> {
- fn clone(&self) -> Self {
- let mut res = Vec::new();
- if self.datalen == 0 { return Self::from(res); }
- res.clone_from_slice(unsafe { std::slice::from_raw_parts_mut(self.data, self.datalen) });
- Self::from(res)
- }
-}
-
-#[repr(C)]
-pub struct C2TupleTempl<A, B> {
- pub a: *mut A,
- pub b: *mut B,
-}
-impl<A, B> From<(A, B)> for C2TupleTempl<A, B> {
- fn from(tup: (A, B)) -> Self {
- Self {
- a: Box::into_raw(Box::new(tup.0)),
- b: Box::into_raw(Box::new(tup.1)),
- }
- }
-}
-impl<A, B> C2TupleTempl<A, B> {
- pub(crate) fn to_rust(mut self) -> (A, B) {
- let res = (unsafe { *Box::from_raw(self.a) }, unsafe { *Box::from_raw(self.b) });
- self.a = std::ptr::null_mut();
- self.b = std::ptr::null_mut();
- res
- }
-}
-pub extern "C" fn C2TupleTempl_free<A, B>(_res: C2TupleTempl<A, B>) { }
-impl<A, B> Drop for C2TupleTempl<A, B> {
- fn drop(&mut self) {
- if !self.a.is_null() {
- unsafe { Box::from_raw(self.a) };
- }
- if !self.b.is_null() {
- unsafe { Box::from_raw(self.b) };
- }
- }
-}
-impl <A: Clone, B: Clone> Clone for C2TupleTempl<A, B> {
- fn clone(&self) -> Self {
- Self {
- a: Box::into_raw(Box::new(unsafe { &*self.a }.clone())),
- b: Box::into_raw(Box::new(unsafe { &*self.b }.clone()))
- }
- }
-}
-
-#[repr(C)]
-pub struct C3TupleTempl<A, B, C> {
- pub a: *mut A,
- pub b: *mut B,
- pub c: *mut C,
-}
-impl<A, B, C> From<(A, B, C)> for C3TupleTempl<A, B, C> {
- fn from(tup: (A, B, C)) -> Self {
- Self {
- a: Box::into_raw(Box::new(tup.0)),
- b: Box::into_raw(Box::new(tup.1)),
- c: Box::into_raw(Box::new(tup.2)),
- }
- }
-}
-impl<A, B, C> C3TupleTempl<A, B, C> {
- pub(crate) fn to_rust(mut self) -> (A, B, C) {
- let res = (unsafe { *Box::from_raw(self.a) }, unsafe { *Box::from_raw(self.b) }, unsafe { *Box::from_raw(self.c) });
- self.a = std::ptr::null_mut();
- self.b = std::ptr::null_mut();
- self.c = std::ptr::null_mut();
- res
- }
-}
-pub extern "C" fn C3TupleTempl_free<A, B, C>(_res: C3TupleTempl<A, B, C>) { }
-impl<A, B, C> Drop for C3TupleTempl<A, B, C> {
- fn drop(&mut self) {
- if !self.a.is_null() {
- unsafe { Box::from_raw(self.a) };
- }
- if !self.b.is_null() {
- unsafe { Box::from_raw(self.b) };
- }
- if !self.c.is_null() {
- unsafe { Box::from_raw(self.c) };
- }
- }
-}
-