modular_sys/
lib.rs

1#[cfg(feature = "dll")]
2pub mod dll;
3
4pub mod core;
5
6use std::ffi::c_void;
7use std::os::raw::c_char;
8use std::ptr::{null, null_mut};
9
10#[derive(Copy, Clone)]
11#[repr(transparent)]
12pub struct Obj(pub *mut c_void);
13
14unsafe impl Send for Obj {}
15unsafe impl Sync for Obj {}
16
17#[derive(Copy, Clone)]
18#[repr(C)]
19pub struct NativeModularVTable {
20    pub create: unsafe extern "system" fn(threads: u32) -> Obj,
21    pub destroy_instance: unsafe extern "system" fn(modular: Obj),
22    pub subscribe: unsafe extern "system" fn(
23        modular: Obj,
24        subscribe: CSubscribe,
25        *mut CSubscriptionRef,
26    ) -> i32,
27    pub publish: unsafe extern "system" fn(modular: Obj, topic: *const c_char, data: CBuf),
28    pub register_module: unsafe extern "system" fn(
29        modular: Obj,
30        name: *const c_char,
31        module: CModule,
32        replace: bool,
33    ) -> i32,
34    pub remove_module: unsafe extern "system" fn(modular: Obj, name: *const c_char),
35    pub get_module_ref: unsafe extern "system" fn(modular: Obj, name: *const c_char) -> CModuleRef,
36}
37
38#[derive(Copy, Clone)]
39#[repr(C)]
40pub struct CBuf {
41    pub data: *const u8,
42    pub len: usize,
43}
44
45impl Default for CBuf {
46    fn default() -> Self {
47        Self {
48            data: null(),
49            len: 0,
50        }
51    }
52}
53
54#[repr(C)]
55pub struct CSubscribe {
56    pub user_data: Obj,
57    pub topic: *const c_char,
58
59    pub on_event: OnEvent,
60    pub on_unsubscribe: Option<Cleanup>,
61}
62
63#[derive(Copy, Clone)]
64#[repr(C)]
65pub struct CSubscriptionRef {
66    pub user_data: Obj,
67
68    pub subscription_ref: Obj,
69    pub unsubscribe: Cleanup,
70}
71
72impl Default for CSubscriptionRef {
73    fn default() -> Self {
74        extern "system" fn dummy(_: Obj) {}
75
76        Self {
77            user_data: Obj(null_mut()),
78            subscription_ref: Obj(null_mut()),
79            unsubscribe: dummy,
80        }
81    }
82}
83
84#[repr(C)]
85pub struct CModule {
86    pub ptr: Obj,
87
88    pub on_invoke:
89        unsafe extern "system" fn(ptr: Obj, method: *const c_char, data: CBuf, callback: CCallback),
90    pub on_drop: unsafe extern "system" fn(ptr: Obj),
91}
92
93unsafe impl Send for CModule {}
94unsafe impl Sync for CModule {}
95
96#[repr(C)]
97pub struct CModuleError {
98    pub code: i32,
99    pub name: *const c_char,
100    pub message: *const c_char,
101}
102
103#[repr(C)]
104pub struct CCallback {
105    pub ptr: Obj,
106
107    pub success: unsafe extern "system" fn(ptr: Obj, data: CBuf),
108    pub error: unsafe extern "system" fn(ptr: Obj, error: CModuleError),
109    pub unknown_method: unsafe extern "system" fn(ptr: Obj),
110    pub destroyed: unsafe extern "system" fn(ptr: Obj),
111}
112
113unsafe impl Send for CCallback {}
114unsafe impl Sync for CCallback {}
115
116#[derive(Copy, Clone)]
117#[repr(C)]
118pub struct CModuleRef {
119    pub ptr: Obj,
120    pub vtable: CModuleRefVTable,
121}
122
123#[derive(Copy, Clone)]
124#[repr(C)]
125pub struct CModuleRefVTable {
126    pub clone: unsafe extern "system" fn(ptr: Obj) -> CModuleRef,
127    pub drop: unsafe extern "system" fn(ptr: Obj),
128    pub invoke:
129        unsafe extern "system" fn(ptr: Obj, action: *const c_char, data: CBuf, callback: CCallback),
130}
131
132pub type OnEvent =
133    unsafe extern "system" fn(subscription: CSubscriptionRef, topic: *const c_char, data: CBuf);
134
135pub type Cleanup = unsafe extern "system" fn(_: Obj);