Do not export fn_native.

This commit is contained in:
Stephen Chung 2020-06-18 18:39:28 +08:00
parent a3ea788fb0
commit 35fa61cd4b
4 changed files with 21 additions and 25 deletions

View File

@ -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)] #![allow(non_snake_case)]

View File

@ -5,11 +5,13 @@ use crate::result::EvalAltResult;
use crate::stdlib::{boxed::Box, fmt, rc::Rc, sync::Arc}; use crate::stdlib::{boxed::Box, fmt, rc::Rc, sync::Arc};
/// Trait that maps to `Send + Sync` only under the `sync` feature.
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
pub trait SendSync: Send + Sync {} pub trait SendSync: Send + Sync {}
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
impl<T: Send + Sync> SendSync for T {} impl<T: Send + Sync> SendSync for T {}
/// Trait that maps to `Send + Sync` only under the `sync` feature.
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
pub trait SendSync {} pub trait SendSync {}
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
@ -57,10 +59,13 @@ pub type FnAny = dyn Fn(&Engine, &mut FnCallArgs) -> Result<Dynamic, Box<EvalAlt
pub type FnAny = pub type FnAny =
dyn Fn(&Engine, &mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync; dyn Fn(&Engine, &mut FnCallArgs) -> Result<Dynamic, Box<EvalAltResult>> + Send + Sync;
/// A standard function that gets an iterator from a type.
pub type IteratorFn = fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>; pub type IteratorFn = fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
/// A standard callback function.
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
pub type Callback<T, R> = Box<dyn Fn(&T) -> R + 'static>; pub type Callback<T, R> = Box<dyn Fn(&T) -> R + 'static>;
/// A standard callback function.
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
pub type Callback<T, R> = Box<dyn Fn(&T) -> R + Send + Sync + 'static>; pub type Callback<T, R> = Box<dyn Fn(&T) -> R + Send + Sync + 'static>;

View File

@ -76,7 +76,7 @@ mod engine;
mod error; mod error;
mod fn_call; mod fn_call;
mod fn_func; mod fn_func;
pub mod fn_native; mod fn_native;
mod fn_register; mod fn_register;
mod module; mod module;
mod optimize; mod optimize;
@ -92,6 +92,7 @@ mod utils;
pub use any::Dynamic; pub use any::Dynamic;
pub use engine::Engine; pub use engine::Engine;
pub use error::{ParseError, ParseErrorType}; pub use error::{ParseError, ParseErrorType};
pub use fn_native::IteratorFn;
pub use fn_register::{RegisterFn, RegisterResultFn}; pub use fn_register::{RegisterFn, RegisterResultFn};
pub use module::Module; pub use module::Module;
pub use parser::{ImmutableString, AST, INT}; pub use parser::{ImmutableString, AST, INT};

View File

@ -339,7 +339,7 @@ impl Module {
/// ///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// let hash = module.set_fn_0("calc", || Ok(42_i64)); /// 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<T: Variant + Clone>( pub fn set_fn_0<T: Variant + Clone>(
&mut self, &mut self,
@ -367,7 +367,7 @@ impl Module {
/// ///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// let hash = module.set_fn_1("calc", |x: i64| Ok(x + 1)); /// 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<A: Variant + Clone, T: Variant + Clone>( pub fn set_fn_1<A: Variant + Clone, T: Variant + Clone>(
&mut self, &mut self,
@ -397,7 +397,7 @@ impl Module {
/// ///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// let hash = module.set_fn_1_mut("calc", |x: &mut i64| { *x += 1; Ok(*x) }); /// 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<A: Variant + Clone, T: Variant + Clone>( pub fn set_fn_1_mut<A: Variant + Clone, T: Variant + Clone>(
&mut self, &mut self,
@ -427,7 +427,7 @@ impl Module {
/// ///
/// let mut module = Module::new(); /// let mut module = Module::new();
/// let hash = module.set_getter_fn("value", |x: &mut i64| { Ok(*x) }); /// 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"))] #[cfg(not(feature = "no_object"))]
pub fn set_getter_fn<A: Variant + Clone, T: Variant + Clone>( pub fn set_getter_fn<A: Variant + Clone, T: Variant + Clone>(
@ -487,7 +487,7 @@ impl Module {
/// let hash = module.set_fn_2_mut("calc", |x: &mut i64, y: ImmutableString| { /// let hash = module.set_fn_2_mut("calc", |x: &mut i64, y: ImmutableString| {
/// *x += y.len() as i64; Ok(*x) /// *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<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>( pub fn set_fn_2_mut<A: Variant + Clone, B: Variant + Clone, T: Variant + Clone>(
&mut self, &mut self,
@ -524,7 +524,7 @@ impl Module {
/// *x = y.len() as i64; /// *x = y.len() as i64;
/// Ok(()) /// Ok(())
/// }); /// });
/// assert!(module.get_fn(hash).is_some()); /// assert!(module.contains_fn(hash));
/// ``` /// ```
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
pub fn set_setter_fn<A: Variant + Clone, B: Variant + Clone>( pub fn set_setter_fn<A: Variant + Clone, B: Variant + Clone>(
@ -549,7 +549,7 @@ impl Module {
/// let hash = module.set_indexer_get_fn(|x: &mut i64, y: ImmutableString| { /// let hash = module.set_indexer_get_fn(|x: &mut i64, y: ImmutableString| {
/// Ok(*x + y.len() as i64) /// 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_object"))]
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
@ -573,7 +573,7 @@ impl Module {
/// let hash = module.set_fn_3("calc", |x: i64, y: ImmutableString, z: i64| { /// let hash = module.set_fn_3("calc", |x: i64, y: ImmutableString, z: i64| {
/// Ok(x + y.len() as i64 + z) /// Ok(x + y.len() as i64 + z)
/// }); /// });
/// assert!(module.get_fn(hash).is_some()); /// assert!(module.contains_fn(hash));
/// ``` /// ```
pub fn set_fn_3< pub fn set_fn_3<
A: Variant + Clone, A: Variant + Clone,
@ -615,7 +615,7 @@ impl Module {
/// let hash = module.set_fn_3_mut("calc", |x: &mut i64, y: ImmutableString, z: i64| { /// let hash = module.set_fn_3_mut("calc", |x: &mut i64, y: ImmutableString, z: i64| {
/// *x += y.len() as i64 + z; Ok(*x) /// *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< pub fn set_fn_3_mut<
A: Variant + Clone, A: Variant + Clone,
@ -658,7 +658,7 @@ impl Module {
/// *x = y.len() as i64 + value; /// *x = y.len() as i64 + value;
/// Ok(()) /// Ok(())
/// }); /// });
/// assert!(module.get_fn(hash).is_some()); /// assert!(module.contains_fn(hash));
/// ``` /// ```
pub fn set_indexer_set_fn<A: Variant + Clone, B: Variant + Clone>( pub fn set_indexer_set_fn<A: Variant + Clone, B: Variant + Clone>(
&mut self, &mut self,
@ -693,7 +693,7 @@ impl Module {
/// let hash = module.set_fn_4("calc", |x: i64, y: ImmutableString, z: i64, _w: ()| { /// let hash = module.set_fn_4("calc", |x: i64, y: ImmutableString, z: i64, _w: ()| {
/// Ok(x + y.len() as i64 + z) /// Ok(x + y.len() as i64 + z)
/// }); /// });
/// assert!(module.get_fn(hash).is_some()); /// assert!(module.contains_fn(hash));
/// ``` /// ```
pub fn set_fn_4< pub fn set_fn_4<
A: Variant + Clone, 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: ()| { /// let hash = module.set_fn_4_mut("calc", |x: &mut i64, y: ImmutableString, z: i64, _w: ()| {
/// *x += y.len() as i64 + z; Ok(*x) /// *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< pub fn set_fn_4_mut<
A: Variant + Clone, A: Variant + Clone,
@ -781,17 +781,7 @@ impl Module {
/// ///
/// The `u64` hash is calculated by the function `crate::calc_fn_hash`. /// The `u64` hash is calculated by the function `crate::calc_fn_hash`.
/// It is also returned by the `set_fn_XXX` calls. /// It is also returned by the `set_fn_XXX` calls.
/// pub(crate) fn get_fn(&self, hash_fn: u64) -> Option<&CallableFunction> {
/// # 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> {
self.functions.get(&hash_fn).map(|(_, _, _, v)| v) self.functions.get(&hash_fn).map(|(_, _, _, v)| v)
} }