Expand description
§Rhai - embedded scripting for Rust
Rhai is a tiny, simple and fast embedded scripting language for Rust that gives you a safe and easy way to add scripting to your applications.
It provides a familiar syntax based on JavaScript+Rust and a simple Rust interface.
§A Quick Example
§Contents of my_script.rhai
ⓘ
/// Brute force factorial function
fn factorial(x) {
if x == 1 { return 1; }
x * factorial(x - 1)
}
// Calling an external function 'compute'
compute(factorial(10))
§The Rust part
use quad_compat_rhai::{Engine, EvalAltResult};
fn main() -> Result<(), Box<EvalAltResult>>
{
// Define external function
fn compute_something(x: i64) -> bool {
(x % 40) == 0
}
// Create scripting engine
let mut engine = Engine::new();
// Register external function as 'compute'
engine.register_fn("compute", compute_something);
assert_eq!(
// Evaluate the script, expects a 'bool' return
engine.eval_file::<bool>("my_script.rhai".into())?,
true
);
Ok(())
}
§Documentation
See The Rhai Book for details on the Rhai scripting engine and language.
Modules§
- Module containing all built-in module resolvers available to Rhai. Module containing all built-in module resolvers.
- Module containing all built-in packages available to Rhai, plus facilities to define custom packages.
- Module defining macros for developing plugins.
- (serde) Serialization and deserialization support for
serde
. Exported under theserde
feature only.
Macros§
- Macro to combine a plugin module into an existing module.
- Macro that makes it easy to define a package (which is basically a shared module) and register functions into it.
- Macro to generate a Rhai
Module
from a plugin module defined via#[export_module]
. - Macro to register a plugin function (defined via
#[export_fn]
) into anEngine
. - Macro to register a plugin function into a Rhai
Module
. - Macro to register a plugin function into a Rhai
Module
and expose it globally.
Structs§
- Compiled AST (abstract syntax tree) of a Rhai script.
- (internals) A binary expression. Exported under the
internals
feature only. - (internals) A custom syntax expression. Exported under the
internals
feature only. - Dynamic type containing any value.
- (internals) Lock guard for reading a
Dynamic
. Exported under theinternals
feature only. - (internals) Lock guard for writing a
Dynamic
. Exported under theinternals
feature only. - Rhai main scripting engine.
- Context of a script evaluation process.
- (internals) A type that holds all the current states of the
Engine
. Exported under theinternals
feature only. - An expression sub-tree in an
AST
. - A type that wraps a floating-point number and implements
Hash
. - (internals) A function call. Exported under the
internals
feature only. - (internals) A set of function call hashes. Exported under the
internals
feature only. - A general function pointer, which may carry additional (i.e. curried) argument values to be passed onto a function during a call.
- (internals) An entry in a function resolution cache. Exported under the
internals
feature only. - (internals) An identifier containing a name and a position. Exported under the
internals
feature only. - (internals) A factory of identifiers from text strings. Exported under the
internals
feature only. - The system immutable string type.
- (internals) A stack of imported modules plus mutable runtime global states. Exported under the
internals
feature only. - A module which may contain variables, sub-modules, external Rust functions, and/or script-defined functions.
- (internals) A type that implements the
InputStream
trait. Exported under theinternals
feature only. - (internals) A chain of module names to namespace-qualify a variable or function call. Exported under the
internals
feature only. - Context of a native Rust function call.
- (internals) An op-assignment operator. Exported under the
internals
feature only. - A type that holds a configuration option with bit-flags. Exported under the
internals
feature only. - Error when parsing a script.
- (internals) A type that encapsulates the current state of the parser. Exported under the
internals
feature only. - A location (line number + character position) in the input script.
- Type containing information about the current scope. Useful for keeping state between
Engine
evaluation runs. - (internals) A type containing information on a scripted function. Exported under the
internals
feature only. - A type containing the metadata of a script-defined function.
- (internals) A scoped block of statements. Exported under the
internals
feature only. - (internals) An iterator on a
Token
stream. Exported under theinternals
feature only. - (internals) State of the tokenizer. Exported under the
internals
feature only. - (internals) A type containing commands to control the tokenizer.
Enums§
- (internals) Modes of access. Exported under the
internals
feature only. - Evaluation result.
- (internals) An expression sub-tree. Exported under the
internals
feature only. - A type representing the access mode of a function.
- A type representing the namespace of a function.
- (internals) Error encountered when tokenizing the script text. Exported under the
internals
feature only. - Level of optimization performed.
- Type of error encountered when parsing a script.
- (internals) A statement. Exported under the
internals
feature only. - (internals) A Rhai language token. Exported under the
internals
feature only.
Constants§
- (internals) The
AST
node breaks out of normal control flow. Exported under theinternals
feature only. - (internals) The
AST
node is constant. Exported under theinternals
feature only. - (internals) The
AST
node is in negated mode. Exported under theinternals
feature only. - (internals) No options for the
AST
node. Exported under theinternals
feature only. - (internals) The
AST
node is public. Exported under theinternals
feature only. - Standard method function for containment testing. The
in
operator is implemented as a call to this method. - Standard equality comparison operator.
- Standard exclusive range operator.
- Standard inclusive range operator.
Traits§
- Trait to create a Rust closure from a script.
- Trait that parses arguments to a function call.
- (internals) Trait that encapsulates a peekable character input stream. Exported under the
internals
feature only. - Trait that encapsulates a module resolution service.
- Trait to register custom Rust functions.
- (internals) Trait to represent any type. Exported under the
internals
feature only.
Functions§
- (internals) Get the next token from the
stream
. Exported under theinternals
feature only. - (internals) Parse a string literal ended by
termination_char
. Exported under theinternals
feature only.
Type Aliases§
- Variable-sized array of
Dynamic
values. Not available underno_index
. - Variable-sized array of
u8
values (byte array). Not available underno_index
. - The system floating-point type. It is defined as
f64
. Not available underno_float
. - (internals) A function resolution cache. Exported under the
internals
feature only. - The system integer type. It is defined as
i64
. - An identifier in Rhai.
SmartString
is used because most identifiers are ASCII and short, fewer than 23 characters, so they can be stored inline. - (internals) Alias to
smallvec::SmallVec<[T; 3]>
, which is aVec
backed by a small, inline, fixed-size array when there are ≤ 3 items stored. Exported under theinternals
feature only. - (internals) A shared object that allows control of the tokenizer from outside.
Attribute Macros§
- Attribute, when put on a Rust function, turns it into a plugin function.
- Attribute, when put on a Rust module, turns it into a plugin module.