From 5eaa2a3d7eebef1699bced02564233cda4ebec94 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Tue, 9 Aug 2022 10:36:33 +0200 Subject: [PATCH 1/3] add example --- examples/README.md | 15 +++----- examples/custom_types.rs | 75 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 examples/custom_types.rs diff --git a/examples/README.md b/examples/README.md index 17c379fe..1a9aa392 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,14 +1,13 @@ -Sample Applications -=================== +# Sample Applications -Standard Examples ------------------ +## Standard Examples | Example | Description | | --------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | | [`arrays_and_structs`](arrays_and_structs.rs) | shows how to register a Rust type and using it with arrays | | [`callback`](callback.rs) | shows how to store a Rhai closure and call it later within Rust | | [`custom_types_and_methods`](custom_types_and_methods.rs) | shows how to register a Rust type and methods/getters/setters for it | +| [`custom_types`](custom_types.rs) | shows how to register a Rust type and methods/getters/setters using the `CustomType` trait. | | [`definitions`](./definitions) | shows how to generate definition files for use with the [Rhai Language Server](https://github.com/rhaiscript/lsp) (requires the `metadata` feature) | | [`hello`](hello.rs) | simple example that evaluates an expression and prints the result | | [`reuse_scope`](reuse_scope.rs) | evaluates two pieces of code in separate runs, but using a common `Scope` | @@ -17,9 +16,7 @@ Standard Examples | [`strings`](strings.rs) | shows different ways to register Rust functions taking string arguments | | [`threading`](threading.rs) | shows how to communicate with an `Engine` running in a separate thread via an MPSC channel | - -Scriptable Event Handler With State Examples -------------------------------------------- +## Scriptable Event Handler With State Examples Because of its popularity, included are sample implementations for the pattern [_Scriptable Event Handler With State_](https://rhai.rs/book/patterns/events.html) in different styles. @@ -30,9 +27,7 @@ Because of its popularity, included are sample implementations for the pattern | [`event_handler_js`](event_handler_js) | [`event_handler_js/script.rhai`](event_handler_js/script.rhai) | [_JS Style_](https://rhai.rs/book/patterns/events-2.html) | | [`event_handler_map`](event_handler_map) | [`event_handler_map/script.rhai`](event_handler_map/script.rhai) | [_Map Style_](https://rhai.rs/book/patterns/events-3.html) | - -Running Examples ----------------- +## Running Examples Examples can be run with the following command: diff --git a/examples/custom_types.rs b/examples/custom_types.rs new file mode 100644 index 00000000..86888c8f --- /dev/null +++ b/examples/custom_types.rs @@ -0,0 +1,75 @@ +//! An example showing how to register a Rust type and methods/getters/setters using the `CustomType` trait. + +#[cfg(feature = "no_object")] +fn main() { + panic!("This example does not run under 'no_object'."); +} + +use rhai::{CustomType, Engine, EvalAltResult, TypeBuilder}; + +#[cfg(not(feature = "no_object"))] +fn main() -> Result<(), Box> { + #[derive(Debug, Clone)] + struct TestStruct { + x: i64, + } + + impl TestStruct { + pub fn new() -> Self { + 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; + } + } + + impl CustomType for TestStruct { + fn build(mut builder: TypeBuilder) { + #[allow(deprecated)] // The TypeBuilder api is volatile. + builder + .with_name("TestStruct") + .with_fn("new_ts", Self::new) + .with_fn("update", Self::update) + .with_fn("calc", Self::calculate) + .with_get_set("x", Self::get_x, Self::set_x); + } + } + + let mut engine = Engine::new(); + + engine.build_type::(); + + #[cfg(feature = "metadata")] + { + println!("Functions registered:"); + + engine + .gen_fn_signatures(false) + .into_iter() + .for_each(|func| println!("{}", func)); + + println!(); + } + + let result = engine.eval::( + " + let x = new_ts(); + x.x = 42; + x.update(); + x.calc(x.x) + ", + )?; + + println!("result: {}", result); // prints 1085764 + + Ok(()) +} From 217ca799305bdd0df023485e25509a3ea938a634 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Tue, 9 Aug 2022 10:36:58 +0200 Subject: [PATCH 2/3] rm volatile note on Engine::build_type --- src/api/build_type.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/api/build_type.rs b/src/api/build_type.rs index 98cc8645..f442af56 100644 --- a/src/api/build_type.rs +++ b/src/api/build_type.rs @@ -76,7 +76,6 @@ impl Engine { /// i.e. register the type and its getters, setters, methods, etc... /// /// See [`CustomType`]. - #[deprecated = "This API is NOT deprecated, but it is considered volatile and may change in the future."] #[inline] pub fn build_type(&mut self) -> &mut Self where From 35c2caab07e6dda3e2b7ea8443036e8b50ec7b15 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Tue, 9 Aug 2022 10:47:53 +0200 Subject: [PATCH 3/3] fix doc --- src/api/build_type.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/api/build_type.rs b/src/api/build_type.rs index f442af56..cf44e51e 100644 --- a/src/api/build_type.rs +++ b/src/api/build_type.rs @@ -38,8 +38,7 @@ use crate::{ /// builder /// .with_name("TestStruct") /// .with_fn("new_ts", Self::new) -/// .with_fn("update", Self::update) -/// .with_get_set("value", Self::get_value, Self::set_value); +/// .with_fn("update", Self::update); /// } /// } /// @@ -57,11 +56,6 @@ use crate::{ /// TestStruct { field: 42 } /// ); /// -/// # #[cfg(not(feature = "no_object"))] -/// assert_eq!( -/// engine.eval::("let x = new_ts(); x.value = 5 + x.value; x")?, -/// TestStruct { field: 6 } -/// ); /// # Ok(()) /// # } /// ```