1 use super::*; 2 3 #[doc(hidden)] 4 pub unsafe trait Abi: Sized + Clone { 5 type Abi; 6 type DefaultType: Sized + Clone + PartialEq; 7 8 /// # Safety from_default(value: &Self::DefaultType) -> Result<Self>9 unsafe fn from_default(value: &Self::DefaultType) -> Result<Self> { 10 let value = value as *const _ as *const Self; 11 Ok((*value).clone()) 12 } 13 14 /// # Safety from_abi(abi: Self::Abi) -> Result<Self>15 unsafe fn from_abi(abi: Self::Abi) -> Result<Self> { 16 Ok(std::mem::transmute_copy(&abi)) 17 } 18 19 /// # Safety drop_param(_: &mut Param<Self>)20 unsafe fn drop_param(_: &mut Param<Self>) {} 21 } 22 23 unsafe impl<T> Abi for *mut T { 24 type Abi = Self; 25 type DefaultType = Self; 26 } 27 28 unsafe impl<T> Abi for *const T { 29 type Abi = Self; 30 type DefaultType = Self; 31 } 32 33 unsafe impl<T: Interface> Abi for T { 34 type Abi = RawPtr; 35 type DefaultType = Option<T>; 36 from_default(value: &Self::DefaultType) -> Result<Self>37 unsafe fn from_default(value: &Self::DefaultType) -> Result<Self> { 38 let value = value as *const _ as *const Option<Self>; 39 40 match &*value { 41 Some(value) => Ok(value.clone()), 42 None => Err(Error::OK), 43 } 44 } 45 from_abi(abi: Self::Abi) -> Result<Self>46 unsafe fn from_abi(abi: Self::Abi) -> Result<Self> { 47 let abi: RawPtr = std::mem::transmute_copy(&abi); 48 49 if abi.is_null() { 50 Err(Error::OK) 51 } else { 52 Ok(std::mem::transmute_copy(&abi)) 53 } 54 } 55 } 56 57 unsafe impl<T: Interface> Abi for Option<T> { 58 type Abi = RawPtr; 59 type DefaultType = Self; 60 } 61