13 releases (2 stable)

1.1.0 Oct 11, 2021
1.0.0 Jul 7, 2021
0.4.1 Jun 22, 2021
0.3.4 Mar 31, 2021
0.3.0 Nov 23, 2020

#87 in WebAssembly

Download history 1389/week @ 2021-07-01 1191/week @ 2021-07-08 1096/week @ 2021-07-15 1581/week @ 2021-07-22 1088/week @ 2021-07-29 1264/week @ 2021-08-05 1657/week @ 2021-08-12 1744/week @ 2021-08-19 1361/week @ 2021-08-26 1306/week @ 2021-09-02 2274/week @ 2021-09-09 2358/week @ 2021-09-16 2293/week @ 2021-09-23 2491/week @ 2021-09-30 2909/week @ 2021-10-07 2408/week @ 2021-10-14

7,281 downloads per month
Used in 20 crates (via rhai)

MIT/Apache

210KB
5.5K SLoC

Procedural Macros for Plugins

Rhai logo

This crate holds procedural macros for code generation, supporting the plugins system for Rhai.

This crate is automatically referenced by the Rhai crate. Normally it should not be used directly.


lib.rs:

This crate contains procedural macros to make creating Rhai plugin modules much easier.

Export an Entire Rust Module to a Rhai Module

use rhai::{EvalAltResult, FLOAT};
use rhai::plugin::*;
use rhai::module_resolvers::*;

#[export_module]
mod advanced_math {
    pub const MYSTIC_NUMBER: FLOAT = 42.0;

    pub fn euclidean_distance(x1: FLOAT, y1: FLOAT, x2: FLOAT, y2: FLOAT) -> FLOAT {
        ((y2 - y1).abs().powf(2.0) + (x2 -x1).abs().powf(2.0)).sqrt()
    }
}

# fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let m = exported_module!(advanced_math);
let mut r = StaticModuleResolver::new();
r.insert("Math::Advanced", m);
engine.set_module_resolver(r);

assert_eq!(engine.eval::<FLOAT>(
    r#"
        import "Math::Advanced" as math;
        math::euclidean_distance(0.0, 1.0, 0.0, math::MYSTIC_NUMBER)
    "#)?, 41.0);
#   Ok(())
# }

Register a Rust Function with a Rhai Module

use rhai::{EvalAltResult, FLOAT, Module};
use rhai::plugin::*;
use rhai::module_resolvers::*;

#[export_fn]
fn distance_function(x1: FLOAT, y1: FLOAT, x2: FLOAT, y2: FLOAT) -> FLOAT {
    ((y2 - y1).abs().powf(2.0) + (x2 -x1).abs().powf(2.0)).sqrt()
}

# fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.register_fn("get_mystic_number", || 42.0 as FLOAT);
let mut m = Module::new();
set_exported_fn!(m, "euclidean_distance", distance_function);
let mut r = StaticModuleResolver::new();
r.insert("Math::Advanced", m);
engine.set_module_resolver(r);

assert_eq!(engine.eval::<FLOAT>(
    r#"
        import "Math::Advanced" as math;
        math::euclidean_distance(0.0, 1.0, 0.0, get_mystic_number())
    "#)?, 41.0);
# Ok(())
# }

Register a Plugin Function with an Engine

use rhai::{EvalAltResult, FLOAT, Module};
use rhai::plugin::*;
use rhai::module_resolvers::*;

#[export_fn]
pub fn distance_function(x1: FLOAT, y1: FLOAT, x2: FLOAT, y2: FLOAT) -> FLOAT {
    ((y2 - y1).abs().powf(2.0) + (x2 -x1).abs().powf(2.0)).sqrt()
}

# fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine.register_fn("get_mystic_number", || { 42 as FLOAT });
register_exported_fn!(engine, "euclidean_distance", distance_function);

assert_eq!(engine.eval::<FLOAT>(
        "euclidean_distance(0.0, 1.0, 0.0, get_mystic_number())"
    )?, 41.0);
# Ok(())
# }

Dependencies

~305–720KB
~18K SLoC