Setters should have no return value.

This commit is contained in:
Stephen Chung 2020-09-22 12:14:26 +08:00
parent 8095ebc9e4
commit 283495a02f
4 changed files with 35 additions and 27 deletions

View File

@ -13,6 +13,11 @@ Bug fixes
* Indexers are available under `no_object`. * Indexers are available under `no_object`.
* Registered operator-assignment functions (e.g. `+=`) now work correctly. * 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<EvalAltResult>>`.
New features New features
------------ ------------

View File

@ -12,10 +12,10 @@ Getters and setters are disabled when the [`no_object`] feature is used.
| `Engine` API | Description | Return Value of Function | | `Engine` API | Description | Return Value of Function |
| --------------------- | ------------------------------------------------- | :-----------------------------------: | | --------------------- | ------------------------------------------------- | :-----------------------------------: |
| `register_get` | Register a getter | _Any_ | | `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_set` | Short-hand to register both a getter and a setter | _None_ |
| `register_get_result` | Register a getter | `Result<Dynamic, Box<EvalAltResult>>` | | `register_get_result` | Register a getter | `Result<Dynamic, Box<EvalAltResult>>` |
| `register_set_result` | Register a setter | `Result<Dynamic, Box<EvalAltResult>>` | | `register_set_result` | Register a setter | `Result<(), Box<EvalAltResult>>` |
Examples Examples

View File

@ -19,10 +19,10 @@ For efficiency reasons, indexers **cannot** be used to overload (i.e. override)
| `Engine` API | Description | Return Value of Function | | `Engine` API | Description | Return Value of Function |
| ----------------------------- | -------------------------------------------------------- | :-----------------------------------: | | ----------------------------- | -------------------------------------------------------- | :-----------------------------------: |
| `register_indexer_get` | Register an index getter | _Any_ | | `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_set` | Short-hand to register both an index getter and a setter | _None_ |
| `register_indexer_get_result` | Register an index getter | `Result<Dynamic, Box<EvalAltResult>>` | | `register_indexer_get_result` | Register an index getter | `Result<Dynamic, Box<EvalAltResult>>` |
| `register_indexer_set_result` | Register an index setter | `Result<Dynamic, Box<EvalAltResult>>` | | `register_indexer_set_result` | Register an index setter | `Result<(), Box<EvalAltResult>>` |
Examples Examples

View File

@ -191,7 +191,6 @@ impl Engine {
/// ///
/// impl TestStruct { /// 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`. /// // Even a getter must start with `&mut self` and not `&self`.
/// fn get_field(&mut self) -> i64 { self.field } /// fn get_field(&mut self) -> i64 { self.field }
/// } /// }
@ -243,7 +242,6 @@ impl Engine {
/// ///
/// impl TestStruct { /// 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`. /// // Even a getter must start with `&mut self` and not `&self`.
/// fn get_field(&mut self) -> Result<Dynamic, Box<EvalAltResult>> { /// fn get_field(&mut self) -> Result<Dynamic, Box<EvalAltResult>> {
/// Ok(self.field.into()) /// Ok(self.field.into())
@ -324,7 +322,7 @@ impl Engine {
} }
/// Register a setter function for a member of a registered type with the `Engine`. /// Register a setter function for a member of a registered type with the `Engine`.
/// Returns `Result<Dynamic, Box<EvalAltResult>>`. /// Returns `Result<(), Box<EvalAltResult>>`.
/// ///
/// # Example /// # Example
/// ///
@ -338,9 +336,9 @@ impl Engine {
/// ///
/// impl TestStruct { /// impl TestStruct {
/// fn new() -> Self { TestStruct { field: 1 } } /// fn new() -> Self { TestStruct { field: 1 } }
/// fn set_field(&mut self, new_val: i64) -> Result<Dynamic, Box<EvalAltResult>> { /// fn set_field(&mut self, new_val: i64) -> Result<(), Box<EvalAltResult>> {
/// self.field = new_val; /// self.field = new_val;
/// Ok(().into()) /// Ok(())
/// } /// }
/// } /// }
/// ///
@ -367,13 +365,16 @@ impl Engine {
pub fn register_set_result<T, U>( pub fn register_set_result<T, U>(
&mut self, &mut self,
name: &str, name: &str,
callback: impl Fn(&mut T, U) -> Result<Dynamic, Box<EvalAltResult>> + SendSync + 'static, callback: impl Fn(&mut T, U) -> Result<(), Box<EvalAltResult>> + SendSync + 'static,
) -> &mut Self ) -> &mut Self
where where
T: Variant + Clone, T: Variant + Clone,
U: 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 /// Short-hand for registering both getter and setter functions
@ -391,8 +392,8 @@ impl Engine {
/// ///
/// impl TestStruct { /// impl TestStruct {
/// fn new() -> Self { TestStruct { field: 1 } } /// 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`. /// // 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; } /// fn set_field(&mut self, new_val: i64) { self.field = new_val; }
/// } /// }
/// ///
@ -442,7 +443,6 @@ impl Engine {
/// ///
/// impl TestStruct { /// 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`. /// // 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 get_field(&mut self, index: i64) -> i64 { self.fields[index as usize] }
/// } /// }
@ -495,7 +495,6 @@ impl Engine {
/// ///
/// impl TestStruct { /// 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`. /// // Even a getter must start with `&mut self` and not `&self`.
/// fn get_field(&mut self, index: i64) -> Result<Dynamic, Box<EvalAltResult>> { /// fn get_field(&mut self, index: i64) -> Result<Dynamic, Box<EvalAltResult>> {
/// Ok(self.fields[index as usize].into()) /// Ok(self.fields[index as usize].into())
@ -569,7 +568,7 @@ impl Engine {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
pub fn register_indexer_set<T, X, U>( pub fn register_indexer_set<T, X, U>(
&mut self, &mut self,
callback: impl Fn(&mut T, X, U) -> () + SendSync + 'static, callback: impl Fn(&mut T, X, U) + SendSync + 'static,
) -> &mut Self ) -> &mut Self
where where
T: Variant + Clone, T: Variant + Clone,
@ -580,7 +579,7 @@ impl Engine {
} }
/// Register an index setter for a custom type with the `Engine`. /// Register an index setter for a custom type with the `Engine`.
/// Returns `Result<Dynamic, Box<EvalAltResult>>`. /// Returns `Result<(), Box<EvalAltResult>>`.
/// ///
/// # Example /// # Example
/// ///
@ -594,9 +593,9 @@ impl Engine {
/// ///
/// impl TestStruct { /// 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) -> Result<Dynamic, Box<EvalAltResult>> { /// fn set_field(&mut self, index: i64, value: i64) -> Result<(), Box<EvalAltResult>> {
/// self.fields[index as usize] = value; /// self.fields[index as usize] = value;
/// Ok(().into()) /// Ok(())
/// } /// }
/// } /// }
/// ///
@ -622,14 +621,17 @@ impl Engine {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
pub fn register_indexer_set_result<T, X, U>( pub fn register_indexer_set_result<T, X, U>(
&mut self, &mut self,
callback: impl Fn(&mut T, X, U) -> Result<Dynamic, Box<EvalAltResult>> + SendSync + 'static, callback: impl Fn(&mut T, X, U) -> Result<(), Box<EvalAltResult>> + SendSync + 'static,
) -> &mut Self ) -> &mut Self
where where
T: Variant + Clone, T: Variant + Clone,
U: Variant + Clone, U: Variant + Clone,
X: 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`. /// Short-hand for register both index getter and setter functions for a custom type with the `Engine`.
@ -644,6 +646,7 @@ impl Engine {
/// ///
/// impl TestStruct { /// 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 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; } /// fn set_field(&mut self, index: i64, value: i64) { self.fields[index as usize] = value; }
/// } /// }