Crate rhai_codegen

Source
Expand description

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

Macros§

combine_with_exported_module
Macro to combine a plugin module into an existing module.
exported_module
Macro to generate a Rhai Module from a plugin module defined via #[export_module].
register_exported_fnDeprecated
Macro to register a plugin function (defined via #[export_fn]) into an Engine.
set_exported_fnDeprecated
Macro to register a plugin function into a Rhai Module.
set_exported_global_fnDeprecated
Macro to register a plugin function into a Rhai Module and expose it globally.

Attribute Macros§

export_fnDeprecated
Attribute, when put on a Rust function, turns it into a plugin function.
export_module
Attribute, when put on a Rust module, turns it into a plugin module.
expose_under_internals
Macro to automatically expose a Rust function, type-def or use statement as pub when under the internals feature.

Derive Macros§

CustomType
Macro to implement the [CustomType][rhai::CustomType] trait.