1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
use std::error::Error;
use std::marker::PhantomData;
use super::null_ptr_error;
use super::{FromForeign, InputType};
pub struct BoxRefMarshaler<T>(PhantomData<T>);
impl<T> InputType for BoxRefMarshaler<T> {
type Foreign = *mut T;
type ForeignTraitObject = ();
}
// impl<'a, T> FromForeign<*mut Box<T>, &'a Box<T>> for BoxRefMarshaler<T> {
// type Error = Box<dyn Error>;
// #[inline(always)]
// unsafe fn from_foreign(foreign: *mut Box<T>) -> Result<&'a Box<T>, Self::Error> {
// log::debug!(
// "<BoxMarshaler<{ty}> as FromForeign<*mut Box<T>, &'a Box<T>>>::from_foreign({:?})",
// foreign,
// ty = std::any::type_name::<T>()
// );
// if foreign.is_null() {
// return Err(null_ptr_error());
// }
// Ok(unsafe { &*foreign as &'a Box<T> })
// }
// }
impl<'a, T> FromForeign<*mut T, &'a T> for BoxRefMarshaler<T> {
type Error = Box<dyn Error>;
#[inline(always)]
unsafe fn from_foreign(foreign: *mut T) -> Result<&'a T, Self::Error> {
log::debug!(
"<BoxMarshaler<{ty}> as FromForeign<*mut Box<T>, &'a mut Box<T>>>::from_foreign({:?})",
foreign,
ty = std::any::type_name::<T>()
);
if foreign.is_null() {
return Err(null_ptr_error());
}
// let mut boxed = unsafe { Box::from_raw(foreign as *mut _ as *mut _) };
// let ptr = &mut boxed as *mut _;
// std::mem::forget(boxed);
// // let ptr = unsafe { std::mem::transmute::<*mut T, *mut Box<T>>(foreign) };
Ok(&*foreign)
}
}