Encapsulate RwLock and RefCell into Locked.

This commit is contained in:
Stephen Chung
2020-10-16 23:41:56 +08:00
parent d88adfd73d
commit a16fc71935
3 changed files with 39 additions and 43 deletions

View File

@@ -1,4 +1,5 @@
use crate::engine::Engine;
use crate::fn_native::Locked;
use crate::module::{Module, ModuleResolver};
use crate::parser::AST;
use crate::result::EvalAltResult;
@@ -6,12 +7,6 @@ use crate::token::Position;
use crate::stdlib::{boxed::Box, collections::HashMap, path::PathBuf, string::String};
#[cfg(not(feature = "sync"))]
use crate::stdlib::cell::RefCell;
#[cfg(feature = "sync")]
use crate::stdlib::sync::RwLock;
/// Module resolution service that loads module script files from the file system.
///
/// Script files are cached so they are are not reloaded and recompiled in subsequent requests.
@@ -21,6 +16,12 @@ use crate::stdlib::sync::RwLock;
/// to the base directory. The script file is then forced to be in a specified extension
/// (default `.rhai`).
///
/// # Function Namespace
///
/// When a function within a script file module is loaded, all functions in the _global_ namespace
/// plus all those defined within the same module are _merged_ into a _unified_ namespace before
/// the call. Therefore, functions in a module script can cross-call each other.
///
/// # Examples
///
/// ```
@@ -39,12 +40,7 @@ use crate::stdlib::sync::RwLock;
pub struct FileModuleResolver {
path: PathBuf,
extension: String,
#[cfg(not(feature = "sync"))]
cache: RefCell<HashMap<PathBuf, AST>>,
#[cfg(feature = "sync")]
cache: RwLock<HashMap<PathBuf, AST>>,
cache: Locked<HashMap<PathBuf, AST>>,
}
impl Default for FileModuleResolver {