From 35fa61cd4bb4c21f6274ccda4cee4b1f70f70625 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 18 Jun 2020 18:39:28 +0800 Subject: [PATCH] Do not export fn_native. --- src/fn_call.rs | 2 +- src/fn_native.rs | 5 +++++ src/lib.rs | 3 ++- src/module.rs | 36 +++++++++++++----------------------- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/fn_call.rs b/src/fn_call.rs index 58711ebb..833014bb 100644 --- a/src/fn_call.rs +++ b/src/fn_call.rs @@ -1,4 +1,4 @@ -//! Helper module which defines `FnArgs` to make function calling easier. +//! Helper module which defines `FuncArgs` to make function calling easier. #![allow(non_snake_case)] diff --git a/src/fn_native.rs b/src/fn_native.rs index 6c9eba10..b2b3aa67 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -5,11 +5,13 @@ use crate::result::EvalAltResult; use crate::stdlib::{boxed::Box, fmt, rc::Rc, sync::Arc}; +/// Trait that maps to `Send + Sync` only under the `sync` feature. #[cfg(feature = "sync")] pub trait SendSync: Send + Sync {} #[cfg(feature = "sync")] impl SendSync for T {} +/// Trait that maps to `Send + Sync` only under the `sync` feature. #[cfg(not(feature = "sync"))] pub trait SendSync {} #[cfg(not(feature = "sync"))] @@ -57,10 +59,13 @@ pub type FnAny = dyn Fn(&Engine, &mut FnCallArgs) -> Result Result> + Send + Sync; +/// A standard function that gets an iterator from a type. pub type IteratorFn = fn(Dynamic) -> Box>; +/// A standard callback function. #[cfg(not(feature = "sync"))] pub type Callback = Box R + 'static>; +/// A standard callback function. #[cfg(feature = "sync")] pub type Callback = Box R + Send + Sync + 'static>; diff --git a/src/lib.rs b/src/lib.rs index aeb1470c..868721d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,7 +76,7 @@ mod engine; mod error; mod fn_call; mod fn_func; -pub mod fn_native; +mod fn_native; mod fn_register; mod module; mod optimize; @@ -92,6 +92,7 @@ mod utils; pub use any::Dynamic; pub use engine::Engine; pub use error::{ParseError, ParseErrorType}; +pub use fn_native::IteratorFn; pub use fn_register::{RegisterFn, RegisterResultFn}; pub use module::Module; pub use parser::{ImmutableString, AST, INT}; diff --git a/src/module.rs b/src/module.rs index 8b022dcb..2f829c50 100644 --- a/src/module.rs +++ b/src/module.rs @@ -339,7 +339,7 @@ impl Module { /// /// let mut module = Module::new(); /// let hash = module.set_fn_0("calc", || Ok(42_i64)); - /// assert!(module.get_fn(hash).is_some()); + /// assert!(module.contains_fn(hash)); /// ``` pub fn set_fn_0( &mut self, @@ -367,7 +367,7 @@ impl Module { /// /// let mut module = Module::new(); /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); - /// assert!(module.get_fn(hash).is_some()); + /// assert!(module.contains_fn(hash)); /// ``` pub fn set_fn_1( &mut self, @@ -397,7 +397,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.get_fn(hash).is_some()); + /// assert!(module.contains_fn(hash)); /// ``` pub fn set_fn_1_mut( &mut self, @@ -427,7 +427,7 @@ impl Module { /// /// let mut module = Module::new(); /// let hash = module.set_getter_fn("value", |x: &mut i64| { Ok(*x) }); - /// assert!(module.get_fn(hash).is_some()); + /// assert!(module.contains_fn(hash)); /// ``` #[cfg(not(feature = "no_object"))] pub fn set_getter_fn( @@ -487,7 +487,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.get_fn(hash).is_some()); + /// assert!(module.contains_fn(hash)); /// ``` pub fn set_fn_2_mut( &mut self, @@ -524,7 +524,7 @@ impl Module { /// *x = y.len() as i64; /// Ok(()) /// }); - /// assert!(module.get_fn(hash).is_some()); + /// assert!(module.contains_fn(hash)); /// ``` #[cfg(not(feature = "no_object"))] pub fn set_setter_fn( @@ -549,7 +549,7 @@ impl Module { /// let hash = module.set_indexer_get_fn(|x: &mut i64, y: ImmutableString| { /// Ok(*x + y.len() as i64) /// }); - /// assert!(module.get_fn(hash).is_some()); + /// assert!(module.contains_fn(hash)); /// ``` #[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_index"))] @@ -573,7 +573,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.get_fn(hash).is_some()); + /// assert!(module.contains_fn(hash)); /// ``` pub fn set_fn_3< A: Variant + Clone, @@ -615,7 +615,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.get_fn(hash).is_some()); + /// assert!(module.contains_fn(hash)); /// ``` pub fn set_fn_3_mut< A: Variant + Clone, @@ -658,7 +658,7 @@ impl Module { /// *x = y.len() as i64 + value; /// Ok(()) /// }); - /// assert!(module.get_fn(hash).is_some()); + /// assert!(module.contains_fn(hash)); /// ``` pub fn set_indexer_set_fn( &mut self, @@ -693,7 +693,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.get_fn(hash).is_some()); + /// assert!(module.contains_fn(hash)); /// ``` pub fn set_fn_4< A: Variant + Clone, @@ -742,7 +742,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.get_fn(hash).is_some()); + /// assert!(module.contains_fn(hash)); /// ``` pub fn set_fn_4_mut< A: Variant + Clone, @@ -781,17 +781,7 @@ impl Module { /// /// The `u64` hash is calculated by the function `crate::calc_fn_hash`. /// It is also returned by the `set_fn_XXX` calls. - /// - /// # Examples - /// - /// ``` - /// use rhai::Module; - /// - /// let mut module = Module::new(); - /// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); - /// assert!(module.get_fn(hash).is_some()); - /// ``` - pub fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction> { + pub(crate) fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction> { self.functions.get(&hash_fn).map(|(_, _, _, v)| v) }