use crate::*;
use bindings::Windows::Win32::Foundation::E_POINTER;
pub unsafe trait Abi: Sized {
type Abi;
fn abi(&self) -> Self::Abi {
unsafe { std::mem::transmute_copy(self) }
}
fn set_abi(&mut self) -> *mut Self::Abi {
self as *mut _ as *mut _
}
unsafe fn from_abi(abi: Self::Abi) -> Result<Self> {
Ok(std::mem::transmute_copy(&abi))
}
fn drop_param(_: &mut Param<Self>) {}
}
unsafe impl<T: Interface> Abi for T {
type Abi = RawPtr;
fn set_abi(&mut self) -> *mut Self::Abi {
panic!("set_abi should not be used with interfaces since it implies nullable.");
}
unsafe fn from_abi(abi: Self::Abi) -> Result<Self> {
let abi: RawPtr = std::mem::transmute_copy(&abi);
if abi.is_null() {
Err(Error::fast_error(E_POINTER))
} else {
Ok(std::mem::transmute_copy(&abi))
}
}
}
unsafe impl<T: Interface> Abi for Option<T> {
type Abi = RawPtr;
fn set_abi(&mut self) -> *mut Self::Abi {
debug_assert!(self.is_none());
self as *mut _ as *mut _
}
unsafe fn from_abi(_: Self::Abi) -> Result<Self> {
panic!("Option<T> should not not be used for return types. Use Result<T> instead.");
}
}