rhai/examples/arrays_and_structs.rs

42 lines
860 B
Rust
Raw Normal View History

2020-12-26 16:21:16 +01:00
use rhai::{Engine, EvalAltResult, RegisterFn, INT};
2016-04-14 03:40:06 +02:00
2020-12-26 08:41:41 +01:00
#[derive(Debug, Clone)]
2016-04-14 03:40:06 +02:00
struct TestStruct {
2020-03-24 02:49:08 +01:00
x: INT,
2016-04-14 03:40:06 +02:00
}
impl TestStruct {
2020-12-26 08:41:41 +01:00
pub fn update(&mut self) {
2016-04-14 03:40:06 +02:00
self.x += 1000;
}
2020-12-26 08:41:41 +01:00
pub fn new() -> Self {
2020-10-19 08:26:15 +02:00
Self { x: 1 }
2016-04-14 03:40:06 +02:00
}
}
#[cfg(not(feature = "no_index"))]
#[cfg(not(feature = "no_object"))]
2020-12-26 08:41:41 +01:00
fn main() -> Result<(), Box<EvalAltResult>> {
2016-04-14 03:40:06 +02:00
let mut engine = Engine::new();
2020-10-07 09:55:45 +02:00
engine
.register_type::<TestStruct>()
2020-12-26 08:41:41 +01:00
.register_fn("new_ts", TestStruct::new)
.register_fn("update", TestStruct::update);
2016-04-14 03:40:06 +02:00
2019-09-18 12:21:07 +02:00
println!(
"{:?}",
2020-12-26 08:41:41 +01:00
engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?
2019-09-18 12:21:07 +02:00
);
println!(
"{:?}",
2020-12-26 08:41:41 +01:00
engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]")?
2019-09-18 12:21:07 +02:00
);
2020-12-26 08:41:41 +01:00
Ok(())
2016-04-14 03:40:06 +02:00
}
#[cfg(any(feature = "no_index", feature = "no_object"))]
fn main() {}