Fix builds.

This commit is contained in:
Stephen Chung 2021-11-08 11:35:46 +08:00
parent 09e6b21729
commit cc6a0571e7
5 changed files with 50 additions and 62 deletions

View File

@ -1,6 +1,6 @@
//! Helper module which defines the [`Any`] trait to to allow dynamic value handling. //! 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::r#unsafe::{unsafe_cast_box, unsafe_try_cast};
use crate::{FnPtr, ImmutableString, INT}; use crate::{FnPtr, ImmutableString, INT};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
@ -283,14 +283,11 @@ enum DynamicWriteLockInner<'d, T: Clone> {
/// A simple mutable reference to a non-shared value. /// A simple mutable reference to a non-shared value.
Reference(&'d mut T), 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 = "no_closure"))]
#[cfg(not(feature = "sync"))] Guard(crate::fn_native::LockGuard<'d, Dynamic>),
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>),
} }
impl<'d, T: Any + Clone> Deref for DynamicWriteLock<'d, T> { impl<'d, T: Any + Clone> Deref for DynamicWriteLock<'d, T> {
@ -1593,10 +1590,7 @@ impl Dynamic {
match self.0 { match self.0 {
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
Union::Shared(ref cell, _, _) => { Union::Shared(ref cell, _, _) => {
#[cfg(not(feature = "sync"))] let value = shared_write_lock(cell);
let value = cell.borrow_mut();
#[cfg(feature = "sync")]
let value = cell.write().unwrap();
if (*value).type_id() != TypeId::of::<T>() if (*value).type_id() != TypeId::of::<T>()
&& TypeId::of::<Dynamic>() != TypeId::of::<T>() && TypeId::of::<Dynamic>() != TypeId::of::<T>()

View File

@ -5,16 +5,16 @@ use crate::custom_syntax::CustomSyntax;
use crate::dynamic::{map_std_type_name, AccessMode, Union, Variant}; use crate::dynamic::{map_std_type_name, AccessMode, Union, Variant};
use crate::fn_hash::get_hasher; use crate::fn_hash::get_hasher;
use crate::fn_native::{ use crate::fn_native::{
CallableFunction, IteratorFn, OnDebugCallback, OnParseTokenCallback, OnPrintCallback, shared_write_lock, CallableFunction, IteratorFn, Locked, OnDebugCallback, OnParseTokenCallback,
OnVarCallback, OnPrintCallback, OnVarCallback,
}; };
use crate::module::NamespaceRef; use crate::module::NamespaceRef;
use crate::packages::{Package, StandardPackage}; use crate::packages::{Package, StandardPackage};
use crate::r#unsafe::unsafe_cast_var_name_to_lifetime; use crate::r#unsafe::unsafe_cast_var_name_to_lifetime;
use crate::token::Token; use crate::token::Token;
use crate::{ use crate::{
Dynamic, EvalAltResult, Identifier, ImmutableString, Locked, Module, Position, RhaiResult, Dynamic, EvalAltResult, Identifier, ImmutableString, Module, Position, RhaiResult, Scope,
Scope, Shared, StaticVec, INT, Shared, StaticVec, INT,
}; };
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -205,12 +205,8 @@ impl Imports {
pub(crate) fn global_constants_mut<'a>( pub(crate) fn global_constants_mut<'a>(
&'a mut self, &'a mut self,
) -> Option<impl DerefMut<Target = BTreeMap<Identifier, Dynamic>> + 'a> { ) -> Option<impl DerefMut<Target = BTreeMap<Identifier, Dynamic>> + 'a> {
if let Some(ref mut global_constants) = self.global_constants { if let Some(ref global_constants) = self.global_constants {
#[cfg(not(feature = "sync"))] Some(shared_write_lock(global_constants))
return Some(global_constants.borrow_mut());
#[cfg(feature = "sync")]
return Some(global_constants.lock().unwrap());
} else { } else {
None None
} }
@ -222,19 +218,12 @@ impl Imports {
self.global_constants = Some(dict.into()); self.global_constants = Some(dict.into());
} }
#[cfg(not(feature = "sync"))] shared_write_lock(
self.global_constants self.global_constants
.as_mut() .as_mut()
.unwrap() .expect("`global_constants` is `Some`"),
.borrow_mut() )
.insert(name.into(), value); .insert(name.into(), value);
#[cfg(feature = "sync")]
self.global_constants
.as_mut()
.lock()
.unwrap()
.insert(name.into(), value);
} }
/// Get the pre-calculated index getter hash. /// Get the pre-calculated index getter hash.
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]

View File

@ -2211,8 +2211,6 @@ impl Engine {
+ SendSync + SendSync
+ 'static, + 'static,
) -> &mut Self { ) -> &mut Self {
use std::string::ParseError;
self.token_mapper = Some(Box::new(callback)); self.token_mapper = Some(Box::new(callback));
self self
} }

View File

@ -39,6 +39,14 @@ pub use std::sync::Arc as Shared;
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
pub use std::cell::RefCell as Locked; 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. /// Synchronized shared object.
/// ///
/// Not available under `no_closure`. /// Not available under `no_closure`.
@ -46,6 +54,13 @@ pub use std::cell::RefCell as Locked;
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
pub use std::sync::RwLock as Locked; 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. /// Context of a native Rust function call.
#[derive(Debug)] #[derive(Debug)]
pub struct NativeCallContext<'a> { pub struct NativeCallContext<'a> {
@ -292,6 +307,16 @@ pub fn shared_take<T>(value: Shared<T>) -> T {
.expect("no outstanding references") .expect("no outstanding references")
} }
#[inline(always)]
#[must_use]
pub fn shared_write_lock<'a, T>(value: &'a Locked<T>) -> LockGuard<'a, T> {
#[cfg(not(feature = "sync"))]
return value.borrow_mut();
#[cfg(feature = "sync")]
return value.write().unwrap();
}
/// A general function trail object. /// A general function trail object.
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
pub type FnAny = dyn Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult; pub type FnAny = dyn Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult;

View File

@ -1,4 +1,6 @@
use crate::fn_native::shared_write_lock;
use crate::{Engine, EvalAltResult, Identifier, Module, ModuleResolver, Position, Scope, Shared}; use crate::{Engine, EvalAltResult, Identifier, Module, ModuleResolver, Position, Scope, Shared};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
use std::prelude::v1::*; use std::prelude::v1::*;
use std::{ use std::{
@ -202,19 +204,12 @@ impl FileModuleResolver {
let file_path = self.get_file_path(path, source_path); let file_path = self.get_file_path(path, source_path);
#[cfg(not(feature = "sync"))] shared_write_lock(&self.cache).contains_key(&file_path)
return self.cache.borrow_mut().contains_key(&file_path);
#[cfg(feature = "sync")]
return self.cache.write().unwrap().contains_key(&file_path);
} }
/// Empty the internal cache. /// Empty the internal cache.
#[inline] #[inline]
pub fn clear_cache(&mut self) -> &mut Self { pub fn clear_cache(&mut self) -> &mut Self {
#[cfg(not(feature = "sync"))] shared_write_lock(&self.cache).clear();
self.cache.borrow_mut().clear();
#[cfg(feature = "sync")]
self.cache.write().unwrap().clear();
self self
} }
/// Remove the specified path from internal cache. /// Remove the specified path from internal cache.
@ -229,19 +224,9 @@ impl FileModuleResolver {
) -> Option<Shared<Module>> { ) -> Option<Shared<Module>> {
let file_path = self.get_file_path(path, source_path); let file_path = self.get_file_path(path, source_path);
#[cfg(not(feature = "sync"))] shared_write_lock(&self.cache)
return self
.cache
.borrow_mut()
.remove_entry(&file_path) .remove_entry(&file_path)
.map(|(_, v)| v); .map(|(_, v)| v)
#[cfg(feature = "sync")]
return self
.cache
.write()
.unwrap()
.remove_entry(&file_path)
.map(|(_, v)| v);
} }
/// Construct a full file path. /// Construct a full file path.
#[must_use] #[must_use]
@ -314,10 +299,7 @@ impl ModuleResolver for FileModuleResolver {
// Put it into the cache // Put it into the cache
if self.is_cache_enabled() { if self.is_cache_enabled() {
#[cfg(not(feature = "sync"))] shared_write_lock(&self.cache).insert(file_path, m.clone());
self.cache.borrow_mut().insert(file_path, m.clone());
#[cfg(feature = "sync")]
self.cache.write().unwrap().insert(file_path, m.clone());
} }
Ok(m) Ok(m)