17 releases (6 stable)

1.4.1 Jun 5, 2022
1.4.0 Mar 30, 2022
1.3.0 Jan 11, 2022
1.2.0 Nov 19, 2021
0.3.0 Nov 23, 2020

#83 in Procedural macros

Download history 3764/week @ 2022-03-08 3773/week @ 2022-03-15 3648/week @ 2022-03-22 4593/week @ 2022-03-29 4379/week @ 2022-04-05 4709/week @ 2022-04-12 4758/week @ 2022-04-19 4844/week @ 2022-04-26 5084/week @ 2022-05-03 4258/week @ 2022-05-10 3873/week @ 2022-05-17 3663/week @ 2022-05-24 4440/week @ 2022-05-31 4590/week @ 2022-06-07 4219/week @ 2022-06-14 4081/week @ 2022-06-21

17,994 downloads per month
Used in 37 crates (2 directly)

MIT/Apache

230KB
6K 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

~240–650KB
~16K SLoC