main.rs

 1use mlua::Lua;
 2
 3use plugin_runtime::*;
 4
 5pub fn main() -> anyhow::Result<()> {
 6    let plugin = WasmPlugin {
 7        source_bytes: include_bytes!(
 8            "../plugin/target/wasm32-unknown-unknown/release/cargo_test.wasm"
 9        )
10        .to_vec(),
11    };
12
13    let mut sum = Wasm::init(plugin)?;
14    let strings = "I hope you have a nice day"
15        .split(" ")
16        .map(|x| x.to_string())
17        .collect();
18    let result = sum.sum_lengths(strings);
19
20    dbg!(result);
21
22    Ok(())
23}
24
25// struct SumLengths {
26//     sum_lengths: Handle,
27// }
28
29// impl Interface for SumLengths {
30//     fn from_runtime<T: Runtime>(runtime: &mut T) -> Option<Self> {
31//         Some(SumLengths {
32//             sum_lengths: runtime.handle_for("sum_lengths")?,
33//         })
34//     }
35// }
36
37// impl SumLengths {
38//     fn sum_lengths<T: Runtime>(&self, runtime: &mut T, strings: Vec<String>) -> Option<usize> {
39//         runtime.call(&self.sum_lengths, strings).ok()
40//     }
41// }
42
43// #[plugin::interface]
44trait SumLengths {
45    fn sum_lengths(&mut self, strings: Vec<String>) -> usize;
46}
47
48impl<T: Runtime> SumLengths for T {
49    fn sum_lengths(&mut self, strings: Vec<String>) -> usize {
50        let result = self.call("sum_lengths", strings).ok().unwrap();
51        return result;
52    }
53}