Hide is_shared and is_locked under feature gates.

This commit is contained in:
Stephen Chung 2021-04-17 12:03:29 +08:00
parent 6eaee57578
commit 6c92011ea1
7 changed files with 52 additions and 18 deletions

View File

@ -4,6 +4,11 @@ Rhai Release Notes
Version 0.20.1 Version 0.20.1
============== ==============
Breaking changes
----------------
* `Dynamic::is_shared` and `Dynamic::is_locked` are removed under the `no_closure` feature. They used to always return `false`.
Version 0.20.0 Version 0.20.0
============== ==============

View File

@ -90,6 +90,7 @@ impl fmt::Display for ScriptFnDef {
} }
/// A type containing the metadata of a script-defined function. /// A type containing the metadata of a script-defined function.
///
/// Not available under `no_function`. /// Not available under `no_function`.
/// ///
/// Created by [`AST::iter_functions`]. /// Created by [`AST::iter_functions`].
@ -265,6 +266,7 @@ impl AST {
} }
/// _(INTERNALS)_ Get the internal shared [`Module`] containing all script-defined functions. /// _(INTERNALS)_ Get the internal shared [`Module`] containing all script-defined functions.
/// Exported under the `internals` feature only. /// Exported under the `internals` feature only.
///
/// Not available under `no_function`. /// Not available under `no_function`.
#[cfg(feature = "internals")] #[cfg(feature = "internals")]
#[deprecated = "this method is volatile and may change"] #[deprecated = "this method is volatile and may change"]
@ -282,6 +284,7 @@ impl AST {
} }
/// _(INTERNALS)_ Get the internal [`Module`] containing all script-defined functions. /// _(INTERNALS)_ Get the internal [`Module`] containing all script-defined functions.
/// Exported under the `internals` feature only. /// Exported under the `internals` feature only.
///
/// Not available under `no_function`. /// Not available under `no_function`.
#[cfg(feature = "internals")] #[cfg(feature = "internals")]
#[deprecated = "this method is volatile and may change"] #[deprecated = "this method is volatile and may change"]
@ -318,6 +321,7 @@ impl AST {
} }
/// Clone the [`AST`]'s functions into a new [`AST`]. /// Clone the [`AST`]'s functions into a new [`AST`].
/// No statements are cloned. /// No statements are cloned.
///
/// Not available under `no_function`. /// Not available under `no_function`.
/// ///
/// This operation is cheap because functions are shared. /// This operation is cheap because functions are shared.
@ -328,6 +332,7 @@ impl AST {
} }
/// Clone the [`AST`]'s functions into a new [`AST`] based on a filter predicate. /// Clone the [`AST`]'s functions into a new [`AST`] based on a filter predicate.
/// No statements are cloned. /// No statements are cloned.
///
/// Not available under `no_function`. /// Not available under `no_function`.
/// ///
/// This operation is cheap because functions are shared. /// This operation is cheap because functions are shared.
@ -650,7 +655,7 @@ impl AST {
} }
/// Iterate through all function definitions. /// Iterate through all function definitions.
/// ///
/// Not available under [`no_function`]. /// Not available under `no_function`.
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
#[inline(always)] #[inline(always)]

View File

