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
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::*;
use bindings::Windows::Win32::SystemServices::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.");
}
}