Shared variant of Dynamic type; All read/write access operations in Dynamic backed by Read/Write lock guards; new shared() script function

This commit is contained in:
Ilya Lakhin
2020-07-27 11:30:09 +07:00
parent 721c578407
commit e5fe222de3
11 changed files with 383 additions and 73 deletions

View File

@@ -1,6 +1,6 @@
//! Module defining interfaces to native-Rust functions.
use crate::any::Dynamic;
use crate::any::{Dynamic, Variant};
use crate::calc_fn_hash;
use crate::engine::Engine;
use crate::module::Module;
@@ -18,9 +18,9 @@ use crate::stdlib::{boxed::Box, convert::TryFrom, fmt, iter::empty, string::Stri
use crate::stdlib::mem;
#[cfg(not(feature = "sync"))]
use crate::stdlib::rc::Rc;
use crate::stdlib::{rc::Rc, cell::{RefCell, Ref, RefMut}};
#[cfg(feature = "sync")]
use crate::stdlib::sync::Arc;
use crate::stdlib::sync::{Arc, RwLock};
/// Trait that maps to `Send + Sync` only under the `sync` feature.
#[cfg(feature = "sync")]
@@ -34,11 +34,18 @@ pub trait SendSync {}
#[cfg(not(feature = "sync"))]
impl<T> SendSync for T {}
/// Immutable reference counting container
#[cfg(not(feature = "sync"))]
pub type Shared<T> = Rc<T>;
#[cfg(feature = "sync")]
pub type Shared<T> = Arc<T>;
/// Mutable reference counting container(read-write lock)
#[cfg(not(feature = "sync"))]
pub type SharedMut<T> = Rc<RefCell<T>>;
#[cfg(feature = "sync")]
pub type SharedMut<T> = Arc<RwLock<T>>;
/// Consume a `Shared` resource and return a mutable reference to the wrapped value.
/// If the resource is shared (i.e. has other outstanding references), a cloned copy is used.
pub fn shared_make_mut<T: Clone>(value: &mut Shared<T>) -> &mut T {