rhai/examples/arrays_and_structs.rs

54 lines
1.0 KiB
Rust
Raw Normal View History

use rhai::{Engine, EvalAltResult};
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 {
x: i64,
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
2021-02-20 16:46:25 +01:00
let result = engine.eval::<TestStruct>(
2021-04-20 06:01:35 +02:00
"
2021-02-20 16:46:25 +01:00
let x = new_ts();
x.update();
x
",
)?;
println!("{:?}", result);
let result = engine.eval::<TestStruct>(
2021-04-20 06:01:35 +02:00
"
2021-02-20 16:46:25 +01:00
let x = [ new_ts() ];
x[0].update();
x[0]
",
)?;
println!("{:?}", result);
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"))]
2021-02-20 16:46:25 +01:00
fn main() {
panic!("This example does not run under 'no_index' or 'no_object'.")
}