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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use crate::ffi::collections::NonNullConst;
use crate::ffi::sys::api::SysBinding;
use crate::ffi::Bool;
use crate::fn_caster::FnCaster;
use crate::sys::sync_handler::SyncHandlerAPI;
use std::ffi::CStr;
pub mod sync_handler;
pub trait SysAPIMin<'interface> {
fn panic(&self, error: Option<impl AsRef<CStr>>) -> !;
fn has_function<U>(&self) -> bool
where
U: FnCaster;
fn get_function<U>(&self, caster: &U) -> Option<<U as FnCaster>::Type>
where
U: FnCaster;
}
pub trait SysAPI<'interface>: SysAPIMin<'interface> {
fn shutdown(&mut self) -> !;
fn get_sync_handler<U>(&self) -> <U as SyncHandlerAPI<'interface>>::Handler
where
U: SyncHandlerAPI<'interface>;
unsafe fn set_sync_handler(&mut self, handler: Option<&impl SyncHandlerAPI<'interface>>);
}
impl<'interface, T> SysAPIMin<'interface> for T
where
T: SysBinding,
{
#[inline]
fn panic(&self, error: Option<impl AsRef<CStr>>) -> ! {
unsafe {
<T as SysBinding>::panic(
self,
error.map(|err| NonNullConst::from(err.as_ref().to_bytes_with_nul())),
)
}
}
#[inline]
fn has_function<U>(&self) -> bool
where
U: FnCaster,
{
unsafe { <T as SysBinding>::has_function(self, U::ID) == Bool::True }
}
#[inline]
fn get_function<U>(&self, caster: &U) -> Option<<U as FnCaster>::Type>
where
U: FnCaster,
{
unsafe {
<T as SysBinding>::get_function(self, U::ID)
.to_option()
.map(|func| caster.cast(func))
}
}
}
impl<'interface, T> SysAPI<'interface> for T
where
T: SysBinding,
{
#[inline]
fn shutdown(&mut self) -> ! {
unsafe { <T as SysBinding>::shutdown(self) }
}
#[inline]
fn get_sync_handler<U>(&self) -> <U as SyncHandlerAPI<'interface>>::Handler
where
U: SyncHandlerAPI<'interface>,
{
unsafe { U::from_interface(<T as SysBinding>::get_sync_handler(self)) }
}
#[inline]
unsafe fn set_sync_handler(&mut self, handler: Option<&impl SyncHandlerAPI<'interface>>) {
<T as SysBinding>::set_sync_handler(self, handler.map(|handler| handler.to_interface()))
}
}