From 7068775f19229e6f9601f873aeecae2d002d119f Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 26 Jun 2022 14:10:09 +0800 Subject: [PATCH] Use locked_read. --- src/api/custom_syntax.rs | 2 +- src/api/register.rs | 4 +- src/ast/ast.rs | 11 ++--- src/func/call.rs | 2 +- src/func/mod.rs | 4 +- src/module/resolvers/file.rs | 7 +-- src/module/resolvers/mod.rs | 2 +- src/optimizer.rs | 2 +- src/types/dynamic.rs | 91 ++++++++--------------------------- src/types/immutable_string.rs | 2 +- 10 files changed, 35 insertions(+), 92 deletions(-) diff --git a/src/api/custom_syntax.rs b/src/api/custom_syntax.rs index 3982aee6..4aacf153 100644 --- a/src/api/custom_syntax.rs +++ b/src/api/custom_syntax.rs @@ -1,7 +1,7 @@ //! Module implementing custom syntax for [`Engine`]. use crate::ast::Expr; -use crate::func::native::SendSync; +use crate::func::SendSync; use crate::parser::ParseResult; use crate::tokenizer::{is_valid_identifier, Token}; use crate::types::dynamic::Variant; diff --git a/src/api/register.rs b/src/api/register.rs index 3c1a2ded..3ee7eb39 100644 --- a/src/api/register.rs +++ b/src/api/register.rs @@ -991,7 +991,7 @@ impl Engine { if !name.contains(separator.as_ref()) { if !module.is_indexed() { // Index the module (making a clone copy if necessary) if it is not indexed - let mut module = crate::func::native::shared_take_or_clone(module); + let mut module = crate::func::shared_take_or_clone(module); module.build_index(); root.insert(name.into(), module.into()); } else { @@ -1009,7 +1009,7 @@ impl Engine { root.insert(sub_module.into(), m.into()); } else { let m = root.remove(sub_module).expect("contains sub-module"); - let mut m = crate::func::native::shared_take_or_clone(m); + let mut m = crate::func::shared_take_or_clone(m); register_static_module_raw(m.sub_modules_mut(), remainder, module); m.build_index(); root.insert(sub_module.into(), m.into()); diff --git a/src/ast/ast.rs b/src/ast/ast.rs index f7e17414..67237b95 100644 --- a/src/ast/ast.rs +++ b/src/ast/ast.rs @@ -615,8 +615,7 @@ impl AST { #[cfg(not(feature = "no_function"))] if !other.lib.is_empty() { - crate::func::native::shared_make_mut(&mut self.lib) - .merge_filtered(&other.lib, &_filter); + crate::func::shared_make_mut(&mut self.lib).merge_filtered(&other.lib, &_filter); } #[cfg(not(feature = "no_module"))] @@ -629,10 +628,8 @@ impl AST { self.set_resolver(other.resolver.unwrap()); } (_, _) => { - let resolver = - crate::func::native::shared_make_mut(self.resolver.as_mut().unwrap()); - let other_resolver = - crate::func::native::shared_take_or_clone(other.resolver.unwrap()); + let resolver = crate::func::shared_make_mut(self.resolver.as_mut().unwrap()); + let other_resolver = crate::func::shared_take_or_clone(other.resolver.unwrap()); for (k, v) in other_resolver { resolver.insert(k, crate::func::shared_take_or_clone(v)); } @@ -673,7 +670,7 @@ impl AST { filter: impl Fn(FnNamespace, FnAccess, &str, usize) -> bool, ) -> &mut Self { if !self.lib.is_empty() { - crate::func::native::shared_make_mut(&mut self.lib).retain_script_functions(filter); + crate::func::shared_make_mut(&mut self.lib).retain_script_functions(filter); } self } diff --git a/src/func/call.rs b/src/func/call.rs index 55300581..6a0b05aa 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -1222,7 +1222,7 @@ impl Engine { let (mut target, _pos) = self.search_namespace(scope, global, lib, this_ptr, first_expr, level)?; - if target.as_ref().is_read_only() { + if target.is_read_only() { target = target.into_owned(); } diff --git a/src/func/mod.rs b/src/func/mod.rs index d47e7186..50d444f1 100644 --- a/src/func/mod.rs +++ b/src/func/mod.rs @@ -22,8 +22,8 @@ pub use hashing::{ combine_hashes, get_hasher, }; pub use native::{ - locked_read, locked_write, shared_make_mut, shared_take, shared_take_or_clone, shared_try_take, - FnAny, FnPlugin, IteratorFn, Locked, NativeCallContext, SendSync, Shared, + locked_read, locked_write, shared_get_mut, shared_make_mut, shared_take, shared_take_or_clone, + shared_try_take, FnAny, FnPlugin, IteratorFn, Locked, NativeCallContext, SendSync, Shared, }; pub use plugin::PluginFunction; pub use register::RegisterNativeFunction; diff --git a/src/module/resolvers/file.rs b/src/module/resolvers/file.rs index 7d61aa18..054168b6 100644 --- a/src/module/resolvers/file.rs +++ b/src/module/resolvers/file.rs @@ -307,12 +307,7 @@ impl FileModuleResolver { let file_path = self.get_file_path(path, source_path); if self.is_cache_enabled() { - #[cfg(not(feature = "sync"))] - let c = self.cache.borrow(); - #[cfg(feature = "sync")] - let c = self.cache.read().unwrap(); - - if let Some(module) = c.get(&file_path) { + if let Some(module) = locked_read(&self.cache).get(&file_path) { return Ok(module.clone()); } } diff --git a/src/module/resolvers/mod.rs b/src/module/resolvers/mod.rs index 6f2899cb..a7f2d5c4 100644 --- a/src/module/resolvers/mod.rs +++ b/src/module/resolvers/mod.rs @@ -1,5 +1,5 @@ use crate::eval::GlobalRuntimeState; -use crate::func::native::SendSync; +use crate::func::SendSync; use crate::{Engine, Module, Position, RhaiResultOf, Shared, AST}; #[cfg(feature = "no_std")] use std::prelude::v1::*; diff --git a/src/optimizer.rs b/src/optimizer.rs index 5fc60ee6..7d4498af 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -1343,7 +1343,7 @@ pub fn optimize_into_ast( let lib2 = &[&lib2]; for fn_def in functions { - let mut fn_def = crate::func::native::shared_take_or_clone(fn_def); + let mut fn_def = crate::func::shared_take_or_clone(fn_def); // Optimize the function body let body = mem::take(&mut *fn_def.body); diff --git a/src/types/dynamic.rs b/src/types/dynamic.rs index ff85dafa..7f21bd85 100644 --- a/src/types/dynamic.rs +++ b/src/types/dynamic.rs @@ -1,7 +1,7 @@ //! Helper module which defines the [`Dynamic`] data type and the //! [`Any`] trait to to allow custom type handling. -use crate::func::native::SendSync; +use crate::func::{locked_read, SendSync}; use crate::{reify, ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, INT}; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -26,7 +26,7 @@ pub use instant::Instant; const CHECKED: &str = "data type was checked"; mod private { - use crate::func::native::SendSync; + use crate::func::SendSync; use std::any::Any; /// A sealed trait that prevents other crates from implementing [`Variant`]. @@ -384,12 +384,7 @@ impl Dynamic { Union::Variant(ref v, ..) => (***v).type_id(), #[cfg(not(feature = "no_closure"))] - #[cfg(not(feature = "sync"))] - Union::Shared(ref cell, ..) => (*cell.borrow()).type_id(), - - #[cfg(not(feature = "no_closure"))] - #[cfg(feature = "sync")] - Union::Shared(ref cell, ..) => (*cell.read().unwrap()).type_id(), + Union::Shared(ref cell, ..) => (*locked_read(cell)).type_id(), } } /// Get the name of the type of the value held by this [`Dynamic`]. @@ -463,12 +458,7 @@ impl Hash for Dynamic { Union::FnPtr(ref f, ..) => f.hash(state), #[cfg(not(feature = "no_closure"))] - #[cfg(not(feature = "sync"))] - Union::Shared(ref cell, ..) => (*cell.borrow()).hash(state), - - #[cfg(not(feature = "no_closure"))] - #[cfg(feature = "sync")] - Union::Shared(ref cell, ..) => (*cell.read().unwrap()).hash(state), + Union::Shared(ref cell, ..) => (*locked_read(cell)).hash(state), Union::Variant(..) => unimplemented!("{} cannot be hashed", self.type_name()), @@ -1025,16 +1015,8 @@ impl Dynamic { match self.0 { Union::Shared(.., ReadOnly) => return true, - #[cfg(not(feature = "sync"))] Union::Shared(ref cell, ..) => { - return match cell.borrow().access_mode() { - ReadWrite => false, - ReadOnly => true, - } - } - #[cfg(feature = "sync")] - Union::Shared(ref cell, ..) => { - return match cell.read().unwrap().access_mode() { + return match locked_read(cell).access_mode() { ReadWrite => false, ReadOnly => true, } @@ -1066,12 +1048,7 @@ impl Dynamic { Union::Map(..) => true, #[cfg(not(feature = "no_closure"))] - #[cfg(not(feature = "sync"))] - Union::Shared(ref cell, ..) => cell.borrow().is_hashable(), - - #[cfg(not(feature = "no_closure"))] - #[cfg(feature = "sync")] - Union::Shared(ref cell, ..) => cell.read().unwrap().is_hashable(), + Union::Shared(ref cell, ..) => locked_read(cell).is_hashable(), _ => false, } @@ -1322,11 +1299,7 @@ impl Dynamic { pub fn flatten_clone(&self) -> Self { match self.0 { #[cfg(not(feature = "no_closure"))] - #[cfg(not(feature = "sync"))] - Union::Shared(ref cell, ..) => cell.borrow().clone(), - #[cfg(not(feature = "no_closure"))] - #[cfg(feature = "sync")] - Union::Shared(ref cell, ..) => cell.read().unwrap().clone(), + Union::Shared(ref cell, ..) => locked_read(cell).clone(), _ => self.clone(), } } @@ -1341,12 +1314,8 @@ impl Dynamic { pub fn flatten(self) -> Self { match self.0 { #[cfg(not(feature = "no_closure"))] - Union::Shared(cell, ..) => crate::func::native::shared_try_take(cell).map_or_else( - #[cfg(not(feature = "sync"))] - |cell| cell.borrow().clone(), - #[cfg(feature = "sync")] - |cell| cell.read().unwrap().clone(), - #[cfg(not(feature = "sync"))] + Union::Shared(cell, ..) => crate::func::shared_try_take(cell).map_or_else( + |ref cell| locked_read(cell).clone(), |value| value.into_inner(), #[cfg(feature = "sync")] |value| value.into_inner().unwrap(), @@ -1366,11 +1335,8 @@ impl Dynamic { #[cfg(not(feature = "no_closure"))] Union::Shared(ref mut cell, ..) => { let cell = mem::take(cell); - *self = crate::func::native::shared_try_take(cell).map_or_else( - #[cfg(not(feature = "sync"))] - |cell| cell.borrow().clone(), - #[cfg(feature = "sync")] - |cell| cell.read().unwrap().clone(), + *self = crate::func::shared_try_take(cell).map_or_else( + |ref cell| locked_read(cell).clone(), #[cfg(not(feature = "sync"))] |value| value.into_inner(), #[cfg(feature = "sync")] @@ -1422,10 +1388,7 @@ impl Dynamic { match self.0 { #[cfg(not(feature = "no_closure"))] Union::Shared(ref cell, ..) => { - #[cfg(not(feature = "sync"))] - let value = cell.borrow(); - #[cfg(feature = "sync")] - let value = cell.read().unwrap(); + let value = locked_read(cell); if (*value).type_id() != TypeId::of::() && TypeId::of::() != TypeId::of::() @@ -1457,7 +1420,7 @@ impl Dynamic { match self.0 { #[cfg(not(feature = "no_closure"))] Union::Shared(ref cell, ..) => { - let guard = crate::func::native::locked_write(cell); + let guard = crate::func::locked_write(cell); if (*guard).type_id() != TypeId::of::() && TypeId::of::() != TypeId::of::() @@ -1772,11 +1735,8 @@ impl Dynamic { match self.0 { Union::Str(s, ..) => Ok(s), #[cfg(not(feature = "no_closure"))] - Union::Shared(cell, ..) => { - #[cfg(not(feature = "sync"))] - let value = cell.borrow(); - #[cfg(feature = "sync")] - let value = cell.read().unwrap(); + Union::Shared(ref cell, ..) => { + let value = locked_read(cell); match value.0 { Union::Str(ref s, ..) => Ok(s.clone()), @@ -1794,11 +1754,8 @@ impl Dynamic { match self.0 { Union::Array(a, ..) => Ok(*a), #[cfg(not(feature = "no_closure"))] - Union::Shared(cell, ..) => { - #[cfg(not(feature = "sync"))] - let value = cell.borrow(); - #[cfg(feature = "sync")] - let value = cell.read().unwrap(); + Union::Shared(ref cell, ..) => { + let value = locked_read(cell); match value.0 { Union::Array(ref a, ..) => Ok(a.as_ref().clone()), @@ -1832,11 +1789,8 @@ impl Dynamic { .collect(), Union::Blob(..) if TypeId::of::() == TypeId::of::() => Ok(self.cast::>()), #[cfg(not(feature = "no_closure"))] - Union::Shared(cell, ..) => { - #[cfg(not(feature = "sync"))] - let value = cell.borrow(); - #[cfg(feature = "sync")] - let value = cell.read().unwrap(); + Union::Shared(ref cell, ..) => { + let value = locked_read(cell); match value.0 { Union::Array(ref a, ..) => { @@ -1873,11 +1827,8 @@ impl Dynamic { match self.0 { Union::Blob(a, ..) => Ok(*a), #[cfg(not(feature = "no_closure"))] - Union::Shared(cell, ..) => { - #[cfg(not(feature = "sync"))] - let value = cell.borrow(); - #[cfg(feature = "sync")] - let value = cell.read().unwrap(); + Union::Shared(ref cell, ..) => { + let value = locked_read(cell); match value.0 { Union::Blob(ref a, ..) => Ok(a.as_ref().clone()), diff --git a/src/types/immutable_string.rs b/src/types/immutable_string.rs index e959b83c..85cd4127 100644 --- a/src/types/immutable_string.rs +++ b/src/types/immutable_string.rs @@ -1,6 +1,6 @@ //! The `ImmutableString` type. -use crate::func::native::{shared_get_mut, shared_make_mut, shared_take}; +use crate::func::{shared_get_mut, shared_make_mut, shared_take}; use crate::{Shared, SmartString}; #[cfg(feature = "no_std")] use std::prelude::v1::*;