@ -205,12 +205,17 @@ fn main() {
.iter_raw() .iter_raw()
.enumerate() .enumerate()
.for_each(|(i, (name, constant, value))| { .for_each(|(i, (name, constant, value))| {
#[cfg(not(feature = "no_closure"))]
let value_is_shared = if value.is_shared() { " (shared" } else { "" };
#[cfg(feature = "no_closure")]
let value_is_shared = "";
println!( println!(
"[{}] {}{}{} = {:?}", "[{}] {}{}{} = {:?}",
i + 1, i + 1,
if constant { "const " } else { "" }, if constant { "const " } else { "" },
name, name,
if value.is_shared() { " (shared)" } else { "" }, value_is_shared,
*value.read_lock::<Dynamic>().unwrap(), *value.read_lock::<Dynamic>().unwrap(),
) )
}); });

View File

@ -281,7 +281,8 @@ impl Dynamic {
} }
/// Is the value held by this [`Dynamic`] shared? /// Is the value held by this [`Dynamic`] shared?
/// ///
/// Always [`false`] under the `no_closure` feature. /// Not available under `no_closure`.
#[cfg(not(feature = "no_closure"))]
#[inline(always)] #[inline(always)]
pub fn is_shared(&self) -> bool { pub fn is_shared(&self) -> bool {
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
@ -948,10 +949,6 @@ impl Dynamic {
/// values. /// values.
/// ///
/// If the [`Dynamic`] value is already shared, this method returns itself. /// If the [`Dynamic`] value is already shared, this method returns itself.
///
/// # Panics
///
/// Panics under the `no_closure` feature.
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
#[inline(always)] #[inline(always)]
pub fn into_shared(self) -> Self { pub fn into_shared(self) -> Self {
@ -1122,12 +1119,15 @@ impl Dynamic {
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn cast<T: Variant + Clone>(self) -> T { pub fn cast<T: Variant + Clone>(self) -> T {
#[cfg(not(feature = "no_closure"))]
let self_type_name = if self.is_shared() { let self_type_name = if self.is_shared() {
// Avoid panics/deadlocks with shared values // Avoid panics/deadlocks with shared values
"<shared>" "<shared>"
} else { } else {
self.type_name() self.type_name()
}; };
#[cfg(feature = "no_closure")]
let self_type_name = self.type_name();
self.try_cast::<T>().unwrap_or_else(|| { self.try_cast::<T>().unwrap_or_else(|| {
panic!( panic!(
@ -1259,11 +1259,14 @@ impl Dynamic {
} }
/// Is the [`Dynamic`] a shared value that is locked? /// Is the [`Dynamic`] a shared value that is locked?
/// ///
/// Not available under `no_closure`.
///
/// ## Note /// ## Note
/// ///
/// Under the `sync` feature, shared values use [`RwLock`][std::sync::RwLock] and they are never locked. /// Under the `sync` feature, shared values use [`RwLock`][std::sync::RwLock] and they are never locked.
/// Access just waits until the [`RwLock`][std::sync::RwLock] is released. /// Access just waits until the [`RwLock`][std::sync::RwLock] is released.
/// So this method always returns [`false`] under [`Sync`]. /// So this method always returns [`false`] under [`Sync`].
#[cfg(not(feature = "no_closure"))]
#[inline(always)] #[inline(always)]
pub fn is_locked(&self) -> bool { pub fn is_locked(&self) -> bool {
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]

View File

@ -344,7 +344,7 @@ impl<'a> Target<'a> {
} }
} }
/// Is the `Target` a shared value? /// Is the `Target` a shared value?
#[allow(dead_code)] #[cfg(not(feature = "no_closure"))]
#[inline(always)] #[inline(always)]
pub fn is_shared(&self) -> bool { pub fn is_shared(&self) -> bool {
match self { match self {
@ -1990,7 +1990,12 @@ impl Engine {
let mut lock_guard; let mut lock_guard;
let lhs_ptr_inner; let lhs_ptr_inner;
if cfg!(not(feature = "no_closure")) && target.is_shared() { #[cfg(not(feature = "no_closure"))]
let target_is_shared = target.is_shared();
#[cfg(feature = "no_closure")]
let target_is_shared = false;
if target_is_shared {
lock_guard = target.as_mut().write_lock::<Dynamic>().unwrap(); lock_guard = target.as_mut().write_lock::<Dynamic>().unwrap();
lhs_ptr_inner = lock_guard.deref_mut(); lhs_ptr_inner = lock_guard.deref_mut();
} else { } else {
@ -2314,7 +2319,12 @@ impl Engine {
let loop_var = scope.get_mut_by_index(index); let loop_var = scope.get_mut_by_index(index);
let value = iter_value.flatten(); let value = iter_value.flatten();
if cfg!(not(feature = "no_closure")) && loop_var.is_shared() { #[cfg(not(feature = "no_closure"))]
let loop_var_is_shared = loop_var.is_shared();
#[cfg(feature = "no_closure")]
let loop_var_is_shared = false;
if loop_var_is_shared {
*loop_var.write_lock().unwrap() = value; *loop_var.write_lock().unwrap() = value;
} else { } else {
*loop_var = value; *loop_var = value;

View File

@ -1314,7 +1314,12 @@ impl Engine {
self.inc_operations(state, pos)?; self.inc_operations(state, pos)?;
args = if target.is_shared() || target.is_value() { #[cfg(not(feature = "no_closure"))]
let target_is_shared = target.is_shared();
#[cfg(feature = "no_closure")]
let target_is_shared = false;
args = if target_is_shared || target.is_value() {
arg_values.insert(0, target.take_or_clone().flatten()); arg_values.insert(0, target.take_or_clone().flatten());
arg_values.iter_mut().collect() arg_values.iter_mut().collect()
} else { } else {
@ -1398,7 +1403,12 @@ impl Engine {
self.inc_operations(state, pos)?; self.inc_operations(state, pos)?;
if target.is_shared() || target.is_value() { #[cfg(not(feature = "no_closure"))]
let target_is_shared = target.is_shared();
#[cfg(feature = "no_closure")]
let target_is_shared = false;
if target_is_shared || target.is_value() {
arg_values[0] = target.take_or_clone().flatten(); arg_values[0] = target.take_or_clone().flatten();
args = arg_values.iter_mut().collect(); args = arg_values.iter_mut().collect();
} else { } else {

View File

@ -104,20 +104,18 @@ pub type INT = i64;
pub type INT = i32; pub type INT = i32;
/// The system floating-point type. It is defined as [`f64`]. /// The system floating-point type. It is defined as [`f64`].
/// Not available under `no_float`.
/// ///
/// If the `f32_float` feature is enabled, this will be [`i32`] instead. /// If the `f32_float` feature is enabled, this will be [`i32`] instead.
///
/// Not available under `no_float`.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
#[cfg(not(feature = "f32_float"))] #[cfg(not(feature = "f32_float"))]
pub type FLOAT = f64; pub type FLOAT = f64;
/// The system floating-point type. /// The system floating-point type.
/// It is defined as [`f32`] since the `f32_float` feature is used. /// It is defined as [`f32`] since the `f32_float` feature is used.
/// Not available under `no_float`.
/// ///
/// If the `f32_float` feature is not used, this will be `f64` instead. /// If the `f32_float` feature is not used, this will be `f64` instead.
///
/// Not available under `no_float`.
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
#[cfg(feature = "f32_float")] #[cfg(feature = "f32_float")]
pub type FLOAT = f32; pub type FLOAT = f32;
@ -180,13 +178,11 @@ pub use fn_args::FuncArgs;
pub use ast::ScriptFnMetadata; pub use ast::ScriptFnMetadata;
/// Variable-sized array of [`Dynamic`] values. /// Variable-sized array of [`Dynamic`] values.
///
/// Not available under `no_index`. /// Not available under `no_index`.
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
pub type Array = stdlib::vec::Vec<Dynamic>; pub type Array = stdlib::vec::Vec<Dynamic>;
/// Hash map of [`Dynamic`] values with [`ImmutableString`] keys. /// Hash map of [`Dynamic`] values with [`ImmutableString`] keys.
///
/// Not available under `no_object`. /// Not available under `no_object`.
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
pub type Map = stdlib::collections::BTreeMap<Identifier, Dynamic>; pub type Map = stdlib::collections::BTreeMap<Identifier, Dynamic>;