rhai/examples/arrays_and_structs.rs

40 lines
795 B
Rust
Raw Normal View History

2020-03-24 02:49:08 +01:00
use rhai::{Engine, RegisterFn, INT};
2016-04-14 03:40:06 +02:00
#[derive(Clone, Debug)]
struct TestStruct {
2020-03-24 02:49:08 +01:00
x: INT,
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 }
}
}
#[cfg(not(feature = "no_index"))]
#[cfg(not(feature = "no_object"))]
2016-04-14 03:40:06 +02:00
fn main() {
let mut engine = Engine::new();
2020-10-07 09:55:45 +02:00
engine
.register_type::<TestStruct>()
.register_fn("update", TestStruct::update)
.register_fn("new_ts", TestStruct::new);
2016-04-14 03:40:06 +02:00
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
}
#[cfg(any(feature = "no_index", feature = "no_object"))]
fn main() {}