[][src]Struct rhai::Module

pub struct Module { /* fields omitted */ }

An imported module, which may contain variables, sub-modules, external Rust functions, and script-defined functions.

Not available under the no_module feature.

Implementations

impl Module[src]

pub fn new() -> Self[src]

Create a new module.

Examples

use rhai::Module;

let mut module = Module::new();
module.set_var("answer", 42_i64);
assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42);

pub fn new_with_capacity(capacity: usize) -> Self[src]

Create a new module with a specified capacity for native Rust functions.

Examples

use rhai::Module;

let mut module = Module::new();
module.set_var("answer", 42_i64);
assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42);

pub fn contains_var(&self, name: &str) -> bool[src]

Does a variable exist in the module?

Examples

use rhai::Module;

let mut module = Module::new();
module.set_var("answer", 42_i64);
assert!(module.contains_var("answer"));

pub fn get_var_value<T: Variant + Clone>(&self, name: &str) -> Option<T>[src]

Get the value of a module variable.

Examples

use rhai::Module;

let mut module = Module::new();
module.set_var("answer", 42_i64);
assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42);

pub fn get_var(&self, name: &str) -> Option<Dynamic>[src]

Get a module variable as a Dynamic.

Examples

use rhai::Module;

let mut module = Module::new();
module.set_var("answer", 42_i64);
assert_eq!(module.get_var("answer").unwrap().cast::<i64>(), 42);

pub fn set_var(&mut self, name: impl Into<String>, value: impl Variant + Clone)[src]

Set a variable into the module.

If there is an existing variable of the same name, it is replaced.

Examples

use rhai::Module;

let mut module = Module::new();
module.set_var("answer", 42_i64);
assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42);

pub fn contains_sub_module(&self, name: &str) -> bool[src]

Does a sub-module exist in the module?

Examples

use rhai::Module;

let mut module = Module::new();
let sub_module = Module::new();
module.set_sub_module("question", sub_module);
assert!(module.contains_sub_module("question"));

pub fn get_sub_module(&self, name: &str) -> Option<&Module>[src]

Get a sub-module.

Examples

use rhai::Module;

let mut module = Module::new();
let sub_module = Module::new();
module.set_sub_module("question", sub_module);
assert!(module.get_sub_module("question").is_some());

pub fn get_sub_module_mut(&mut self, name: &str) -> Option<&mut Module>[src]

Get a mutable reference to a sub-module.

Examples

use rhai::Module;

let mut module = Module::new();
let sub_module = Module::new();
module.set_sub_module("question", sub_module);
assert!(module.get_sub_module_mut("question").is_some());

pub fn set_sub_module(&mut self, name: impl Into<String>, sub_module: Module)[src]

Set a sub-module into the module.

If there is an existing sub-module of the same name, it is replaced.

Examples

use rhai::Module;

let mut module = Module::new();
let sub_module = Module::new();
module.set_sub_module("question", sub_module);
assert!(module.get_sub_module("question").is_some());

pub fn contains_fn(&self, hash_fn: u64) -> bool[src]

Does the particular Rust function exist in the module?

The u64 hash is calculated by the function crate::calc_fn_hash. It is also returned by the set_fn_XXX calls.

Examples

use rhai::Module;

let mut module = Module::new();
let hash = module.set_fn_0("calc", || Ok(42_i64));
assert!(module.contains_fn(hash));

pub fn set_fn(
    &mut self,
    name: impl Into<String>,
    access: FnAccess,
    params: &[TypeId],
    func: CallableFunction
) -> u64
[src]

Set a Rust function into the module, returning a hash key.

If there is an existing Rust function of the same hash, it is replaced.

pub fn set_fn_0<T: Variant + Clone>(
    &mut self,
    name: impl Into<String>,
    func: impl Fn() -> Result<T, Box<EvalAltResult>> + 'static
) -> u64
[src]

Set a Rust function taking no parameters into the module, returning a hash key.

If there is a similar existing Rust function, it is replaced.

Examples

use rhai::Module;

let mut module = Module::new();
let hash = module.set_fn_0("calc", || Ok(42_i64));
assert!(module.get_fn(hash).is_some());

pub fn set_fn_1<A: Variant + Clone, T: Variant + Clone>(
    &mut self,
    name: impl Into<String>,
    func: impl Fn(A) -> Result<T, Box<EvalAltResult>> + 'static
) -> u64
[src]

