From c8e7c970d10b289392172eaeddeeb49fcd4c3e20 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 20 Oct 2021 11:06:38 +0800 Subject: [PATCH] Add variants of Array methods taking FnPtr with function names. --- CHANGELOG.md | 1 + src/packages/array_basic.rs | 107 ++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71e36a06..2999ebc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Enhancements ------------ * Array methods now avoid cloning as much as possible (although most predicates will involve cloning anyway if passed a closure). +* Array methods that take function pointers (e.g. closures) now optionally take the function name as a string. * Inlining is disabled for error-path functions because errors are exceptional and scripts usually fail completely when an error is encountered. Deprecated API's diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 342fbd3e..0e3e533f 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -242,6 +242,14 @@ mod array_functions { result } } + #[rhai_fn(name = "map", return_raw, pure)] + pub fn map_with_fn_name( + ctx: NativeCallContext, + array: &mut Array, + mapper: &str, + ) -> Result> { + map(ctx, array, FnPtr::new(mapper)?) + } #[rhai_fn(return_raw, pure)] pub fn map( ctx: NativeCallContext, @@ -285,6 +293,14 @@ mod array_functions { Ok(ar) } + #[rhai_fn(name = "filter", return_raw, pure)] + pub fn filter_with_fn_name( + ctx: NativeCallContext, + array: &mut Array, + filter_func: &str, + ) -> Result> { + filter(ctx, array, FnPtr::new(filter_func)?) + } #[rhai_fn(return_raw, pure)] pub fn filter( ctx: NativeCallContext, @@ -427,6 +443,14 @@ mod array_functions { Ok(-1 as INT) } #[rhai_fn(name = "index_of", return_raw, pure)] + pub fn index_of_with_fn_name( + ctx: NativeCallContext, + array: &mut Array, + filter: &str, + ) -> Result> { + index_of_filter(ctx, array, FnPtr::new(filter)?) + } + #[rhai_fn(name = "index_of", return_raw, pure)] pub fn index_of_filter( ctx: NativeCallContext, array: &mut Array, @@ -439,6 +463,15 @@ mod array_functions { } } #[rhai_fn(name = "index_of", return_raw, pure)] + pub fn index_of_with_fn_name_starting_from( + ctx: NativeCallContext, + array: &mut Array, + filter: &str, + start: INT, + ) -> Result> { + index_of_filter_starting_from(ctx, array, FnPtr::new(filter)?, start) + } + #[rhai_fn(name = "index_of", return_raw, pure)] pub fn index_of_filter_starting_from( ctx: NativeCallContext, array: &mut Array, @@ -496,6 +529,14 @@ mod array_functions { Ok(-1 as INT) } + #[rhai_fn(name = "some", return_raw, pure)] + pub fn some_with_fn_name( + ctx: NativeCallContext, + array: &mut Array, + filter: &str, + ) -> Result> { + some(ctx, array, FnPtr::new(filter)?) + } #[rhai_fn(return_raw, pure)] pub fn some( ctx: NativeCallContext, @@ -542,6 +583,14 @@ mod array_functions { Ok(false) } + #[rhai_fn(name = "all", return_raw, pure)] + pub fn all_with_fn_name( + ctx: NativeCallContext, + array: &mut Array, + filter: &str, + ) -> Result> { + all(ctx, array, FnPtr::new(filter)?) + } #[rhai_fn(return_raw, pure)] pub fn all( ctx: NativeCallContext, @@ -588,6 +637,14 @@ mod array_functions { Ok(true) } + #[rhai_fn(name = "reduce", return_raw, pure)] + pub fn reduce_with_fn_name( + ctx: NativeCallContext, + array: &mut Array, + reducer: &str, + ) -> Result> { + reduce(ctx, array, FnPtr::new(reducer)?) + } #[rhai_fn(return_raw, pure)] pub fn reduce( ctx: NativeCallContext, @@ -624,6 +681,15 @@ mod array_functions { Ok(result) } #[rhai_fn(name = "reduce", return_raw, pure)] + pub fn reduce_with_fn_name_with_initial( + ctx: NativeCallContext, + array: &mut Array, + reducer: &str, + initial: Dynamic, + ) -> Result> { + reduce_with_initial(ctx, array, FnPtr::new(reducer)?, initial) + } + #[rhai_fn(name = "reduce", return_raw, pure)] pub fn reduce_with_initial( ctx: NativeCallContext, array: &mut Array, @@ -659,6 +725,14 @@ mod array_functions { Ok(result) } + #[rhai_fn(name = "reduce_ref", return_raw, pure)] + pub fn reduce_rev_with_fn_name( + ctx: NativeCallContext, + array: &mut Array, + reducer: &str, + ) -> Result> { + reduce_rev(ctx, array, FnPtr::new(reducer)?) + } #[rhai_fn(return_raw, pure)] pub fn reduce_rev( ctx: NativeCallContext, @@ -694,6 +768,15 @@ mod array_functions { Ok(result) } + #[rhai_fn(name = "reduce_ref", return_raw, pure)] + pub fn reduce_rev_with_fn_name_with_initial( + ctx: NativeCallContext, + array: &mut Array, + reducer: &str, + initial: Dynamic, + ) -> Result> { + reduce_rev_with_initial(ctx, array, FnPtr::new(reducer)?, initial) + } #[rhai_fn(name = "reduce_rev", return_raw, pure)] pub fn reduce_rev_with_initial( ctx: NativeCallContext, @@ -730,6 +813,14 @@ mod array_functions { Ok(result) } + #[rhai_fn(name = "sort", return_raw)] + pub fn sort_with_fn_name( + ctx: NativeCallContext, + array: &mut Array, + comparer: &str, + ) -> Result<(), Box> { + sort(ctx, array, FnPtr::new(comparer)?) + } #[rhai_fn(return_raw)] pub fn sort( ctx: NativeCallContext, @@ -756,6 +847,14 @@ mod array_functions { Ok(()) } + #[rhai_fn(name = "drain", return_raw, pure)] + pub fn drain_with_fn_name( + ctx: NativeCallContext, + array: &mut Array, + filter: &str, + ) -> Result> { + drain(ctx, array, FnPtr::new(filter)?) + } #[rhai_fn(return_raw)] pub fn drain( ctx: NativeCallContext, @@ -850,6 +949,14 @@ mod array_functions { array.drain(start..start + len).collect() } + #[rhai_fn(name = "retain", return_raw, pure)] + pub fn retain_with_fn_name( + ctx: NativeCallContext, + array: &mut Array, + filter: &str, + ) -> Result> { + retain(ctx, array, FnPtr::new(filter)?) + } #[rhai_fn(return_raw)] pub fn retain( ctx: NativeCallContext,