rhai/examples/arrays_and_structs.rs

30 lines
629 B
Rust
Raw Normal View History

2016-04-14 03:40:06 +02:00
extern crate rhai;
2017-12-20 16:39:03 +01:00
use rhai::{Engine, RegisterFn};
2016-04-14 03:40:06 +02:00
#[derive(Clone, Debug)]
struct TestStruct {
2017-12-20 12:16:14 +01:00
x: i64,
2016-04-14 03:40:06 +02:00
}
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);
2017-12-21 12:28:59 +01:00
println!("{:?}", engine.eval::<TestStruct>("let x = new_ts(); x.update(); x"));
println!("{:?}", engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]"));
2016-04-14 03:40:06 +02:00
}