From db9dcd1bcc94e4e6655cf7ecabb9e9c6cc754e81 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 26 Dec 2020 15:41:41 +0800 Subject: [PATCH] Refine examples. --- examples/arrays_and_structs.rs | 18 ++++++++++-------- examples/custom_types_and_methods.rs | 10 +++++----- examples/rhai-repl.rs | 6 +++--- examples/serde.rs | 16 ++++++++++++++-- examples/strings.rs | 2 +- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/examples/arrays_and_structs.rs b/examples/arrays_and_structs.rs index 0ad34051..75137dac 100644 --- a/examples/arrays_and_structs.rs +++ b/examples/arrays_and_structs.rs @@ -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> { let mut engine = Engine::new(); engine .register_type::() - .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::("let x = new_ts(); x.update(); x") + engine.eval::("let x = new_ts(); x.update(); x")? ); println!( "{:?}", - engine.eval::("let x = [new_ts()]; x[0].update(); x[0]") + engine.eval::("let x = [new_ts()]; x[0].update(); x[0]")? ); + + Ok(()) } #[cfg(any(feature = "no_index", feature = "no_object"))] diff --git a/examples/custom_types_and_methods.rs b/examples/custom_types_and_methods.rs index 4b0118fa..c48a7fde 100644 --- a/examples/custom_types_and_methods.rs +++ b/examples/custom_types_and_methods.rs @@ -1,16 +1,16 @@ use rhai::{Engine, EvalAltResult, RegisterFn, INT}; -#[derive(Clone)] +#[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 } } } @@ -21,8 +21,8 @@ fn main() -> Result<(), Box> { engine .register_type::() - .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::("let x = new_ts(); x.update(); x")?; diff --git a/examples/rhai-repl.rs b/examples/rhai-repl.rs index 92e04a71..6e4d2719 100644 --- a/examples/rhai-repl.rs +++ b/examples/rhai-repl.rs @@ -67,7 +67,7 @@ fn main() { print_help(); 'main_loop: loop { - print!("rhai> "); + print!("rhai-repl> "); stdout().flush().expect("couldn't flush stdout"); input.clear(); @@ -126,12 +126,12 @@ fn main() { } "astu" => { // print the last un-optimized AST - println!("{:#?}\n", &ast_u); + println!("{:#?}\n", ast_u); continue; } "ast" => { // print the last AST - println!("{:#?}\n", &ast); + println!("{:#?}\n", ast); continue; } "functions" => { diff --git a/examples/serde.rs b/examples/serde.rs index 1868f44b..eca31453 100644 --- a/examples/serde.rs +++ b/examples/serde.rs @@ -17,13 +17,13 @@ mod example { use rhai::{Dynamic, Engine, Map}; use serde::{Deserialize, Serialize}; - #[derive(Debug, Serialize, Deserialize)] + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] struct Point { x: f64, y: f64, } - #[derive(Debug, Serialize, Deserialize)] + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] struct MyStruct { a: i64, b: Vec, @@ -71,6 +71,18 @@ mod example { // Convert the 'Dynamic' object map into 'MyStruct' 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); } } diff --git a/examples/strings.rs b/examples/strings.rs index f24e9545..dc5f76b3 100644 --- a/examples/strings.rs +++ b/examples/strings.rs @@ -69,7 +69,7 @@ fn main() -> Result<(), Box> { display("Trimmed", x); display("Trimmed Length", x.len()); display("Index of \"!!!\"", x.index_of("!!!")); - "#, + "#, )?; println!();