Change AST filters to Fn from FnMut.

This commit is contained in:
Stephen Chung 2021-02-13 10:56:09 +08:00
parent 2846d1b63f
commit 4fdd58f220
4 changed files with 18 additions and 17 deletions

View File

@ -19,6 +19,7 @@ Breaking changes
* trigonometry functions now take radians and return radians instead of degrees * trigonometry functions now take radians and return radians instead of degrees
* `Dynamic::into_shared` is no longer available under `no_closure`. It used to panic. * `Dynamic::into_shared` is no longer available under `no_closure`. It used to panic.
* `Token::is_operator` is renamed to `Token::is_symbol`. * `Token::is_operator` is renamed to `Token::is_symbol`.
* `AST::clone_functions_only_filtered`, `AST::merge_filtered`, `AST::combine_filtered` and `AST::retain_functions` now take `Fn` instead of `FnMut` as the filter predicate.
New features New features
------------ ------------

View File

@ -346,10 +346,10 @@ impl AST {
#[inline(always)] #[inline(always)]
pub fn clone_functions_only_filtered( pub fn clone_functions_only_filtered(
&self, &self,
mut filter: impl FnMut(FnNamespace, FnAccess, bool, &str, usize) -> bool, filter: impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool,
) -> Self { ) -> Self {
let mut functions: Module = Default::default(); let mut functions: Module = Default::default();
functions.merge_filtered(&self.functions, &mut filter); functions.merge_filtered(&self.functions, &filter);
Self { Self {
source: self.source.clone(), source: self.source.clone(),
statements: Default::default(), statements: Default::default(),
@ -531,7 +531,7 @@ impl AST {
pub fn merge_filtered( pub fn merge_filtered(
&self, &self,
other: &Self, other: &Self,
mut filter: impl FnMut(FnNamespace, FnAccess, bool, &str, usize) -> bool, filter: impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool,
) -> Self { ) -> Self {
let Self { let Self {
statements, statements,
@ -553,7 +553,7 @@ impl AST {
let source = other.source.clone().or_else(|| self.source.clone()); let source = other.source.clone().or_else(|| self.source.clone());
let mut functions = functions.as_ref().clone(); let mut functions = functions.as_ref().clone();
functions.merge_filtered(&other.functions, &mut filter); functions.merge_filtered(&other.functions, &filter);
if let Some(source) = source { if let Some(source) = source {
Self::new_with_source(ast, functions, source) Self::new_with_source(ast, functions, source)
@ -616,11 +616,11 @@ impl AST {
pub fn combine_filtered( pub fn combine_filtered(
&mut self, &mut self,
other: Self, other: Self,
mut filter: impl FnMut(FnNamespace, FnAccess, bool, &str, usize) -> bool, filter: impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool,
) -> &mut Self { ) -> &mut Self {
self.statements.extend(other.statements.into_iter()); self.statements.extend(other.statements.into_iter());
if !other.functions.is_empty() { if !other.functions.is_empty() {
shared_make_mut(&mut self.functions).merge_filtered(&other.functions, &mut filter); shared_make_mut(&mut self.functions).merge_filtered(&other.functions, &filter);
} }
self self
} }
@ -653,7 +653,7 @@ impl AST {
#[inline(always)] #[inline(always)]
pub fn retain_functions( pub fn retain_functions(
&mut self, &mut self,
filter: impl FnMut(FnNamespace, FnAccess, &str, usize) -> bool, filter: impl Fn(FnNamespace, FnAccess, &str, usize) -> bool,
) -> &mut Self { ) -> &mut Self {
if !self.functions.is_empty() { if !self.functions.is_empty() {
shared_make_mut(&mut self.functions).retain_script_functions(filter); shared_make_mut(&mut self.functions).retain_script_functions(filter);

View File

@ -699,11 +699,11 @@ impl Engine {
#[inline(always)] #[inline(always)]
pub fn register_indexer_get_set<T: Variant + Clone, X: Variant + Clone, U: Variant + Clone>( pub fn register_indexer_get_set<T: Variant + Clone, X: Variant + Clone, U: Variant + Clone>(
&mut self, &mut self,
getter: impl Fn(&mut T, X) -> U + SendSync + 'static, get_fn: impl Fn(&mut T, X) -> U + SendSync + 'static,
setter: impl Fn(&mut T, X, U) -> () + SendSync + 'static, set_fn: impl Fn(&mut T, X, U) -> () + SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
self.register_indexer_get(getter) self.register_indexer_get(get_fn)
.register_indexer_set(setter) .register_indexer_set(set_fn)
} }
/// Register a shared [`Module`] into the global namespace of [`Engine`]. /// Register a shared [`Module`] into the global namespace of [`Engine`].
/// ///

View File

@ -1334,12 +1334,12 @@ impl Module {
#[inline(always)] #[inline(always)]
pub fn set_indexer_get_set_fn<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>( pub fn set_indexer_get_set_fn<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>(
&mut self, &mut self,
getter: impl Fn(&mut A, B) -> Result<T, Box<EvalAltResult>> + SendSync + 'static, get_fn: impl Fn(&mut A, B) -> Result<T, Box<EvalAltResult>> + SendSync + 'static,
setter: impl Fn(&mut A, B, T) -> Result<(), Box<EvalAltResult>> + SendSync + 'static, set_fn: impl Fn(&mut A, B, T) -> Result<(), Box<EvalAltResult>> + SendSync + 'static,
) -> (NonZeroU64, NonZeroU64) { ) -> (NonZeroU64, NonZeroU64) {
( (
self.set_indexer_get_fn(getter), self.set_indexer_get_fn(get_fn),
self.set_indexer_set_fn(setter), self.set_indexer_set_fn(set_fn),
) )
} }
@ -1570,7 +1570,7 @@ impl Module {
pub(crate) fn merge_filtered( pub(crate) fn merge_filtered(
&mut self, &mut self,
other: &Self, other: &Self,
mut _filter: &mut impl FnMut(FnNamespace, FnAccess, bool, &str, usize) -> bool, _filter: &impl Fn(FnNamespace, FnAccess, bool, &str, usize) -> bool,
) -> &mut Self { ) -> &mut Self {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
other.modules.iter().for_each(|(k, v)| { other.modules.iter().for_each(|(k, v)| {
@ -1625,7 +1625,7 @@ impl Module {
#[inline] #[inline]
pub(crate) fn retain_script_functions( pub(crate) fn retain_script_functions(
&mut self, &mut self,
mut filter: impl FnMut(FnNamespace, FnAccess, &str, usize) -> bool, filter: impl Fn(FnNamespace, FnAccess, &str, usize) -> bool,
) -> &mut Self { ) -> &mut Self {
self.functions.retain( self.functions.retain(
|_, |_,