Set a Rust function taking one parameter into the module, returning a hash key.

If there is a similar existing Rust function, it is replaced.

Examples

use rhai::Module;

let mut module = Module::new();
let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1));
assert!(module.get_fn(hash).is_some());

pub fn set_fn_1_mut<A: Variant + Clone, T: Variant + Clone>(
    &mut self,
    name: impl Into<String>,
    func: impl Fn(&mut A) -> Result<T, Box<EvalAltResult>> + 'static
) -> u64
[src]

Set a Rust function taking one mutable parameter into the module, returning a hash key.

If there is a similar existing Rust function, it is replaced.

Examples

use rhai::Module;

let mut module = Module::new();
let hash = module.set_fn_1_mut("calc", |x: &mut i64| { *x += 1; Ok(*x) });
assert!(module.get_fn(hash).is_some());

pub fn set_getter_fn<A: Variant + Clone, T: Variant + Clone>(
    &mut self,
    name: impl Into<String>,
    func: impl Fn(&mut A) -> Result<T, Box<EvalAltResult>> + 'static
) -> u64
[src]

Set a Rust getter function taking one mutable parameter, returning a hash key.

If there is a similar existing Rust getter function, it is replaced.

Examples

use rhai::Module;

let mut module = Module::new();
let hash = module.set_getter_fn("value", |x: &mut i64| { Ok(*x) });
assert!(module.get_fn(hash).is_some());

pub fn set_fn_2<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>(
    &mut self,
    name: impl Into<String>,
    func: impl Fn(A, B) -> Result<T, Box<EvalAltResult>> + 'static
) -> u64
[src]

Set a Rust function taking two parameters into the module, returning a hash key.

If there is a similar existing Rust function, it is replaced.

Examples

use rhai::{Module, ImmutableString};

let mut module = Module::new();
let hash = module.set_fn_2("calc", |x: i64, y: ImmutableString| {
    Ok(x + y.len() as i64)
});
assert!(module.get_fn(hash).is_some());

pub fn set_fn_2_mut<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>(
    &mut self,
    name: impl Into<String>,
    func: impl Fn(&mut A, B) -> Result<T, Box<EvalAltResult>> + 'static
) -> u64
[src]

Set a Rust function taking two parameters (the first one mutable) into the module, returning a hash key.

If there is a similar existing Rust function, it is replaced.

Examples

use rhai::{Module, ImmutableString};

let mut module = Module::new();
let hash = module.set_fn_2_mut("calc", |x: &mut i64, y: ImmutableString| {
    *x += y.len() as i64; Ok(*x)
});
assert!(module.get_fn(hash).is_some());

pub fn set_setter_fn<A: Variant + Clone, B: Variant + Clone>(
    &mut self,
    name: impl Into<String>,
    func: impl Fn(&mut A, B) -> Result<(), Box<EvalAltResult>> + 'static
) -> u64
[src]

Set a Rust setter function taking two parameters (the first one mutable) into the module, returning a hash key.

If there is a similar existing setter Rust function, it is replaced.

Examples

use rhai::{Module, ImmutableString};

let mut module = Module::new();
let hash = module.set_setter_fn("value", |x: &mut i64, y: ImmutableString| {
    *x = y.len() as i64;
    Ok(())
});
assert!(module.get_fn(hash).is_some());

pub fn set_indexer_fn<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>(
    &mut self,
    func: impl Fn(&mut A, B) -> Result<T, Box<EvalAltResult>> + 'static
) -> u64
[src]

Set a Rust indexer function taking two parameters (the first one mutable) into the module, returning a hash key.

If there is a similar existing setter Rust function, it is replaced.

Examples

use rhai::{Module, ImmutableString};

let mut module = Module::new();
let hash = module.set_indexer_fn(|x: &mut i64, y: ImmutableString| {
    Ok(*x + y.len() as i64)
});
assert!(module.get_fn(hash).is_some());

pub fn set_fn_3<A: Variant + Clone, B: Variant + Clone, C: Variant + Clone, T: Variant + Clone>(
    &mut self,
    name: impl Into<String>,
    func: impl Fn(A, B, C) -> Result<T, Box<EvalAltResult>> + 'static
) -> u64
[src]

