Trait rhai::CustomType
source · pub trait CustomType: Variant + Clone {
// Required method
fn build(builder: TypeBuilder<'_, Self>);
}
Expand description
Trait to build the API of a custom type for use with an Engine
(i.e. register the type and its getters, setters, methods, etc.).
Example
use rhai::{CustomType, TypeBuilder, Engine};
#[derive(Debug, Clone, Eq, PartialEq)]
struct TestStruct {
field: i64
}
impl TestStruct {
fn new() -> Self {
Self { field: 1 }
}
fn update(&mut self, offset: i64) {
self.field += offset;
}
fn get_value(&mut self) -> i64 {
self.field
}
fn set_value(&mut self, value: i64) {
self.field = value;
}
}
impl CustomType for TestStruct {
fn build(mut builder: TypeBuilder<Self>) {
builder
.with_name("TestStruct")
.with_fn("new_ts", Self::new)
.with_fn("update", Self::update);
}
}
let mut engine = Engine::new();
// Register API for the custom type.
engine.build_type::<TestStruct>();
assert_eq!(
engine.eval::<TestStruct>("let x = new_ts(); x.update(41); x")?,
TestStruct { field: 42 }
);
Required Methods§
sourcefn build(builder: TypeBuilder<'_, Self>)
fn build(builder: TypeBuilder<'_, Self>)
Builds the custom type for use with the Engine
.
Methods, property getters/setters, indexers etc. should be registered in this function.