From ae02668d0859e3cb87312c4abe80ffd3fe392f1e Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 3 Jan 2023 14:00:18 +0800 Subject: [PATCH] Mark pure. --- CHANGELOG.md | 9 +++++++++ src/packages/array_basic.rs | 38 +++++++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 135fc201..0ba1bc4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,15 @@ Rhai Release Notes ================== +Version 1.13.0 +============== + +Bug fixes +--------- + +* `map` and `filter` for arrays are marked `pure`. Warnings are added to the documentation of pure array methods that take `this` closures. + + Version 1.12.0 ============== diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 08c61b9e..7dabd569 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -651,6 +651,8 @@ pub mod array_functions { /// /// Array element (mutable) is bound to `this`. /// + /// This method is marked _pure_; the `mapper` function should not mutate array elements. + /// /// # Function Parameters /// /// * `element`: copy of array element @@ -669,7 +671,7 @@ pub mod array_functions { /// /// print(y); // prints "[0, 2, 6, 12, 20]" /// ``` - #[rhai_fn(return_raw)] + #[rhai_fn(return_raw, pure)] pub fn map(ctx: NativeCallContext, array: &mut Array, map: FnPtr) -> RhaiResultOf { if array.is_empty() { return Ok(Array::new()); @@ -692,6 +694,8 @@ pub mod array_functions { /// /// Array element (mutable) is bound to `this`. /// + /// This method is marked _pure_; the `filter` function should not mutate array elements. + /// /// # Function Parameters /// /// * `element`: copy of array element @@ -710,7 +714,7 @@ pub mod array_functions { /// /// print(y); // prints "[12, 20]" /// ``` - #[rhai_fn(return_raw)] + #[rhai_fn(return_raw, pure)] pub fn filter(ctx: NativeCallContext, array: &mut Array, filter: FnPtr) -> RhaiResultOf { if array.is_empty() { return Ok(Array::new()); @@ -882,6 +886,8 @@ pub mod array_functions { /// /// Array element (mutable) is bound to `this`. /// + /// This method is marked _pure_; the `filter` function should not mutate array elements. + /// /// # Function Parameters /// /// * `element`: copy of array element @@ -922,6 +928,8 @@ pub mod array_functions { /// /// Array element (mutable) is bound to `this`. /// + /// This method is marked _pure_; the `filter` function should not mutate array elements. + /// /// # Function Parameters /// /// * `element`: copy of array element @@ -1011,6 +1019,8 @@ pub mod array_functions { /// /// Array element (mutable) is bound to `this`. /// + /// This method is marked _pure_; the `filter` function should not mutate array elements. + /// /// # Function Parameters /// /// * `element`: copy of array element @@ -1055,6 +1065,8 @@ pub mod array_functions { /// /// Array element (mutable) is bound to `this`. /// + /// This method is marked _pure_; the `mapper` function should not mutate array elements. + /// /// # Function Parameters /// /// * `element`: copy of array element @@ -1087,6 +1099,8 @@ pub mod array_functions { /// /// Array element (mutable) is bound to `this`. /// + /// This method is marked _pure_; the `mapper` function should not mutate array elements. + /// /// # Function Parameters /// /// * `element`: copy of array element @@ -1143,6 +1157,8 @@ pub mod array_functions { /// /// Array element (mutable) is bound to `this`. /// + /// This method is marked _pure_; the `filter` function should not mutate array elements. + /// /// # Function Parameters /// /// * `element`: copy of array element @@ -1185,6 +1201,8 @@ pub mod array_functions { /// /// Array element (mutable) is bound to `this`. /// + /// This method is marked _pure_; the `filter` function should not mutate array elements. + /// /// # Function Parameters /// /// * `element`: copy of array element @@ -1281,9 +1299,11 @@ pub mod array_functions { /// # Function Parameters /// /// * `result`: accumulated result, initially `()` - /// * `element`: copy of array element + /// * `element`: copy of array element, or bound to `this` if omitted /// * `index` _(optional)_: current index in the array /// + /// This method is marked _pure_; the `reducer` function should not mutate array elements. + /// /// # Example /// /// ```rhai @@ -1306,9 +1326,11 @@ pub mod array_functions { /// # Function Parameters /// /// * `result`: accumulated result, starting with the value of `initial` - /// * `element`: copy of array element + /// * `element`: copy of array element, or bound to `this` if omitted /// * `index` _(optional)_: current index in the array /// + /// This method is marked _pure_; the `reducer` function should not mutate array elements. + /// /// # Example /// /// ```rhai @@ -1347,9 +1369,11 @@ pub mod array_functions { /// # Function Parameters /// /// * `result`: accumulated result, initially `()` - /// * `element`: copy of array element + /// * `element`: copy of array element, or bound to `this` if omitted /// * `index` _(optional)_: current index in the array /// + /// This method is marked _pure_; the `reducer` function should not mutate array elements. + /// /// # Example /// /// ```rhai @@ -1373,9 +1397,11 @@ pub mod array_functions { /// # Function Parameters /// /// * `result`: accumulated result, starting with the value of `initial` - /// * `element`: copy of array element + /// * `element`: copy of array element, or bound to `this` if omitted /// * `index` _(optional)_: current index in the array /// + /// This method is marked _pure_; the `reducer` function should not mutate array elements. + /// /// # Example /// /// ```rhai