From c727b529f5925674c5bd32050a7af1238032c5e5 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 15 Sep 2022 08:55:07 +0800 Subject: [PATCH] Minor refactors. --- src/api/register.rs | 4 +-- src/func/builtin.rs | 62 ++++++++++++++++++++---------------- src/func/call.rs | 8 ++--- src/func/register.rs | 2 +- src/module/mod.rs | 14 ++++---- src/module/resolvers/file.rs | 8 ++--- src/types/dynamic.rs | 4 +-- src/types/interner.rs | 1 - 8 files changed, 51 insertions(+), 52 deletions(-) diff --git a/src/api/register.rs b/src/api/register.rs index d4a4e2c1..2b4e20ed 100644 --- a/src/api/register.rs +++ b/src/api/register.rs @@ -695,13 +695,13 @@ impl Engine { if root.is_empty() || !root.contains_key(sub_module) { let mut m = Module::new(); - register_static_module_raw(m.get_sub_modules(), remainder, module); + register_static_module_raw(m.get_sub_modules_mut(), remainder, module); m.build_index(); root.insert(sub_module.into(), m.into()); } else { let m = root.remove(sub_module).expect("contains sub-module"); let mut m = crate::func::shared_take_or_clone(m); - register_static_module_raw(m.get_sub_modules(), remainder, module); + register_static_module_raw(m.get_sub_modules_mut(), remainder, module); m.build_index(); root.insert(sub_module.into(), m.into()); } diff --git a/src/func/builtin.rs b/src/func/builtin.rs index cc70a0b6..4ad42130 100644 --- a/src/func/builtin.rs +++ b/src/func/builtin.rs @@ -25,32 +25,42 @@ const BUILTIN: &str = "data type was checked"; #[inline] #[must_use] fn is_numeric(type_id: TypeId) -> bool { - let result = false; + if type_id == TypeId::of::() { + return true; + } #[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i32"))] - let result = result - || type_id == TypeId::of::() + if type_id == TypeId::of::() || type_id == TypeId::of::() || type_id == TypeId::of::() || type_id == TypeId::of::() || type_id == TypeId::of::() || type_id == TypeId::of::() || type_id == TypeId::of::() - || type_id == TypeId::of::(); + || type_id == TypeId::of::() + { + return true; + } #[cfg(not(feature = "only_i64"))] #[cfg(not(feature = "only_i32"))] #[cfg(not(target_family = "wasm"))] - let result = result || type_id == TypeId::of::() || type_id == TypeId::of::(); + if type_id == TypeId::of::() || type_id == TypeId::of::() { + return true; + } #[cfg(not(feature = "no_float"))] - let result = result || type_id == TypeId::of::() || type_id == TypeId::of::(); + if type_id == TypeId::of::() || type_id == TypeId::of::() { + return true; + } #[cfg(feature = "decimal")] - let result = result || type_id == TypeId::of::(); + if type_id == TypeId::of::() { + return true; + } - result + false } /// Build in common binary operator implementations to avoid the cost of calling a registered function. @@ -61,8 +71,6 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option { |_, args| { let x = &*args[0].read_lock::<$xx>().expect(BUILTIN); @@ -255,7 +263,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option { - if types_pair == (TypeId::of::<$x>(), TypeId::of::<$y>()) { + if (type1, type2) == (TypeId::of::<$x>(), TypeId::of::<$y>()) { return match op { "+" => Some(impl_op!(FLOAT => $xx + $yy)), "-" => Some(impl_op!(FLOAT => $xx - $yy)), @@ -285,7 +293,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option { - if types_pair == (TypeId::of::<$x>(), TypeId::of::<$y>()) { + if (type1, type2) == (TypeId::of::<$x>(), TypeId::of::<$y>()) { #[cfg(not(feature = "unchecked"))] use crate::packages::arithmetic::decimal_functions::*; @@ -335,7 +343,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option(), TypeId::of::()) { + if (type1, type2) == (TypeId::of::(), TypeId::of::()) { fn get_s1s2(args: &FnCallArgs) -> ([char; 2], [char; 2]) { let x = args[0].as_char().expect(BUILTIN); let y = &*args[1].read_lock::().expect(BUILTIN); @@ -361,7 +369,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option(), TypeId::of::()) { + if (type1, type2) == (TypeId::of::(), TypeId::of::()) { fn get_s1s2(args: &FnCallArgs) -> ([char; 2], [char; 2]) { let x = &*args[0].read_lock::().expect(BUILTIN); let y = args[1].as_char().expect(BUILTIN); @@ -397,7 +405,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option(), TypeId::of::()) { + if (type1, type2) == (TypeId::of::<()>(), TypeId::of::()) { return match op { "+" => Some(|_, args| Ok(args[1].clone())), "==" | ">" | ">=" | "<" | "<=" => Some(|_, _| Ok(Dynamic::FALSE)), @@ -406,7 +414,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option(), TypeId::of::<()>()) { + if (type1, type2) == (TypeId::of::(), TypeId::of::<()>()) { return match op { "+" => Some(|_, args| Ok(args[0].clone())), "==" | ">" | ">=" | "<" | "<=" => Some(|_, _| Ok(Dynamic::FALSE)), @@ -446,7 +454,7 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option(), TypeId::of::()) { + if (type1, type2) == (TypeId::of::(), TypeId::of::()) { use crate::Map; return match op { @@ -456,12 +464,12 @@ pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option(), TypeId::of::(), ) - || types_pair + || (type1, type2) == ( TypeId::of::(), TypeId::of::(), @@ -554,8 +562,6 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio let type1 = x.type_id(); let type2 = y.type_id(); - let types_pair = (type1, type2); - macro_rules! impl_op { ($x:ty = x $op:tt $yy:ident) => { |_, args| { let x = args[0].$yy().expect(BUILTIN); @@ -691,7 +697,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio #[cfg(not(feature = "no_float"))] macro_rules! impl_float { ($x:ident, $xx:ident, $y:ty, $yy:ident) => { - if types_pair == (TypeId::of::<$x>(), TypeId::of::<$y>()) { + if (type1, type2) == (TypeId::of::<$x>(), TypeId::of::<$y>()) { return match op { "+=" => Some(impl_op!($x += $yy)), "-=" => Some(impl_op!($x -= $yy)), @@ -714,7 +720,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio #[cfg(feature = "decimal")] macro_rules! impl_decimal { ($x:ident, $xx:ident, $y:ty, $yy:ident) => { - if types_pair == (TypeId::of::<$x>(), TypeId::of::<$y>()) { + if (type1, type2) == (TypeId::of::<$x>(), TypeId::of::<$y>()) { #[cfg(not(feature = "unchecked"))] use crate::packages::arithmetic::decimal_functions::*; @@ -753,7 +759,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio } // string op= char - if types_pair == (TypeId::of::(), TypeId::of::()) { + if (type1, type2) == (TypeId::of::(), TypeId::of::()) { return match op { "+=" => Some(impl_op!(ImmutableString += as_char as char)), "-=" => Some(impl_op!(ImmutableString -= as_char as char)), @@ -761,7 +767,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio }; } // char op= string - if types_pair == (TypeId::of::(), TypeId::of::()) { + if (type1, type2) == (TypeId::of::(), TypeId::of::()) { return match op { "+=" => Some(|_, args| { let mut ch = args[0].as_char().expect(BUILTIN).to_string(); @@ -810,7 +816,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio use crate::Blob; // blob op= int - if types_pair == (TypeId::of::(), TypeId::of::()) { + if (type1, type2) == (TypeId::of::(), TypeId::of::()) { return match op { "+=" => Some(|_, args| { let x = args[1].as_int().expect("`INT`"); @@ -822,7 +828,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio } // blob op= char - if types_pair == (TypeId::of::(), TypeId::of::()) { + if (type1, type2) == (TypeId::of::(), TypeId::of::()) { return match op { "+=" => Some(|_, args| { let x = args[1].as_char().expect("`char`"); @@ -834,7 +840,7 @@ pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Optio } // blob op= string - if types_pair == (TypeId::of::(), TypeId::of::()) { + if (type1, type2) == (TypeId::of::(), TypeId::of::()) { return match op { "+=" => Some(|_, args| { let s = std::mem::take(args[1]).cast::(); diff --git a/src/func/call.rs b/src/func/call.rs index 7adcded8..9445e714 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -362,12 +362,12 @@ impl Engine { let parent_source = global.source.clone(); // Check if function access already in the cache - let mut local_entry = None; + let local_entry = &mut None; let func = self.resolve_fn( global, caches, - &mut local_entry, + local_entry, lib, name, hash, @@ -640,14 +640,14 @@ impl Engine { // Script-defined function call? #[cfg(not(feature = "no_function"))] - let mut local_entry = None; + let local_entry = &mut None; #[cfg(not(feature = "no_function"))] if let Some(FnResolutionCacheEntry { func, ref source }) = self .resolve_fn( global, caches, - &mut local_entry, + local_entry, lib, fn_name, hashes.script, diff --git a/src/func/register.rs b/src/func/register.rs index b1fed5bd..37cc6bd0 100644 --- a/src/func/register.rs +++ b/src/func/register.rs @@ -64,7 +64,7 @@ pub fn by_value(data: &mut Dynamic) -> T { /// /// # Type Parameters /// -/// * `ARGS` - a tuple containing parameter types, with `&mut T` represented by [`Mut`]. +/// * `ARGS` - a tuple containing parameter types, with `&mut T` represented by `Mut`. /// * `RET` - return type of the function; if the function returns `Result`, it is the unwrapped inner value type. pub trait RegisterNativeFunction { /// Convert this function into a [`CallableFunction`]. diff --git a/src/module/mod.rs b/src/module/mod.rs index 73f925f8..e027f384 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -16,7 +16,7 @@ use crate::{ use std::prelude::v1::*; use std::{ any::TypeId, - collections::{BTreeMap, BTreeSet}, + collections::BTreeMap, fmt, ops::{Add, AddAssign}, }; @@ -213,7 +213,7 @@ impl fmt::Debug for Module { .iter() .flat_map(|m| m.keys()) .map(SmartString::as_str) - .collect::>(), + .collect::>(), ) .field("vars", &self.variables) .field( @@ -221,7 +221,7 @@ impl fmt::Debug for Module { &self .iter_fn() .map(|f| f.func.to_string()) - .collect::>(), + .collect::>(), ); #[cfg(feature = "metadata")] @@ -710,8 +710,7 @@ impl Module { #[cfg(feature = "metadata")] comments: Box::default(), func: fn_def.into(), - } - .into(), + }, ); self.indexed = false; self.contains_indexed_global_functions = false; @@ -749,7 +748,7 @@ impl Module { #[cfg(not(feature = "no_module"))] #[inline] #[must_use] - pub(crate) fn get_sub_modules(&mut self) -> &mut BTreeMap> { + pub(crate) fn get_sub_modules_mut(&mut self) -> &mut BTreeMap> { // We must assume that the user has changed the sub-modules // (otherwise why take a mutable reference?) self.all_functions = None; @@ -1044,8 +1043,7 @@ impl Module { return_type: return_type_name, #[cfg(feature = "metadata")] comments: Box::default(), - } - .into(), + }, ); self.indexed = false; diff --git a/src/module/resolvers/file.rs b/src/module/resolvers/file.rs index 69731944..ee71ce13 100644 --- a/src/module/resolvers/file.rs +++ b/src/module/resolvers/file.rs @@ -4,7 +4,7 @@ use crate::eval::GlobalRuntimeState; use crate::func::{locked_read, locked_write}; use crate::{ - Engine, Identifier, Module, ModuleResolver, Position, RhaiResultOf, Scope, Shared, ERR, + Engine, Identifier, Locked, Module, ModuleResolver, Position, RhaiResultOf, Scope, Shared, ERR, }; use std::{ @@ -51,11 +51,7 @@ pub struct FileModuleResolver { extension: Identifier, cache_enabled: bool, scope: Scope<'static>, - - #[cfg(not(feature = "sync"))] - cache: std::cell::RefCell>>, - #[cfg(feature = "sync")] - cache: std::sync::RwLock>>, + cache: Locked>>, } impl Default for FileModuleResolver { diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index 5d4803d1..6823dd74 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -1115,12 +1115,12 @@ impl Dynamic { /// Beware that you need to pass in an [`Array`][crate::Array] type for it to be recognized as /// an [`Array`][crate::Array]. A [`Vec`][Vec] does not get automatically converted to an /// [`Array`][crate::Array], but will be a custom type instead (stored as a trait object). Use - /// `Into` to convert a [`Vec`][Vec] into a [`Dynamic`] as an + /// [`Dynamic::from_array`] to convert a [`Vec`][Vec] into a [`Dynamic`] as an /// [`Array`][crate::Array] value. /// /// Similarly, passing in a [`HashMap`][std::collections::HashMap] or /// [`BTreeMap`][std::collections::BTreeMap] will not get a [`Map`][crate::Map] but a - /// custom type. Again, use `Into` to get a [`Dynamic`] with a [`Map`][crate::Map] + /// custom type. Again, use [`Dynamic::from_map`] to get a [`Dynamic`] with a [`Map`][crate::Map] /// value. /// /// # Examples diff --git a/src/types/interner.rs b/src/types/interner.rs index 2af5363d..3dfa1048 100644 --- a/src/types/interner.rs +++ b/src/types/interner.rs @@ -23,7 +23,6 @@ pub const MAX_STRING_LEN: usize = 24; /// Exported under the `internals` feature only. /// /// Normal identifiers, property getters and setters are interned separately. -#[derive(Clone)] pub struct StringsInterner<'a> { /// Maximum number of strings interned. pub capacity: usize,