rhai/examples/arrays_and_structs.rs

40 lines
789 B
Rust
Raw Normal View History

2020-03-24 09:49:08 +08:00
use rhai::{Engine, RegisterFn, INT};
2016-04-13 18:40:06 -07:00
#[derive(Clone, Debug)]
struct TestStruct {
2020-03-24 09:49:08 +08:00
x: INT,
2016-04-13 18:40:06 -07:00
}
impl TestStruct {
fn update(&mut self) {
self.x += 1000;
}
2020-03-19 13:52:10 +08:00
fn new() -> Self {
2020-10-19 14:26:15 +08:00
Self { x: 1 }
2016-04-13 18:40:06 -07:00
}
}
#[cfg(not(feature = "no_index"))]
#[cfg(not(feature = "no_object"))]
2016-04-13 18:40:06 -07:00
fn main() {
let mut engine = Engine::new();
2020-10-07 15:55:45 +08:00
engine
.register_type::<TestStruct>()
.register_fn("update", TestStruct::update)
.register_fn("new_ts", TestStruct::new);
2016-04-13 18:40:06 -07:00
2019-09-18 11:21:07 +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-13 18:40:06 -07:00
}
#[cfg(any(feature = "no_index", feature = "no_object"))]
fn main() {}