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.
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::<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::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<impl DerefMut<Target = BTreeMap<Identifier, Dynamic>> + '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")))]

View File

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

View File

@ -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<T>(value: Shared<T>) -> T {
.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.
#[cfg(not(feature = "sync"))]
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};
#[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<Shared<Module>> {
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)