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

View File

@ -1,16 +1,16 @@
use rhai::{Engine, EvalAltResult, RegisterFn, INT}; use rhai::{Engine, EvalAltResult, RegisterFn, INT};
#[derive(Clone)] #[derive(Debug, Clone)]
struct TestStruct { struct TestStruct {
x: INT, x: INT,
} }
impl TestStruct { impl TestStruct {
fn update(&mut self) { pub fn update(&mut self) {
self.x += 1000; self.x += 1000;
} }
fn new() -> Self { pub fn new() -> Self {
Self { x: 1 } Self { x: 1 }
} }
} }
@ -21,8 +21,8 @@ fn main() -> Result<(), Box<EvalAltResult>> {
engine engine
.register_type::<TestStruct>() .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);
let result = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?; let result = engine.eval::<TestStruct>("let x = new_ts(); x.update(); x")?;

View File

@ -67,7 +67,7 @@ fn main() {
print_help(); print_help();
'main_loop: loop { 'main_loop: loop {
print!("rhai> "); print!("rhai-repl> ");
stdout().flush().expect("couldn't flush stdout"); stdout().flush().expect("couldn't flush stdout");
input.clear(); input.clear();
@ -126,12 +126,12 @@ fn main() {
} }
"astu" => { "astu" => {
// print the last un-optimized AST // print the last un-optimized AST
println!("{:#?}\n", &ast_u); println!("{:#?}\n", ast_u);
continue; continue;
} }
"ast" => { "ast" => {
// print the last AST // print the last AST
println!("{:#?}\n", &ast); println!("{:#?}\n", ast);
continue; continue;
} }
"functions" => { "functions" => {

View File

@ -17,13 +17,13 @@ mod example {
use rhai::{Dynamic, Engine, Map}; use rhai::{Dynamic, Engine, Map};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
struct Point { struct Point {
x: f64, x: f64,
y: f64, y: f64,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
struct MyStruct { struct MyStruct {
a: i64, a: i64,
b: Vec<String>, b: Vec<String>,
@ -71,6 +71,18 @@ mod example {
// Convert the 'Dynamic' object map into 'MyStruct' // Convert the 'Dynamic' object map into 'MyStruct'
let x: MyStruct = from_dynamic(&result).unwrap(); let x: MyStruct = from_dynamic(&result).unwrap();
assert_eq!(
x,
MyStruct {
a: 42,
b: vec!["hello".into(), "world".into()],
c: true,
d: Point {
x: 123.456,
y: 999.0,
},
}
);
println!("Deserialized to struct: {:#?}", x); println!("Deserialized to struct: {:#?}", x);
} }
} }

View File

@ -69,7 +69,7 @@ fn main() -> Result<(), Box<EvalAltResult>> {
display("Trimmed", x); display("Trimmed", x);
display("Trimmed Length", x.len()); display("Trimmed Length", x.len());
display("Index of \"!!!\"", x.index_of("!!!")); display("Index of \"!!!\"", x.index_of("!!!"));
"#, "#,
)?; )?;
println!(); println!();