Set a Rust function taking three parameters into the module, returning a hash key.

If there is a similar existing Rust function, it is replaced.

Examples

use rhai::{Module, ImmutableString};

let mut module = Module::new();
let hash = module.set_fn_3("calc", |x: i64, y: ImmutableString, z: i64| {
    Ok(x + y.len() as i64 + z)
});
assert!(module.get_fn(hash).is_some());

pub fn set_fn_3_mut<A: Variant + Clone, B: Variant + Clone, C: Variant + Clone, T: Variant + Clone>(
    &mut self,
    name: impl Into<String>,
    func: impl Fn(&mut A, B, C) -> Result<T, Box<EvalAltResult>> + 'static
) -> u64
[src]

Set a Rust function taking three parameters (the first one mutable) into the module, returning a hash key.

If there is a similar existing Rust function, it is replaced.

Examples

use rhai::{Module, ImmutableString};

let mut module = Module::new();
let hash = module.set_fn_3_mut("calc", |x: &mut i64, y: ImmutableString, z: i64| {
    *x += y.len() as i64 + z; Ok(*x)
});
assert!(module.get_fn(hash).is_some());

pub fn set_fn_4<A: Variant + Clone, B: Variant + Clone, C: Variant + Clone, D: Variant + Clone, T: Variant + Clone>(
    &mut self,
    name: impl Into<String>,
    func: impl Fn(A, B, C, D) -> Result<T, Box<EvalAltResult>> + 'static
) -> u64
[src]

Set a Rust function taking four parameters into the module, returning a hash key.

If there is a similar existing Rust function, it is replaced.

Examples

use rhai::{Module, ImmutableString};

let mut module = Module::new();
let hash = module.set_fn_4("calc", |x: i64, y: ImmutableString, z: i64, _w: ()| {
    Ok(x + y.len() as i64 + z)
});
assert!(module.get_fn(hash).is_some());

pub fn set_fn_4_mut<A: Variant + Clone, B: Variant + Clone, C: Variant + Clone, D: Variant + Clone, T: Variant + Clone>(
    &mut self,
    name: impl Into<String>,
    func: impl Fn(&mut A, B, C, D) -> Result<T, Box<EvalAltResult>> + 'static
) -> u64
[src]

Set a Rust function taking four parameters (the first one mutable) into the module, returning a hash key.

If there is a similar existing Rust function, it is replaced.

Examples

use rhai::{Module, ImmutableString};

let mut module = Module::new();
let hash = module.set_fn_4_mut("calc", |x: &mut i64, y: ImmutableString, z: i64, _w: ()| {
    *x += y.len() as i64 + z; Ok(*x)
});
assert!(module.get_fn(hash).is_some());

pub fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction>[src]

Get a Rust function.

The u64 hash is calculated by the function crate::calc_fn_hash. It is also returned by the set_fn_XXX calls.

Examples

use rhai::Module;

let mut module = Module::new();
let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1));
assert!(module.get_fn(hash).is_some());

pub fn eval_ast_as_new(
    scope: Scope,
    ast: &AST,
    engine: &Engine
) -> Result<Self, Box<EvalAltResult>>
[src]

Create a new Module by evaluating an AST.

Examples

use rhai::{Engine, Module, Scope};

let engine = Engine::new();
let ast = engine.compile("let answer = 42; export answer;")?;
let module = Module::eval_ast_as_new(Scope::new(), &ast, &engine)?;
assert!(module.contains_var("answer"));
assert_eq!(module.get_var_value::<i64>("answer").unwrap(), 42);

pub fn contains_iter(&self, id: TypeId) -> bool[src]

Does a type iterator exist in the module?

pub fn set_iter(
    &mut self,
    typ: TypeId,
    func: fn(_: Dynamic) -> Box<dyn Iterator<Item = Dynamic>>
)
[src]

Set a type iterator into the module.

pub fn get_iter(
    &self,
    id: TypeId
) -> Option<fn(_: Dynamic) -> Box<dyn Iterator<Item = Dynamic>>>
[src]

Get the specified type iterator.

Trait Implementations

impl Clone for Module[src]

impl Debug for Module[src]

impl Default for Module[src]

Auto Trait Implementations

impl !RefUnwindSafe for Module

impl !Send for Module

impl !Sync for Module

impl Unpin for Module

impl !UnwindSafe for Module

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.