rhai/examples/custom_types_and_methods.rs

31 lines
595 B
Rust
Raw Normal View History

extern crate rhai;
use rhai::{Engine, FnRegister};
#[derive(Clone)]
struct TestStruct {
x: i32
}
impl TestStruct {
fn update(&mut self) {
self.x += 1000;
}
fn new() -> TestStruct {
TestStruct { x: 1 }
}
}
fn main() {
let mut engine = Engine::new();
engine.register_type::<TestStruct>();
engine.register_fn("update", TestStruct::update);
engine.register_fn("new_ts", TestStruct::new);
2016-03-16 23:32:05 +01:00
if let Ok(result) = engine.eval::<TestStruct>("var x = new_ts(); x.update(); x") {
println!("result: {}", result.x); // prints 1001
}
}