From 21567b7518c98bdfa5011f175dc99078c7fddf33 Mon Sep 17 00:00:00 2001 From: jonathandturner Date: Thu, 10 Mar 2016 16:25:03 -0500 Subject: [PATCH] Add member/method example --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/README.md b/README.md index b2c6e4de..54565536 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,13 @@ fn add(x, y) { print(add(2, 3)) ``` +## Members and methods + +```Rust +var a = new_ts(); +a.x = 500; +a.update(); +``` # Example: Hello world @@ -218,6 +225,45 @@ if let Ok(result) = engine.eval("var x = new_ts(); x.update(); x".to_string()).u } ``` +# Example: working with custom types and members + +Similarly, you can work with members of your custom types. This works by registering a 'get' or a 'set' function for working with your struct. + +For example: + +```Rust +#[derive(Clone)] +struct TestStruct { + x: i32 +} + +impl TestStruct { + fn get_x(&mut self) -> i32 { + self.x + } + + fn set_x(&mut self, new_x: i32) { + self.x = new_x; + } + + fn new() -> TestStruct { + TestStruct { x: 1 } + } +} + +let mut engine = Engine::new(); + +engine.register_type::(); + +&(TestStruct::get_x as fn(&mut TestStruct)->i32).register(&mut engine, "get$x"); +&(TestStruct::set_x as fn(&mut TestStruct, i32)->()).register(&mut engine, "set$x"); +&(TestStruct::new as fn()->TestStruct).register(&mut engine, "new_ts"); + +if let Ok(result) = engine.eval("var a = new_ts(); a.x = 500; a.x".to_string()).unwrap().downcast::() { + println!("result: {}", result); +} +``` + # Example: Maintaining state By default, Rhai treats each engine invocation as a fresh one, persisting only the functions that have been defined but no top-level state. This gives each one a fairly clean starting place. Sometimes, though, you want to continue using the same top-level state from one invocation to the next. @@ -239,3 +285,4 @@ fn main() { } } ``` +