rhai/examples/arrays_and_structs.rs

35 lines
648 B
Rust
Raw Normal View History

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;
}
2020-03-19 06:52:10 +01:00
fn new() -> Self {
2016-04-14 03:40:06 +02:00
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);
2019-09-18 12:21:07 +02: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
}