From 283495a02fee371397146fab9c7fb5eaa55f59b1 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 22 Sep 2020 12:14:26 +0800 Subject: [PATCH] Setters should have no return value. --- RELEASES.md | 5 ++++ doc/src/rust/getters-setters.md | 4 +-- doc/src/rust/indexers.md | 4 +-- src/api.rs | 49 +++++++++++++++++---------------- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index b04aa5af..5502d0c1 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -13,6 +13,11 @@ Bug fixes * Indexers are available under `no_object`. * Registered operator-assignment functions (e.g. `+=`) now work correctly. +Breaking changes +---------------- + +* `Engine::register_set_result` and `Engine::register_indexer_set_result` now take a function that returns `Result<(), Box>`. + New features ------------ diff --git a/doc/src/rust/getters-setters.md b/doc/src/rust/getters-setters.md index 6f6184ce..ba4e8b79 100644 --- a/doc/src/rust/getters-setters.md +++ b/doc/src/rust/getters-setters.md @@ -12,10 +12,10 @@ Getters and setters are disabled when the [`no_object`] feature is used. | `Engine` API | Description | Return Value of Function | | --------------------- | ------------------------------------------------- | :-----------------------------------: | | `register_get` | Register a getter | _Any_ | -| `register_set` | Register a setter | _Any_ | +| `register_set` | Register a setter | _None_ | | `register_get_set` | Short-hand to register both a getter and a setter | _None_ | | `register_get_result` | Register a getter | `Result>` | -| `register_set_result` | Register a setter | `Result>` | +| `register_set_result` | Register a setter | `Result<(), Box>` | Examples diff --git a/doc/src/rust/indexers.md b/doc/src/rust/indexers.md index 107ed5c9..81c5cf22 100644 --- a/doc/src/rust/indexers.md +++ b/doc/src/rust/indexers.md @@ -19,10 +19,10 @@ For efficiency reasons, indexers **cannot** be used to overload (i.e. override) | `Engine` API | Description | Return Value of Function | | ----------------------------- | -------------------------------------------------------- | :-----------------------------------: | | `register_indexer_get` | Register an index getter | _Any_ | -| `register_indexer_set` | Register an index setter | _Any_ | +| `register_indexer_set` | Register an index setter | _None_ | | `register_indexer_get_set` | Short-hand to register both an index getter and a setter | _None_ | | `register_indexer_get_result` | Register an index getter | `Result>` | -| `register_indexer_set_result` | Register an index setter | `Result>` | +| `register_indexer_set_result` | Register an index setter | `Result<(), Box>` | Examples diff --git a/src/api.rs b/src/api.rs index b2cc4b31..72240dc6 100644 --- a/src/api.rs +++ b/src/api.rs @@ -191,7 +191,6 @@ impl Engine { /// /// impl TestStruct { /// fn new() -> Self { TestStruct { field: 1 } } - /// /// // Even a getter must start with `&mut self` and not `&self`. /// fn get_field(&mut self) -> i64 { self.field } /// } @@ -242,8 +241,7 @@ impl Engine { /// } /// /// impl TestStruct { - /// fn new() -> Self { TestStruct { field: 1 } } - /// + /// fn new() -> Self { TestStruct { field: 1 } } /// // Even a getter must start with `&mut self` and not `&self`. /// fn get_field(&mut self) -> Result> { /// Ok(self.field.into()) @@ -324,7 +322,7 @@ impl Engine { } /// Register a setter function for a member of a registered type with the `Engine`. - /// Returns `Result>`. + /// Returns `Result<(), Box>`. /// /// # Example /// @@ -337,10 +335,10 @@ impl Engine { /// } /// /// impl TestStruct { - /// fn new() -> Self { TestStruct { field: 1 } } - /// fn set_field(&mut self, new_val: i64) -> Result> { + /// fn new() -> Self { TestStruct { field: 1 } } + /// fn set_field(&mut self, new_val: i64) -> Result<(), Box> { /// self.field = new_val; - /// Ok(().into()) + /// Ok(()) /// } /// } /// @@ -367,13 +365,16 @@ impl Engine { pub fn register_set_result( &mut self, name: &str, - callback: impl Fn(&mut T, U) -> Result> + SendSync + 'static, + callback: impl Fn(&mut T, U) -> Result<(), Box> + SendSync + 'static, ) -> &mut Self where T: Variant + Clone, U: Variant + Clone, { - self.register_result_fn(&make_setter(name), callback) + self.register_result_fn(&make_setter(name), move |obj: &mut T, value: U| { + callback(obj, value)?; + Ok(().into()) + }) } /// Short-hand for registering both getter and setter functions @@ -391,8 +392,8 @@ impl Engine { /// /// impl TestStruct { /// fn new() -> Self { TestStruct { field: 1 } } - /// fn get_field(&mut self) -> i64 { self.field } /// // Even a getter must start with `&mut self` and not `&self`. + /// fn get_field(&mut self) -> i64 { self.field } /// fn set_field(&mut self, new_val: i64) { self.field = new_val; } /// } /// @@ -441,8 +442,7 @@ impl Engine { /// } /// /// impl TestStruct { - /// fn new() -> Self { TestStruct { fields: vec![1, 2, 3, 4, 5] } } - /// + /// fn new() -> Self { TestStruct { fields: vec![1, 2, 3, 4, 5] } } /// // Even a getter must start with `&mut self` and not `&self`. /// fn get_field(&mut self, index: i64) -> i64 { self.fields[index as usize] } /// } @@ -494,8 +494,7 @@ impl Engine { /// } /// /// impl TestStruct { - /// fn new() -> Self { TestStruct { fields: vec![1, 2, 3, 4, 5] } } - /// + /// fn new() -> Self { TestStruct { fields: vec![1, 2, 3, 4, 5] } } /// // Even a getter must start with `&mut self` and not `&self`. /// fn get_field(&mut self, index: i64) -> Result> { /// Ok(self.fields[index as usize].into()) @@ -541,7 +540,7 @@ impl Engine { /// } /// /// impl TestStruct { - /// fn new() -> Self { TestStruct { fields: vec![1, 2, 3, 4, 5] } } + /// fn new() -> Self { TestStruct { fields: vec![1, 2, 3, 4, 5] } } /// fn set_field(&mut self, index: i64, value: i64) { self.fields[index as usize] = value; } /// } /// @@ -569,7 +568,7 @@ impl Engine { #[cfg(not(feature = "no_index"))] pub fn register_indexer_set( &mut self, - callback: impl Fn(&mut T, X, U) -> () + SendSync + 'static, + callback: impl Fn(&mut T, X, U) + SendSync + 'static, ) -> &mut Self where T: Variant + Clone, @@ -580,7 +579,7 @@ impl Engine { } /// Register an index setter for a custom type with the `Engine`. - /// Returns `Result>`. + /// Returns `Result<(), Box>`. /// /// # Example /// @@ -593,10 +592,10 @@ impl Engine { /// } /// /// impl TestStruct { - /// fn new() -> Self { TestStruct { fields: vec![1, 2, 3, 4, 5] } } - /// fn set_field(&mut self, index: i64, value: i64) -> Result> { + /// fn new() -> Self { TestStruct { fields: vec![1, 2, 3, 4, 5] } } + /// fn set_field(&mut self, index: i64, value: i64) -> Result<(), Box> { /// self.fields[index as usize] = value; - /// Ok(().into()) + /// Ok(()) /// } /// } /// @@ -622,14 +621,17 @@ impl Engine { #[cfg(not(feature = "no_index"))] pub fn register_indexer_set_result( &mut self, - callback: impl Fn(&mut T, X, U) -> Result> + SendSync + 'static, + callback: impl Fn(&mut T, X, U) -> Result<(), Box> + SendSync + 'static, ) -> &mut Self where T: Variant + Clone, U: Variant + Clone, X: Variant + Clone, { - self.register_result_fn(FN_IDX_SET, callback) + self.register_result_fn(FN_IDX_SET, move |obj: &mut T, index: X, value: U| { + callback(obj, index, value)?; + Ok(().into()) + }) } /// Short-hand for register both index getter and setter functions for a custom type with the `Engine`. @@ -643,7 +645,8 @@ impl Engine { /// } /// /// impl TestStruct { - /// fn new() -> Self { TestStruct { fields: vec![1, 2, 3, 4, 5] } } + /// fn new() -> Self { TestStruct { fields: vec![1, 2, 3, 4, 5] } } + /// // Even a getter must start with `&mut self` and not `&self`. /// fn get_field(&mut self, index: i64) -> i64 { self.fields[index as usize] } /// fn set_field(&mut self, index: i64, value: i64) { self.fields[index as usize] = value; } /// }