Fix builds.

This commit is contained in:
Stephen Chung 2021-11-08 12:07:49 +08:00
parent cc6a0571e7
commit 31ef7e6c69
4 changed files with 32 additions and 22 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::{shared_write_lock, SendSync}; use crate::fn_native::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")]
@ -1590,7 +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, _, _) => {
let value = shared_write_lock(cell); let value = crate::fn_native::shared_write_lock(cell);
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,8 +5,8 @@ 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::{
shared_write_lock, CallableFunction, IteratorFn, Locked, OnDebugCallback, OnParseTokenCallback, CallableFunction, IteratorFn, OnDebugCallback, OnParseTokenCallback, OnPrintCallback,
OnPrintCallback, OnVarCallback, OnVarCallback,
}; };
use crate::module::NamespaceRef; use crate::module::NamespaceRef;
use crate::packages::{Package, StandardPackage}; use crate::packages::{Package, StandardPackage};
@ -73,10 +73,14 @@ pub struct Imports {
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
fn_hash_indexing: (u64, u64), fn_hash_indexing: (u64, u64),
/// Embedded module resolver. /// Embedded module resolver.
///
/// Not available under `no_module`.
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
pub embedded_module_resolver: Option<Shared<crate::module::resolvers::StaticModuleResolver>>, pub embedded_module_resolver: Option<Shared<crate::module::resolvers::StaticModuleResolver>>,
/// Cache of globally-defined constants. /// Cache of globally-defined constants.
global_constants: Option<Shared<Locked<BTreeMap<Identifier, Dynamic>>>>, #[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
global_constants: Option<Shared<crate::Locked<BTreeMap<Identifier, Dynamic>>>>,
} }
impl Imports { impl Imports {
@ -94,6 +98,8 @@ impl Imports {
embedded_module_resolver: None, embedded_module_resolver: None,
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
fn_hash_indexing: (0, 0), fn_hash_indexing: (0, 0),
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
global_constants: None, global_constants: None,
} }
} }
@ -201,24 +207,28 @@ impl Imports {
.find_map(|m| m.get_qualified_iter(id)) .find_map(|m| m.get_qualified_iter(id))
} }
/// Get a mutable reference to the cache of globally-defined constants. /// Get a mutable reference to the cache of globally-defined constants.
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
#[must_use] #[must_use]
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 global_constants) = self.global_constants { if let Some(ref global_constants) = self.global_constants {
Some(shared_write_lock(global_constants)) Some(crate::fn_native::shared_write_lock(global_constants))
} else { } else {
None None
} }
} }
/// Set a constant into the cache of globally-defined constants. /// Set a constant into the cache of globally-defined constants.
#[cfg(not(feature = "no_module"))]
#[cfg(not(feature = "no_function"))]
pub(crate) fn set_global_constant(&mut self, name: &str, value: Dynamic) { pub(crate) fn set_global_constant(&mut self, name: &str, value: Dynamic) {
if self.global_constants.is_none() { if self.global_constants.is_none() {
let dict: Locked<_> = BTreeMap::new().into(); let dict: crate::Locked<_> = BTreeMap::new().into();
self.global_constants = Some(dict.into()); self.global_constants = Some(dict.into());
} }
shared_write_lock( crate::fn_native::shared_write_lock(
self.global_constants self.global_constants
.as_mut() .as_mut()
.expect("`global_constants` is `Some`"), .expect("`global_constants` is `Some`"),
@ -1204,6 +1214,7 @@ impl Engine {
Expr::Variable(None, _var_pos, v) => match v.as_ref() { Expr::Variable(None, _var_pos, v) => match v.as_ref() {
// Normal variable access // Normal variable access
(_, None, _) => self.search_scope_only(scope, mods, state, lib, this_ptr, expr), (_, None, _) => self.search_scope_only(scope, mods, state, lib, this_ptr, expr),
// Qualified variable access // Qualified variable access
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
(_, Some((namespace, hash_var)), var_name) => { (_, Some((namespace, hash_var)), var_name) => {
@ -1265,6 +1276,7 @@ impl Engine {
.into(), .into(),
) )
} }
#[cfg(feature = "no_module")] #[cfg(feature = "no_module")]
(_, Some((_, _)), _) => unreachable!("qualified access under no_module"), (_, Some((_, _)), _) => unreachable!("qualified access under no_module"),
}, },

View File

@ -34,31 +34,23 @@ pub use std::rc::Rc as Shared;
pub use std::sync::Arc as Shared; pub use std::sync::Arc as Shared;
/// Synchronized shared object. /// Synchronized shared object.
///
/// Not available under `no_closure`.
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
#[allow(dead_code)]
pub use std::cell::RefCell as Locked; pub use std::cell::RefCell as Locked;
/// Lock guard for synchronized shared object. /// Lock guard for synchronized shared object.
///
/// Not available under `no_closure`.
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
#[allow(dead_code)]
pub type LockGuard<'a, T> = std::cell::RefMut<'a, T>; pub type LockGuard<'a, T> = std::cell::RefMut<'a, T>;
/// Synchronized shared object. /// Synchronized shared object.
///
/// Not available under `no_closure`.
#[cfg(not(feature = "no_closure"))]
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
#[allow(dead_code)]
pub use std::sync::RwLock as Locked; pub use std::sync::RwLock as Locked;
/// Lock guard for synchronized shared object. /// Lock guard for synchronized shared object.
///
/// Not available under `no_closure`.
#[cfg(not(feature = "no_closure"))]
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
#[allow(dead_code)]
pub type LockGuard<'a, T> = std::sync::RwLockWriteGuard<'a, T>; pub type LockGuard<'a, T> = std::sync::RwLockWriteGuard<'a, T>;
/// Context of a native Rust function call. /// Context of a native Rust function call.
@ -277,6 +269,7 @@ impl<'a> NativeCallContext<'a> {
/// If the resource is shared (i.e. has other outstanding references), a cloned copy is used. /// If the resource is shared (i.e. has other outstanding references), a cloned copy is used.
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
#[allow(dead_code)]
pub fn shared_make_mut<T: Clone>(value: &mut Shared<T>) -> &mut T { pub fn shared_make_mut<T: Clone>(value: &mut Shared<T>) -> &mut T {
Shared::make_mut(value) Shared::make_mut(value)
} }
@ -284,12 +277,14 @@ pub fn shared_make_mut<T: Clone>(value: &mut Shared<T>) -> &mut T {
/// Consume a [`Shared`] resource if is unique (i.e. not shared), or clone it otherwise. /// Consume a [`Shared`] resource if is unique (i.e. not shared), or clone it otherwise.
#[inline] #[inline]
#[must_use] #[must_use]
#[allow(dead_code)]
pub fn shared_take_or_clone<T: Clone>(value: Shared<T>) -> T { pub fn shared_take_or_clone<T: Clone>(value: Shared<T>) -> T {
shared_try_take(value).unwrap_or_else(|v| v.as_ref().clone()) shared_try_take(value).unwrap_or_else(|v| v.as_ref().clone())
} }
/// Consume a [`Shared`] resource if is unique (i.e. not shared). /// Consume a [`Shared`] resource if is unique (i.e. not shared).
#[inline(always)] #[inline(always)]
#[allow(dead_code)]
pub fn shared_try_take<T>(value: Shared<T>) -> Result<T, Shared<T>> { pub fn shared_try_take<T>(value: Shared<T>) -> Result<T, Shared<T>> {
Shared::try_unwrap(value) Shared::try_unwrap(value)
} }
@ -301,14 +296,17 @@ pub fn shared_try_take<T>(value: Shared<T>) -> Result<T, Shared<T>> {
/// Panics if the resource is shared (i.e. has other outstanding references). /// Panics if the resource is shared (i.e. has other outstanding references).
#[inline] #[inline]
#[must_use] #[must_use]
#[allow(dead_code)]
pub fn shared_take<T>(value: Shared<T>) -> T { pub fn shared_take<T>(value: Shared<T>) -> T {
shared_try_take(value) shared_try_take(value)
.ok() .ok()
.expect("no outstanding references") .expect("no outstanding references")
} }
/// Lock a [`Shared`] resource.
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
#[allow(dead_code)]
pub fn shared_write_lock<'a, T>(value: &'a Locked<T>) -> LockGuard<'a, T> { pub fn shared_write_lock<'a, T>(value: &'a Locked<T>) -> LockGuard<'a, T> {
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
return value.borrow_mut(); return value.borrow_mut();

View File

@ -171,8 +171,8 @@ pub type Identifier = ImmutableString;
/// Alias to [`Rc`][std::rc::Rc] or [`Arc`][std::sync::Arc] depending on the `sync` feature flag. /// Alias to [`Rc`][std::rc::Rc] or [`Arc`][std::sync::Arc] depending on the `sync` feature flag.
pub use fn_native::Shared; pub use fn_native::Shared;
#[cfg(not(feature = "no_closure"))] //// Alias to [`RefCell`][std::cell::RefCell] or [`RwLock`][std::sync::RwLock] depending on the `sync` feature flag.
use fn_native::Locked; pub use fn_native::Locked;
pub(crate) use fn_hash::{ pub(crate) use fn_hash::{
calc_fn_hash, calc_fn_params_hash, calc_qualified_fn_hash, calc_qualified_var_hash, calc_fn_hash, calc_fn_params_hash, calc_qualified_fn_hash, calc_qualified_var_hash,