From cc6a0571e700bd4543c80f09a2f120a7e6f22852 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 8 Nov 2021 11:35:46 +0800 Subject: [PATCH] Fix builds. --- src/dynamic.rs | 18 ++++++------------ src/engine.rs | 35 ++++++++++++----------------------- src/engine_api.rs | 2 -- src/fn_native.rs | 25 +++++++++++++++++++++++++ src/module/resolvers/file.rs | 32 +++++++------------------------- 5 files changed, 50 insertions(+), 62 deletions(-) diff --git a/src/dynamic.rs b/src/dynamic.rs index 76d99ad9..b3eefbc8 100644 --- a/src/dynamic.rs +++ b/src/dynamic.rs @@ -1,6 +1,6 @@ //! Helper module which defines the [`Any`] trait to to allow dynamic value handling. -use crate::fn_native::SendSync; +use crate::fn_native::{shared_write_lock, SendSync}; use crate::r#unsafe::{unsafe_cast_box, unsafe_try_cast}; use crate::{FnPtr, ImmutableString, INT}; #[cfg(feature = "no_std")] @@ -283,14 +283,11 @@ enum DynamicWriteLockInner<'d, T: Clone> { /// A simple mutable reference to a non-shared value. Reference(&'d mut T), - /// A write guard to a shared [`RefCell`][std::cell::RefCell]. + /// A write guard to a shared value. + /// + /// Not available under `no_closure`. #[cfg(not(feature = "no_closure"))] - #[cfg(not(feature = "sync"))] - Guard(std::cell::RefMut<'d, Dynamic>), - /// A write guard to a shared [`RwLock`][std::sync::RwLock]. - #[cfg(not(feature = "no_closure"))] - #[cfg(feature = "sync")] - Guard(std::sync::RwLockWriteGuard<'d, Dynamic>), + Guard(crate::fn_native::LockGuard<'d, Dynamic>), } impl<'d, T: Any + Clone> Deref for DynamicWriteLock<'d, T> { @@ -1593,10 +1590,7 @@ impl Dynamic { match self.0 { #[cfg(not(feature = "no_closure"))] Union::Shared(ref cell, _, _) => { - #[cfg(not(feature = "sync"))] - let value = cell.borrow_mut(); - #[cfg(feature = "sync")] - let value = cell.write().unwrap(); + let value = shared_write_lock(cell); if (*value).type_id() != TypeId::of::() && TypeId::of::() != TypeId::of::() diff --git a/src/engine.rs b/src/engine.rs index b3e27d94..2a762cf3 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -5,16 +5,16 @@ use crate::custom_syntax::CustomSyntax; use crate::dynamic::{map_std_type_name, AccessMode, Union, Variant}; use crate::fn_hash::get_hasher; use crate::fn_native::{ - CallableFunction, IteratorFn, OnDebugCallback, OnParseTokenCallback, OnPrintCallback, - OnVarCallback, + shared_write_lock, CallableFunction, IteratorFn, Locked, OnDebugCallback, OnParseTokenCallback, + OnPrintCallback, OnVarCallback, }; use crate::module::NamespaceRef; use crate::packages::{Package, StandardPackage}; use crate::r#unsafe::unsafe_cast_var_name_to_lifetime; use crate::token::Token; use crate::{ - Dynamic, EvalAltResult, Identifier, ImmutableString, Locked, Module, Position, RhaiResult, - Scope, Shared, StaticVec, INT, + Dynamic, EvalAltResult, Identifier, ImmutableString, Module, Position, RhaiResult, Scope, + Shared, StaticVec, INT, }; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -205,12 +205,8 @@ impl Imports { pub(crate) fn global_constants_mut<'a>( &'a mut self, ) -> Option> + 'a> { - if let Some(ref mut global_constants) = self.global_constants { - #[cfg(not(feature = "sync"))] - return Some(global_constants.borrow_mut()); - - #[cfg(feature = "sync")] - return Some(global_constants.lock().unwrap()); + if let Some(ref global_constants) = self.global_constants { + Some(shared_write_lock(global_constants)) } else { None } @@ -222,19 +218,12 @@ impl Imports { self.global_constants = Some(dict.into()); } - #[cfg(not(feature = "sync"))] - self.global_constants - .as_mut() - .unwrap() - .borrow_mut() - .insert(name.into(), value); - - #[cfg(feature = "sync")] - self.global_constants - .as_mut() - .lock() - .unwrap() - .insert(name.into(), value); + shared_write_lock( + self.global_constants + .as_mut() + .expect("`global_constants` is `Some`"), + ) + .insert(name.into(), value); } /// Get the pre-calculated index getter hash. #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] diff --git a/src/engine_api.rs b/src/engine_api.rs index 8bf946df..60a5d94b 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -2211,8 +2211,6 @@ impl Engine { + SendSync + 'static, ) -> &mut Self { - use std::string::ParseError; - self.token_mapper = Some(Box::new(callback)); self } diff --git a/src/fn_native.rs b/src/fn_native.rs index ef706800..fd3063ff 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -39,6 +39,14 @@ pub use std::sync::Arc as Shared; #[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "sync"))] pub use std::cell::RefCell as Locked; + +/// Lock guard for synchronized shared object. +/// +/// Not available under `no_closure`. +#[cfg(not(feature = "no_closure"))] +#[cfg(not(feature = "sync"))] +pub type LockGuard<'a, T> = std::cell::RefMut<'a, T>; + /// Synchronized shared object. /// /// Not available under `no_closure`. @@ -46,6 +54,13 @@ pub use std::cell::RefCell as Locked; #[cfg(feature = "sync")] pub use std::sync::RwLock as Locked; +/// Lock guard for synchronized shared object. +/// +/// Not available under `no_closure`. +#[cfg(not(feature = "no_closure"))] +#[cfg(feature = "sync")] +pub type LockGuard<'a, T> = std::sync::RwLockWriteGuard<'a, T>; + /// Context of a native Rust function call. #[derive(Debug)] pub struct NativeCallContext<'a> { @@ -292,6 +307,16 @@ pub fn shared_take(value: Shared) -> T { .expect("no outstanding references") } +#[inline(always)] +#[must_use] +pub fn shared_write_lock<'a, T>(value: &'a Locked) -> LockGuard<'a, T> { + #[cfg(not(feature = "sync"))] + return value.borrow_mut(); + + #[cfg(feature = "sync")] + return value.write().unwrap(); +} + /// A general function trail object. #[cfg(not(feature = "sync"))] pub type FnAny = dyn Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult; diff --git a/src/module/resolvers/file.rs b/src/module/resolvers/file.rs index 09bd7e4d..1cf096f8 100644 --- a/src/module/resolvers/file.rs +++ b/src/module/resolvers/file.rs @@ -1,4 +1,6 @@ +use crate::fn_native::shared_write_lock; use crate::{Engine, EvalAltResult, Identifier, Module, ModuleResolver, Position, Scope, Shared}; + #[cfg(feature = "no_std")] use std::prelude::v1::*; use std::{ @@ -202,19 +204,12 @@ impl FileModuleResolver { let file_path = self.get_file_path(path, source_path); - #[cfg(not(feature = "sync"))] - return self.cache.borrow_mut().contains_key(&file_path); - #[cfg(feature = "sync")] - return self.cache.write().unwrap().contains_key(&file_path); + shared_write_lock(&self.cache).contains_key(&file_path) } /// Empty the internal cache. #[inline] pub fn clear_cache(&mut self) -> &mut Self { - #[cfg(not(feature = "sync"))] - self.cache.borrow_mut().clear(); - #[cfg(feature = "sync")] - self.cache.write().unwrap().clear(); - + shared_write_lock(&self.cache).clear(); self } /// Remove the specified path from internal cache. @@ -229,19 +224,9 @@ impl FileModuleResolver { ) -> Option> { let file_path = self.get_file_path(path, source_path); - #[cfg(not(feature = "sync"))] - return self - .cache - .borrow_mut() + shared_write_lock(&self.cache) .remove_entry(&file_path) - .map(|(_, v)| v); - #[cfg(feature = "sync")] - return self - .cache - .write() - .unwrap() - .remove_entry(&file_path) - .map(|(_, v)| v); + .map(|(_, v)| v) } /// Construct a full file path. #[must_use] @@ -314,10 +299,7 @@ impl ModuleResolver for FileModuleResolver { // Put it into the cache if self.is_cache_enabled() { - #[cfg(not(feature = "sync"))] - self.cache.borrow_mut().insert(file_path, m.clone()); - #[cfg(feature = "sync")] - self.cache.write().unwrap().insert(file_path, m.clone()); + shared_write_lock(&self.cache).insert(file_path, m.clone()); } Ok(m)