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
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: MIT
//! C 兼容插件 VTable — 动态库插件的 ABI 契约
//! C-compatible plugin VTable — ABI contract for dynamic library plugins.
//!
//! ## 插件 ABI 规范
//!
//! 每个动态插件必须导出一个 C 函数:
//!
//! ```rust,ignore
//! #[no_mangle]
//! pub extern "C" fn libplugin_entry() -> PluginVTable { ... }
//! ```
//!
//! 返回的 `PluginVTable` 包含所有生命周期回调的函数指针。
//! 所有字符串通过 C 字符串(null-terminated UTF-8)传递。
//!
//! Each dynamic plugin must export one C function:
//!
//! ```rust,ignore
//! #[no_mangle]
//! pub extern "C" fn libplugin_entry() -> PluginVTable { ... }
//! ```
//!
//! The returned `PluginVTable` holds function pointers for every lifecycle
//! callback. All strings cross the boundary as null-terminated UTF-8.
//!
//! ### 字符串所有权约定 / String ownership
//!
//! | 函数 / Fn | 字符串参数 / String args | 所有权 / Owner | 返回字符串 / Returned | 所有权 / Owner |
//!------|-----------|--------|-----------|--------|
//! | `name()` | 无 / none | — | 静态字符串 / static | 插件拥有,永不释放 / plugin-owned, never freed |
//! | `version()` | 无 / none | — | 静态字符串 / static | 插件拥有,永不释放 / plugin-owned, never freed |
//! | `on_load(config)` | JSON 配置 / JSON config | 宿主拥有,只读 / host-owned, read-only | 无 / none | — |
//! | `on_unload()` | 无 / none | — | 无 / none | — |
//! | `on_message(msg, out, out_len)` | 消息 JSON / message JSON | 宿主拥有,只读 / host-owned, read-only | 写入 out 缓冲区 / written to out | 宿主拥有 / host-owned |
//! | `on_tick()` | 无 / none | — | 无 / none | — |
//! | `on_shutdown()` | 无 / none | — | 无 / none | — |
use ;
/// C 兼容插件 VTable。
/// C-compatible plugin VTable.
///
/// 插件通过 `libplugin_entry()` 导出此结构体。
/// Plugins export this struct via `libplugin_entry()`.
/// 插件入口点符号名(null-terminated,供 libloading::Library::get 使用)
/// Plugin entry point symbol name (null-terminated, for libloading::Library::get)
pub const PLUGIN_ENTRY_SYMBOL: & = b"libplugin_entry\0";
/// 插件入口点函数类型
/// Plugin entry point function type
pub type PluginEntryFn = unsafe extern "C" fn ;
/// `on_message` 输出缓冲区默认大小(64 KB)
/// Default `on_message` output buffer size (64 KB)
pub const MESSAGE_BUFFER_SIZE: usize = 65536;
// ── 辅助函数 / Helpers ──
/// 将 C 字符串指针安全转换为 Rust String。
/// 如果指针为 null,返回空字符串。
///
/// Convert a C string pointer to a Rust String.
/// Returns an empty string for a null pointer.
///
/// # Safety
///
/// 调用者必须确保 `ptr` 指向一个有效的 null-terminated UTF-8 字符串。
/// The caller must guarantee `ptr` points to a valid null-terminated UTF-8 string.
pub unsafe