#scripting-language #plugin #scripting #scripting-engine #proc-macro

macro no-std rhai_codegen

Procedural macros support package for Rhai, a scripting language and engine for Rust

23 releases (12 stable)

new 2.1.0 Apr 12, 2024
2.0.0 Feb 2, 2024
1.17.0 Jan 31, 2024
1.6.0 Sep 9, 2023
0.3.0 Nov 23, 2020

#7 in #scripting-engine

Download history 9856/week @ 2023-12-21 10927/week @ 2023-12-28 15987/week @ 2024-01-04 17280/week @ 2024-01-11 18946/week @ 2024-01-18 17516/week @ 2024-01-25 18638/week @ 2024-02-01 16345/week @ 2024-02-08 17598/week @ 2024-02-15 17136/week @ 2024-02-22 17604/week @ 2024-02-29 17494/week @ 2024-03-07 16160/week @ 2024-03-14 20835/week @ 2024-03-21 19999/week @ 2024-03-28 16274/week @ 2024-04-04

76,178 downloads per month
Used in 134 crates (2 directly)

MIT/Apache

260KB
6.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()
    }
}

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);

Dependencies

~280–720KB
~17K SLoC