Refine examples.

This commit is contained in:
Stephen Chung
2020-12-26 15:41:41 +08:00
parent dc4e52e795
commit db9dcd1bcc
5 changed files with 33 additions and 19 deletions

View File

@@ -1,38 +1,40 @@
use rhai::{Engine, RegisterFn, INT};
#[derive(Clone, Debug)]
#[derive(Debug, Clone)]
struct TestStruct {
x: INT,
}
impl TestStruct {
fn update(&mut self) {
pub fn update(&mut self) {
self.x += 1000;
}
fn new() -> Self {
pub fn new() -> Self {
Self { x: 1 }
}
}
#[cfg(not(feature = "no_index"))]
#[cfg(not(feature = "no_object"))]
fn main() {
fn main() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine
.register_type::<TestStruct>()
.register_fn("update", TestStruct::update)
.register_fn("new_ts", TestStruct::new);
.register_fn("new_ts", TestStruct::new)
.register_fn("update", TestStruct::update);
println!(
"{:?}",
engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")
engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?
);
println!(
"{:?}",
engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]")
engine.eval::<TestStruct>("let x = [new_ts()]; x[0].update(); x[0]")?
);
Ok(())
}
#[cfg(any(feature = "no_index", feature = "no_object"))]