Add public_only parameter to module function methods.

This commit is contained in:
Stephen Chung
2020-07-27 18:10:45 +08:00
parent a3a167424b
commit 057f6435a4
8 changed files with 87 additions and 100 deletions

View File

@@ -355,10 +355,20 @@ impl Module {
///
/// let mut module = Module::new();
/// let hash = module.set_fn_0("calc", || Ok(42_i64));
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
pub fn contains_fn(&self, hash_fn: u64) -> bool {
self.functions.contains_key(&hash_fn)
pub fn contains_fn(&self, hash_fn: u64, public_only: bool) -> bool {
if public_only {
self.functions
.get(&hash_fn)
.map(|(_, access, _, _)| match access {
FnAccess::Public => true,
FnAccess::Private => false,
})
.unwrap_or(false)
} else {
self.functions.contains_key(&hash_fn)
}
}
/// Set a Rust function into the module, returning a hash key.
@@ -443,7 +453,7 @@ impl Module {
/// Ok(orig) // return Result<T, Box<EvalAltResult>>
/// });
///
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
pub fn set_raw_fn<T: Variant + Clone>(
&mut self,
@@ -468,7 +478,7 @@ impl Module {
///
/// let mut module = Module::new();
/// let hash = module.set_fn_0("calc", || Ok(42_i64));
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
pub fn set_fn_0<T: Variant + Clone>(
&mut self,
@@ -491,7 +501,7 @@ impl Module {
///
/// let mut module = Module::new();
/// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1));
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
pub fn set_fn_1<A: Variant + Clone, T: Variant + Clone>(
&mut self,
@@ -516,7 +526,7 @@ impl Module {
///
/// let mut module = Module::new();
/// let hash = module.set_fn_1_mut("calc", |x: &mut i64| { *x += 1; Ok(*x) });
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
pub fn set_fn_1_mut<A: Variant + Clone, T: Variant + Clone>(
&mut self,
@@ -541,7 +551,7 @@ impl Module {
///
/// let mut module = Module::new();
/// let hash = module.set_getter_fn("value", |x: &mut i64| { Ok(*x) });
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
#[cfg(not(feature = "no_object"))]
pub fn set_getter_fn<A: Variant + Clone, T: Variant + Clone>(
@@ -565,7 +575,7 @@ impl Module {
/// let hash = module.set_fn_2("calc", |x: i64, y: ImmutableString| {
/// Ok(x + y.len() as i64)
/// });
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
pub fn set_fn_2<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>(
&mut self,
@@ -596,7 +606,7 @@ impl Module {
/// let hash = module.set_fn_2_mut("calc", |x: &mut i64, y: ImmutableString| {
/// *x += y.len() as i64; Ok(*x)
/// });
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
pub fn set_fn_2_mut<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>(
&mut self,
@@ -628,7 +638,7 @@ impl Module {
/// *x = y.len() as i64;
/// Ok(())
/// });
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
#[cfg(not(feature = "no_object"))]
pub fn set_setter_fn<A: Variant + Clone, B: Variant + Clone>(
@@ -653,7 +663,7 @@ impl Module {
/// let hash = module.set_indexer_get_fn(|x: &mut i64, y: ImmutableString| {
/// Ok(*x + y.len() as i64)
/// });
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_index"))]
@@ -677,7 +687,7 @@ impl Module {
/// let hash = module.set_fn_3("calc", |x: i64, y: ImmutableString, z: i64| {
/// Ok(x + y.len() as i64 + z)
/// });
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
pub fn set_fn_3<
A: Variant + Clone,
@@ -714,7 +724,7 @@ impl Module {
/// let hash = module.set_fn_3_mut("calc", |x: &mut i64, y: ImmutableString, z: i64| {
/// *x += y.len() as i64 + z; Ok(*x)
/// });
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
pub fn set_fn_3_mut<
A: Variant + Clone,
@@ -752,7 +762,7 @@ impl Module {
/// *x = y.len() as i64 + value;
/// Ok(())
/// });
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_index"))]
@@ -796,8 +806,8 @@ impl Module {
/// Ok(())
/// }
/// );
/// assert!(module.contains_fn(hash_get));
/// assert!(module.contains_fn(hash_set));
/// assert!(module.contains_fn(hash_get, true));
/// assert!(module.contains_fn(hash_set, true));
/// ```
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_index"))]
@@ -825,7 +835,7 @@ impl Module {
/// let hash = module.set_fn_4("calc", |x: i64, y: ImmutableString, z: i64, _w: ()| {
/// Ok(x + y.len() as i64 + z)
/// });
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
pub fn set_fn_4<
A: Variant + Clone,
@@ -869,7 +879,7 @@ impl Module {
/// let hash = module.set_fn_4_mut("calc", |x: &mut i64, y: ImmutableString, z: i64, _w: ()| {
/// *x += y.len() as i64 + z; Ok(*x)
/// });
/// assert!(module.contains_fn(hash));
/// assert!(module.contains_fn(hash, true));
/// ```
pub fn set_fn_4_mut<
A: Variant + Clone,
@@ -903,8 +913,14 @@ impl Module {
///
/// The `u64` hash is calculated by the function `crate::calc_fn_hash`.
/// It is also returned by the `set_fn_XXX` calls.
pub(crate) fn get_fn(&self, hash_fn: u64) -> Option<&Func> {
self.functions.get(&hash_fn).map(|(_, _, _, v)| v)
pub(crate) fn get_fn(&self, hash_fn: u64, public_only: bool) -> Option<&Func> {
self.functions
.get(&hash_fn)
.and_then(|(_, access, _, f)| match access {
_ if !public_only => Some(f),
FnAccess::Public => Some(f),
FnAccess::Private => None,
})
}
/// Get a modules-qualified function.