Improve examples.
This commit is contained in:
parent
d1a97be9e3
commit
775bd6fb35
@ -1,29 +1,41 @@
|
|||||||
use rhai::{Engine, EvalAltResult};
|
use rhai::{Engine, EvalAltResult};
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_index"))]
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
|
fn main() -> Result<(), Box<EvalAltResult>> {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct TestStruct {
|
struct TestStruct {
|
||||||
x: i64,
|
x: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestStruct {
|
impl TestStruct {
|
||||||
pub fn update(&mut self) {
|
|
||||||
self.x += 1000;
|
|
||||||
}
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { x: 1 }
|
Self { x: 1 }
|
||||||
}
|
}
|
||||||
|
pub fn update(&mut self) {
|
||||||
|
self.x += 1000;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_index"))]
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
fn main() -> Result<(), Box<EvalAltResult>> {
|
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
engine
|
engine
|
||||||
.register_type::<TestStruct>()
|
.register_type_with_name::<TestStruct>("TestStruct")
|
||||||
.register_fn("new_ts", TestStruct::new)
|
.register_fn("new_ts", TestStruct::new)
|
||||||
.register_fn("update", TestStruct::update);
|
.register_fn("update", TestStruct::update);
|
||||||
|
|
||||||
|
#[cfg(feature = "metadata")]
|
||||||
|
{
|
||||||
|
println!("Functions registered:");
|
||||||
|
|
||||||
|
engine
|
||||||
|
.gen_fn_signatures(false)
|
||||||
|
.into_iter()
|
||||||
|
.for_each(|func| println!("{}", func));
|
||||||
|
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
|
||||||
let result = engine.eval::<TestStruct>(
|
let result = engine.eval::<TestStruct>(
|
||||||
"
|
"
|
||||||
let x = new_ts();
|
let x = new_ts();
|
||||||
|
@ -1,38 +1,61 @@
|
|||||||
use rhai::{Engine, EvalAltResult};
|
use rhai::{Engine, EvalAltResult};
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_object"))]
|
||||||
|
fn main() -> Result<(), Box<EvalAltResult>> {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct TestStruct {
|
struct TestStruct {
|
||||||
x: i64,
|
x: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestStruct {
|
impl TestStruct {
|
||||||
pub fn update(&mut self) {
|
|
||||||
self.x += 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { x: 1 }
|
Self { x: 1 }
|
||||||
}
|
}
|
||||||
|
pub fn update(&mut self) {
|
||||||
|
self.x += 1000;
|
||||||
|
}
|
||||||
|
pub fn calculate(&mut self, data: i64) -> i64 {
|
||||||
|
self.x * data
|
||||||
|
}
|
||||||
|
pub fn get_x(&mut self) -> i64 {
|
||||||
|
self.x
|
||||||
|
}
|
||||||
|
pub fn set_x(&mut self, value: i64) {
|
||||||
|
self.x = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
fn main() -> Result<(), Box<EvalAltResult>> {
|
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
engine
|
engine
|
||||||
.register_type::<TestStruct>()
|
.register_type_with_name::<TestStruct>("TestStruct")
|
||||||
.register_fn("new_ts", TestStruct::new)
|
.register_fn("new_ts", TestStruct::new)
|
||||||
.register_fn("update", TestStruct::update);
|
.register_fn("update", TestStruct::update)
|
||||||
|
.register_fn("calc", TestStruct::calculate)
|
||||||
|
.register_get_set("x", TestStruct::get_x, TestStruct::set_x);
|
||||||
|
|
||||||
let result = engine.eval::<TestStruct>(
|
#[cfg(feature = "metadata")]
|
||||||
|
{
|
||||||
|
println!("Functions registered:");
|
||||||
|
|
||||||
|
engine
|
||||||
|
.gen_fn_signatures(false)
|
||||||
|
.into_iter()
|
||||||
|
.for_each(|func| println!("{}", func));
|
||||||
|
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = engine.eval::<i64>(
|
||||||
"
|
"
|
||||||
let x = new_ts();
|
let x = new_ts();
|
||||||
|
x.x = 42;
|
||||||
x.update();
|
x.update();
|
||||||
x
|
x.calc(x.x)
|
||||||
",
|
",
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
println!("result: {}", result.x); // prints 1001
|
println!("result: {}", result); // prints 1085764
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ fn main() -> Result<(), Box<EvalAltResult>> {
|
|||||||
|
|
||||||
let result = engine.eval::<i64>("40 + 2")?;
|
let result = engine.eval::<i64>("40 + 2")?;
|
||||||
|
|
||||||
println!("Answer: {}", result); // prints 42
|
println!("The Answer: {}", result); // prints 42
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user