3 个版本

0.1.2 2024 年 5 月 9 日
0.1.1 2024 年 5 月 9 日
0.1.0 2024 年 5 月 4 日

#9 in #reload

Download history 153/week @ 2024-04-30 269/week @ 2024-05-07 5/week @ 2024-05-14 8/week @ 2024-05-21

每月下载 87 次

Apache-2.0

4MB
418

Jude 这是一个为 libloading 编写的微小宏,使得编写插件更加容易 Jude 依赖 libloading

您可以在不实现结构的情况下编写结构,并从插件中加载功能

代码示例

// examples/jude_plugin.rs
#[repr(C)]
#[derive(Clone, Debug)]
pub struct JudePlugin {
    pub word: String,
    pub one: u8,
    pub two: f32,
    pub tree: bool,
}

#[no_mangle]
pub fn who_am_i(_self: &JudePlugin) {
    println!("my name is Jude");
}
// examples/jude_plugin_app.rs
use std::{ffi::OsString, thread, time::Duration};

use jude::jude;

jude! (
    #[repr(C)]
    #[derive(Debug, Clone)]
    pub struct JudePlugin {
        pub word: String = String::from("example string"),
        pub one: u8 = 1,
        pub two: f32 = 2.0,
        pub tree: bool = true,

        pub fn who_am_i(&self),
    }
);

fn main() -> Result<(), libloading::Error> {
    let mut lib =
        JudePlugin::_load_from(OsString::from("target/debug/examples/libjude_plugin.dylib"))?;

    loop {
        if let Ok(true) = lib._is_changed() {
            if let Err(e) = lib._reload() {
                println!("{:?}", e);
                break;
            }
        }

        lib.who_am_i();

        thread::sleep(Duration::from_secs(1));
    }

    Ok(())
}

用法

# Cargo.toml
[dependencies]
jude = "0.1.*"

示例构建

1, cargo build --examples
2. cargo run --example jude_plugin_app
3. change who_am_i func examples/jude_plugin.rs 
4. cargo build --exmaples

依赖项