6 个版本
新版本 0.1.5 | 2024年8月16日 |
---|---|
0.1.4 | 2024年8月10日 |
0.1.2 | 2024年5月11日 |
#1126 在 WebAssembly
255 每月下载量
用于 4 个 crates (2 直接使用)
26KB
419 行
wings
Wings 是 Rust 的 WASM 插件系统。它直接与 Geese 事件库集成,允许插件通过事件和系统无缝地相互通信以及与宿主通信。以下功能得到支持
- 向宿主发送事件:客端可以发送和接收强类型事件到宿主,这些事件将被注入到宿主的 Geese 上下文中。
- 向其他客端系统发送事件:事件将在客端系统之间传播,并从宿主的 Geese 上下文中传播到 WASM 模块。
- 访问宿主系统:宿主可以将 Geese 系统公开为特质对象,这些对象可以直接从客端调用。函数参数和返回类型将在 WASM 边界进行序列化。
- 访问其他插件系统:插件可以将它们的系统公开为特质对象,其他插件可以调用这些对象。就像常规的 Geese 一样,Wings 保证每个系统都是单例 - 如果加载了共享依赖项的不同插件,两个插件都将访问相同的依赖项实例。
- 自动解析插件依赖项/版本:插件作为包含所有传递依赖项的 Rust crates 构建,因此无需担心创建依赖项解析器。每当 Wings 加载一个系统时,它都会从加载的插件集中选择最新的 Semver 兼容版本。
示例
以下是一个简化的 Wings 使用示例。完整代码可在 wings_example
文件夹中找到。
Wings 允许定义如下特质,这些特质可以在宿主代码和插件代码之间共享
// Define a system that the host will expose.
// This can also be used to expose guest systems to other guest systems.
#[system_trait(host)]
pub trait ExampleSystem: 'static {
// Prints a value to the console.
fn print(&self, value: &str);
}
然后,可以从 WASM 中将系统作为特质对象引用
// Define a Wings system that will run within WASM
#[export_system]
pub struct PluginSystem;
impl WingsSystem for PluginSystem {
// Declare a dependency on the exported host system
const DEPENDENCIES: Dependencies = dependencies()
.with::<dyn ExampleSystem>();
// Invoked when the plugin is created
fn new(mut ctx: WingsContextHandle<Self>) -> Self {
// Get the system from WASM and invoke its function
ctx.get::<dyn ExampleSystem>().print(&format!("Hello from WASM!"));
Self
}
}
最后,在宿主上实现系统并将其公开给插件
// Define a host type
pub struct TestHost;
impl Host for TestHost {
// Declare the systems that should be exported to WASM,
// and the traits under which they should be exported
const SYSTEMS: Systems<Self> = systems()
.with::<ExampleSystemImpl>(traits()
.with::<dyn example_host_system::ExampleSystem>());
...
}
// Declare an implementation for the WASM-exported system
pub struct ExampleSystemImpl;
impl ExampleSystem for ExampleSystemImpl {
fn print(&self, value: &str) {
println!("Plugin says '{value}'");
}
}
一般来说,任何可以使用纯 Geese 实现的功能都可以使用 Wings 实现。请参阅 示例 以演示更多功能。
依赖关系
~0.6–1.2MB
~28K SLoC