Use constant booleans for trait.

This commit is contained in:
Stephen Chung 2022-12-03 11:23:34 +08:00
parent 6e99e391e1
commit 4e33bcfa0a
5 changed files with 152 additions and 92 deletions

View File

@ -117,10 +117,10 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
/// Register a custom function. /// Register a custom function.
#[inline(always)] #[inline(always)]
pub fn with_fn<A, const N: usize, R, S>( pub fn with_fn<A, const N: usize, const C: bool, R: Variant + Clone, const L: bool>(
&mut self, &mut self,
name: impl AsRef<str> + Into<Identifier>, name: impl AsRef<str> + Into<Identifier>,
method: impl RegisterNativeFunction<A, N, R, S>, method: impl RegisterNativeFunction<A, N, C, R, L>,
) -> &mut Self { ) -> &mut Self {
self.engine.register_fn(name, method); self.engine.register_fn(name, method);
self self
@ -149,10 +149,10 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
/// ///
/// Not available under `no_object`. /// Not available under `no_object`.
#[inline(always)] #[inline(always)]
pub fn with_get<V: Variant + Clone, S>( pub fn with_get<const C: bool, V: Variant + Clone, const L: bool>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S> + crate::func::SendSync + 'static, get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, C, V, L> + crate::func::SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
self.engine.register_get(name, get_fn); self.engine.register_get(name, get_fn);
self self
@ -162,10 +162,10 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
/// ///
/// Not available under `no_object`. /// Not available under `no_object`.
#[inline(always)] #[inline(always)]
pub fn with_set<V: Variant + Clone, S>( pub fn with_set<const C: bool, V: Variant + Clone, const L: bool>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S> + crate::func::SendSync + 'static, set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, C, (), L> + crate::func::SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
self.engine.register_set(name, set_fn); self.engine.register_set(name, set_fn);
self self
@ -177,11 +177,19 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
/// ///
/// Not available under `no_object`. /// Not available under `no_object`.
#[inline(always)] #[inline(always)]
pub fn with_get_set<V: Variant + Clone, S1, S2>( pub fn with_get_set<
const C1: bool,
const C2: bool,
V: Variant + Clone,
const L1: bool,
const L2: bool,
>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S1> + crate::func::SendSync + 'static, get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, C1, V, L1> + crate::func::SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S2> + crate::func::SendSync + 'static, set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, C2, (), L2>
+ crate::func::SendSync
+ 'static,
) -> &mut Self { ) -> &mut Self {
self.engine.register_get_set(name, get_fn, set_fn); self.engine.register_get_set(name, get_fn, set_fn);
self self
@ -196,9 +204,14 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
/// ///
/// Not available under both `no_index` and `no_object`. /// Not available under both `no_index` and `no_object`.
#[inline(always)] #[inline(always)]
pub fn with_indexer_get<X: Variant + Clone, V: Variant + Clone, S>( pub fn with_indexer_get<
X: Variant + Clone,
const C: bool,
V: Variant + Clone,
const L: bool,
>(
&mut self, &mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S> + crate::func::SendSync + 'static, get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, C, V, L> + crate::func::SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
self.engine.register_indexer_get(get_fn); self.engine.register_indexer_get(get_fn);
self self
@ -208,9 +221,16 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
/// ///
/// Not available under both `no_index` and `no_object`. /// Not available under both `no_index` and `no_object`.
#[inline(always)] #[inline(always)]
pub fn with_indexer_set<X: Variant + Clone, V: Variant + Clone, S>( pub fn with_indexer_set<
X: Variant + Clone,
const C: bool,
V: Variant + Clone,
const L: bool,
>(
&mut self, &mut self,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S> + crate::func::SendSync + 'static, set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, C, (), L>
+ crate::func::SendSync
+ 'static,
) -> &mut Self { ) -> &mut Self {
self.engine.register_indexer_set(set_fn); self.engine.register_indexer_set(set_fn);
self self
@ -220,10 +240,19 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
/// ///
/// Not available under both `no_index` and `no_object`. /// Not available under both `no_index` and `no_object`.
#[inline(always)] #[inline(always)]
pub fn with_indexer_get_set<X: Variant + Clone, V: Variant + Clone, S1, S2>( pub fn with_indexer_get_set<
X: Variant + Clone,
const C1: bool,
const C2: bool,
V: Variant + Clone,
const L1: bool,
const L2: bool,
>(
&mut self, &mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S1> + crate::func::SendSync + 'static, get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, C1, V, L1> + crate::func::SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S2> + crate::func::SendSync + 'static, set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, C2, (), L2>
+ crate::func::SendSync
+ 'static,
) -> &mut Self { ) -> &mut Self {
self.engine.register_indexer_get_set(get_fn, set_fn); self.engine.register_indexer_get_set(get_fn, set_fn);
self self

View File

@ -185,10 +185,10 @@ impl Engine {
/// This method will be removed in the next major version. /// This method will be removed in the next major version.
#[deprecated(since = "1.9.1", note = "use `register_fn` instead")] #[deprecated(since = "1.9.1", note = "use `register_fn` instead")]
#[inline(always)] #[inline(always)]
pub fn register_result_fn<A, const N: usize, R, S>( pub fn register_result_fn<A, const N: usize, const C: bool, R: Variant + Clone>(
&mut self, &mut self,
name: impl AsRef<str> + Into<Identifier>, name: impl AsRef<str> + Into<Identifier>,
func: impl RegisterNativeFunction<A, N, R, RhaiResultOf<S>>, func: impl RegisterNativeFunction<A, N, C, R, true>,
) -> &mut Self { ) -> &mut Self {
self.register_fn(name, func) self.register_fn(name, func)
} }
@ -206,12 +206,10 @@ impl Engine {
#[deprecated(since = "1.9.1", note = "use `register_get` instead")] #[deprecated(since = "1.9.1", note = "use `register_get` instead")]
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline(always)] #[inline(always)]
pub fn register_get_result<T: Variant + Clone, V: Variant + Clone, S>( pub fn register_get_result<T: Variant + Clone, const C: bool, V: Variant + Clone>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, RhaiResultOf<S>> get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, C, V, true> + crate::func::SendSync + 'static,
+ crate::func::SendSync
+ 'static,
) -> &mut Self { ) -> &mut Self {
self.register_get(name, get_fn) self.register_get(name, get_fn)
} }
@ -227,10 +225,10 @@ impl Engine {
#[deprecated(since = "1.9.1", note = "use `register_set` instead")] #[deprecated(since = "1.9.1", note = "use `register_set` instead")]
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline(always)] #[inline(always)]
pub fn register_set_result<T: Variant + Clone, V: Variant + Clone, S>( pub fn register_set_result<T: Variant + Clone, V: Variant + Clone, const C: bool, S>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), RhaiResultOf<S>> set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, C, (), true>
+ crate::func::SendSync + crate::func::SendSync
+ 'static, + 'static,
) -> &mut Self { ) -> &mut Self {
@ -254,10 +252,10 @@ impl Engine {
T: Variant + Clone, T: Variant + Clone,
X: Variant + Clone, X: Variant + Clone,
V: Variant + Clone, V: Variant + Clone,
S, const C: bool,
>( >(
&mut self, &mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, RhaiResultOf<S>> get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, C, V, true>
+ crate::func::SendSync + crate::func::SendSync
+ 'static, + 'static,
) -> &mut Self { ) -> &mut Self {
@ -279,10 +277,10 @@ impl Engine {
T: Variant + Clone, T: Variant + Clone,
X: Variant + Clone, X: Variant + Clone,
V: Variant + Clone, V: Variant + Clone,
S, const C: bool,
>( >(
&mut self, &mut self,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), RhaiResultOf<S>> set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, C, (), true>
+ crate::func::SendSync + crate::func::SendSync
+ 'static, + 'static,
) -> &mut Self { ) -> &mut Self {
@ -516,10 +514,15 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
/// This method will be removed in the next major version. /// This method will be removed in the next major version.
#[deprecated(since = "1.9.1", note = "use `with_fn` instead")] #[deprecated(since = "1.9.1", note = "use `with_fn` instead")]
#[inline(always)] #[inline(always)]
pub fn with_result_fn<X, A, const N: usize, F, R, S>(&mut self, name: X, method: F) -> &mut Self pub fn with_result_fn<S, A, const N: usize, const C: bool, R, F>(
&mut self,
name: S,
method: F,
) -> &mut Self
where where
X: AsRef<str> + Into<Identifier>, S: AsRef<str> + Into<Identifier>,
F: RegisterNativeFunction<A, N, R, RhaiResultOf<S>>, R: Variant + Clone,
F: RegisterNativeFunction<A, N, C, R, true>,
{ {
self.with_fn(name, method) self.with_fn(name, method)
} }
@ -538,12 +541,10 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
#[deprecated(since = "1.9.1", note = "use `with_get` instead")] #[deprecated(since = "1.9.1", note = "use `with_get` instead")]
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline(always)] #[inline(always)]
pub fn with_get_result<V: Variant + Clone, S>( pub fn with_get_result<V: Variant + Clone, const C: bool>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, RhaiResultOf<S>> get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, C, V, true> + crate::func::SendSync + 'static,
+ crate::func::SendSync
+ 'static,
) -> &mut Self { ) -> &mut Self {
self.with_get(name, get_fn) self.with_get(name, get_fn)
} }
@ -560,10 +561,10 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
#[deprecated(since = "1.9.1", note = "use `with_set` instead")] #[deprecated(since = "1.9.1", note = "use `with_set` instead")]
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline(always)] #[inline(always)]
pub fn with_set_result<V: Variant + Clone, S>( pub fn with_set_result<V: Variant + Clone, const C: bool>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), RhaiResultOf<S>> set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, C, (), true>
+ crate::func::SendSync + crate::func::SendSync
+ 'static, + 'static,
) -> &mut Self { ) -> &mut Self {
@ -584,9 +585,9 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
#[deprecated(since = "1.9.1", note = "use `with_indexer_get` instead")] #[deprecated(since = "1.9.1", note = "use `with_indexer_get` instead")]
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline(always)] #[inline(always)]
pub fn with_indexer_get_result<X: Variant + Clone, V: Variant + Clone, S>( pub fn with_indexer_get_result<X: Variant + Clone, V: Variant + Clone, const C: bool>(
&mut self, &mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, RhaiResultOf<S>> get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, C, V, true>
+ crate::func::SendSync + crate::func::SendSync
+ 'static, + 'static,
) -> &mut Self { ) -> &mut Self {
@ -605,9 +606,9 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
#[deprecated(since = "1.9.1", note = "use `with_indexer_set` instead")] #[deprecated(since = "1.9.1", note = "use `with_indexer_set` instead")]
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline(always)] #[inline(always)]
pub fn with_indexer_set_result<X: Variant + Clone, V: Variant + Clone, S>( pub fn with_indexer_set_result<X: Variant + Clone, V: Variant + Clone, const C: bool>(
&mut self, &mut self,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), RhaiResultOf<S>> set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, C, (), true>
+ crate::func::SendSync + crate::func::SendSync
+ 'static, + 'static,
) -> &mut Self { ) -> &mut Self {

View File

@ -56,7 +56,14 @@ impl Engine {
/// # } /// # }
/// ``` /// ```
#[inline] #[inline]
pub fn register_fn<A, const N: usize, R, S, F: RegisterNativeFunction<A, N, R, S>>( pub fn register_fn<
A,
const N: usize,
const C: bool,
R: Variant + Clone,
const L: bool,
F: RegisterNativeFunction<A, N, C, R, L>,
>(
&mut self, &mut self,
name: impl AsRef<str> + Into<Identifier>, name: impl AsRef<str> + Into<Identifier>,
func: F, func: F,
@ -299,10 +306,10 @@ impl Engine {
/// ``` /// ```
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline(always)] #[inline(always)]
pub fn register_get<T: Variant + Clone, V: Variant + Clone, S>( pub fn register_get<T: Variant + Clone, const C: bool, V: Variant + Clone, const L: bool>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S> + SendSync + 'static, get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, C, V, L> + SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
self.register_fn(crate::engine::make_getter(name.as_ref()).as_str(), get_fn) self.register_fn(crate::engine::make_getter(name.as_ref()).as_str(), get_fn)
} }
@ -349,10 +356,10 @@ impl Engine {
/// ``` /// ```
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline(always)] #[inline(always)]
pub fn register_set<T: Variant + Clone, V: Variant + Clone, S>( pub fn register_set<T: Variant + Clone, const C: bool, V: Variant + Clone, const L: bool>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S> + SendSync + 'static, set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, C, (), L> + SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
self.register_fn(crate::engine::make_setter(name.as_ref()).as_str(), set_fn) self.register_fn(crate::engine::make_setter(name.as_ref()).as_str(), set_fn)
} }
@ -403,11 +410,18 @@ impl Engine {
/// ``` /// ```
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline(always)] #[inline(always)]
pub fn register_get_set<T: Variant + Clone, V: Variant + Clone, S1, S2>( pub fn register_get_set<
T: Variant + Clone,
const C1: bool,
const C2: bool,
V: Variant + Clone,
const L1: bool,
const L2: bool,
>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S1> + SendSync + 'static, get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, C1, V, L1> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S2> + SendSync + 'static, set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, C2, (), L2> + SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
self.register_get(&name, get_fn).register_set(&name, set_fn) self.register_get(&name, get_fn).register_set(&name, set_fn)
} }
@ -462,9 +476,15 @@ impl Engine {
/// ``` /// ```
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline] #[inline]
pub fn register_indexer_get<T: Variant + Clone, X: Variant + Clone, V: Variant + Clone, S>( pub fn register_indexer_get<
T: Variant + Clone,
X: Variant + Clone,
const C: bool,
V: Variant + Clone,
const L: bool,
>(
&mut self, &mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S> + SendSync + 'static, get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, C, V, L> + SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<crate::Array>() { if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
@ -537,9 +557,15 @@ impl Engine {
/// ``` /// ```
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline] #[inline]
pub fn register_indexer_set<T: Variant + Clone, X: Variant + Clone, V: Variant + Clone, S>( pub fn register_indexer_set<
T: Variant + Clone,
X: Variant + Clone,
const C: bool,
V: Variant + Clone,
const L: bool,
>(
&mut self, &mut self,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S> + SendSync + 'static, set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, C, (), L> + SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<crate::Array>() { if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
@ -616,13 +642,15 @@ impl Engine {
pub fn register_indexer_get_set< pub fn register_indexer_get_set<
T: Variant + Clone, T: Variant + Clone,
X: Variant + Clone, X: Variant + Clone,
const C1: bool,
const C2: bool,
V: Variant + Clone, V: Variant + Clone,
S1, const L1: bool,
S2, const L2: bool,
>( >(
&mut self, &mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S1> + SendSync + 'static, get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, C1, V, L1> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S2> + SendSync + 'static, set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, C2, (), L2> + SendSync + 'static,
) -> &mut Self { ) -> &mut Self {
self.register_indexer_get(get_fn) self.register_indexer_get(get_fn)
.register_indexer_set(set_fn) .register_indexer_set(set_fn)

View File

@ -26,13 +26,13 @@ use std::{
/// ///
/// # Examples /// # Examples
/// ///
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), 3, R, ()>` = `Fn(&mut A, B, &C) -> R` /// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), 3, false, R, false>` = `Fn(&mut A, B, &C) -> R`
/// ///
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), 3, R, NativeCallContext>` = `Fn(NativeCallContext, &mut A, B, &C) -> R` /// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), 3, true, R, false>` = `Fn(NativeCallContext, &mut A, B, &C) -> R`
/// ///
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), 3, R, RhaiResultOf<()>>` = `Fn(&mut A, B, &C) -> Result<R, Box<EvalAltResult>>` /// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), 3, false, R, true>` = `Fn(&mut A, B, &C) -> Result<R, Box<EvalAltResult>>`
/// ///
/// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), 3, R, RhaiResultOf<NativeCallContext>>` = `Fn(NativeCallContext, &mut A, B, &C) -> Result<R, Box<EvalAltResult>>` /// `RegisterNativeFunction<(Mut<A>, B, Ref<C>), 3, true, R, true>` = `Fn(NativeCallContext, &mut A, B, &C) -> Result<R, Box<EvalAltResult>>`
/// ///
/// These types are not actually used anywhere. /// These types are not actually used anywhere.
pub struct Mut<T>(T); pub struct Mut<T>(T);
@ -73,9 +73,10 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
/// ///
/// * `ARGS` - a tuple containing parameter types, with `&mut T` represented by `Mut<T>`. /// * `ARGS` - a tuple containing parameter types, with `&mut T` represented by `Mut<T>`.
/// * `NUM` - a constant generic containing the number of parameters, must be consistent with `ARGS`. /// * `NUM` - a constant generic containing the number of parameters, must be consistent with `ARGS`.
/// * `CTX` - a constant boolean generic indicating whether there is a `NativeCallContext` parameter.
/// * `RET` - return type of the function; if the function returns `Result`, it is the unwrapped inner value type. /// * `RET` - return type of the function; if the function returns `Result`, it is the unwrapped inner value type.
/// * `RESULT` - a type to distinguish between multiple forms. Must match the function signature. /// * `FALL` - a constant boolean generic indicating whether the function is fallible (i.e. returns `Result<T, Box<EvalAltResult>>`).
pub trait RegisterNativeFunction<ARGS, const NUM: usize, RET, RESULT> { pub trait RegisterNativeFunction<ARGS, const NUM: usize, const CTX: bool, RET, const FALL: bool> {
/// Convert this function into a [`CallableFunction`]. /// Convert this function into a [`CallableFunction`].
#[must_use] #[must_use]
fn into_callable_function(self) -> CallableFunction; fn into_callable_function(self) -> CallableFunction;
@ -105,8 +106,6 @@ pub trait RegisterNativeFunction<ARGS, const NUM: usize, RET, RESULT> {
} }
} }
const EXPECT_ARGS: &str = "arguments";
macro_rules! check_constant { macro_rules! check_constant {
($abi:ident, $n:expr, $ctx:ident, $args:ident) => { ($abi:ident, $n:expr, $ctx:ident, $args:ident) => {
#[cfg(any(not(feature = "no_object"), not(feature = "no_index")))] #[cfg(any(not(feature = "no_object"), not(feature = "no_index")))]
@ -150,7 +149,7 @@ macro_rules! def_register {
FN: Fn($($param),*) -> RET + SendSync + 'static, FN: Fn($($param),*) -> RET + SendSync + 'static,
$($par: Variant + Clone,)* $($par: Variant + Clone,)*
RET: Variant + Clone RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), $n, RET, ()> for FN { > RegisterNativeFunction<($($mark,)*), $n, false, RET, false> for FN {
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] } #[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
#[inline(always)] fn num_params() -> usize { $n } #[inline(always)] fn num_params() -> usize { $n }
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] } #[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
@ -161,7 +160,7 @@ macro_rules! def_register {
check_constant!($abi, $n, ctx, args); check_constant!($abi, $n, ctx, args);
let mut drain = args.iter_mut(); let mut drain = args.iter_mut();
$(let mut $par = $clone(drain.next().expect(EXPECT_ARGS)); )* $(let mut $par = $clone(drain.next().unwrap()); )*
// Call the function with each argument value // Call the function with each argument value
let r = self($($arg),*); let r = self($($arg),*);
@ -176,7 +175,7 @@ macro_rules! def_register {
FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RET + SendSync + 'static, FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RET + SendSync + 'static,
$($par: Variant + Clone,)* $($par: Variant + Clone,)*
RET: Variant + Clone RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), $n, RET, NativeCallContext<'static>> for FN { > RegisterNativeFunction<($($mark,)*), $n, true, RET, false> for FN {
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] } #[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
#[inline(always)] fn num_params() -> usize { $n } #[inline(always)] fn num_params() -> usize { $n }
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] } #[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
@ -187,7 +186,7 @@ macro_rules! def_register {
check_constant!($abi, $n, ctx, args); check_constant!($abi, $n, ctx, args);
let mut drain = args.iter_mut(); let mut drain = args.iter_mut();
$(let mut $par = $clone(drain.next().expect(EXPECT_ARGS)); )* $(let mut $par = $clone(drain.next().unwrap()); )*
// Call the function with each argument value // Call the function with each argument value
let r = self(ctx, $($arg),*); let r = self(ctx, $($arg),*);
@ -202,7 +201,7 @@ macro_rules! def_register {
FN: Fn($($param),*) -> RhaiResultOf<RET> + SendSync + 'static, FN: Fn($($param),*) -> RhaiResultOf<RET> + SendSync + 'static,
$($par: Variant + Clone,)* $($par: Variant + Clone,)*
RET: Variant + Clone RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), $n, RET, RhaiResultOf<()>> for FN { > RegisterNativeFunction<($($mark,)*), $n, false, RET, true> for FN {
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] } #[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
#[inline(always)] fn num_params() -> usize { $n } #[inline(always)] fn num_params() -> usize { $n }
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] } #[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
@ -214,7 +213,7 @@ macro_rules! def_register {
check_constant!($abi, $n, ctx, args); check_constant!($abi, $n, ctx, args);
let mut drain = args.iter_mut(); let mut drain = args.iter_mut();
$(let mut $par = $clone(drain.next().expect(EXPECT_ARGS)); )* $(let mut $par = $clone(drain.next().unwrap()); )*
// Call the function with each argument value // Call the function with each argument value
self($($arg),*).map(Dynamic::from) self($($arg),*).map(Dynamic::from)
@ -226,7 +225,7 @@ macro_rules! def_register {
FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RhaiResultOf<RET> + SendSync + 'static, FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RhaiResultOf<RET> + SendSync + 'static,
$($par: Variant + Clone,)* $($par: Variant + Clone,)*
RET: Variant + Clone RET: Variant + Clone
> RegisterNativeFunction<($($mark,)*), $n, RET, RhaiResultOf<NativeCallContext<'static>>> for FN { > RegisterNativeFunction<($($mark,)*), $n, true, RET, true> for FN {
#[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] } #[inline(always)] fn param_types() -> [TypeId;$n] { [$(TypeId::of::<$par>()),*] }
#[inline(always)] fn num_params() -> usize { $n } #[inline(always)] fn num_params() -> usize { $n }
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] } #[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> [&'static str;$n] { [$(type_name::<$param>()),*] }
@ -238,7 +237,7 @@ macro_rules! def_register {
check_constant!($abi, $n, ctx, args); check_constant!($abi, $n, ctx, args);
let mut drain = args.iter_mut(); let mut drain = args.iter_mut();
$(let mut $par = $clone(drain.next().expect(EXPECT_ARGS)); )* $(let mut $par = $clone(drain.next().unwrap()); )*
// Call the function with each argument value // Call the function with each argument value
self(ctx, $($arg),*).map(Dynamic::from) self(ctx, $($arg),*).map(Dynamic::from)

View File

@ -1237,14 +1237,14 @@ impl Module {
/// assert!(module.contains_fn(hash)); /// assert!(module.contains_fn(hash));
/// ``` /// ```
#[inline(always)] #[inline(always)]
pub fn set_native_fn<A, const N: usize, T, F, S>( pub fn set_native_fn<A, const N: usize, const C: bool, T, F>(
&mut self, &mut self,
name: impl AsRef<str> + Into<Identifier>, name: impl AsRef<str> + Into<Identifier>,
func: F, func: F,
) -> u64 ) -> u64
where where
T: Variant + Clone, T: Variant + Clone,
F: RegisterNativeFunction<A, N, T, RhaiResultOf<S>>, F: RegisterNativeFunction<A, N, C, T, true>,
{ {
self.set_fn( self.set_fn(
name, name,
@ -1276,11 +1276,11 @@ impl Module {
/// ``` /// ```
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline(always)] #[inline(always)]
pub fn set_getter_fn<A, T, F, S>(&mut self, name: impl AsRef<str>, func: F) -> u64 pub fn set_getter_fn<A, const C: bool, T, F>(&mut self, name: impl AsRef<str>, func: F) -> u64
where where
A: Variant + Clone, A: Variant + Clone,
T: Variant + Clone, T: Variant + Clone,
F: RegisterNativeFunction<(Mut<A>,), 1, T, RhaiResultOf<S>> + SendSync + 'static, F: RegisterNativeFunction<(Mut<A>,), 1, C, T, true> + SendSync + 'static,
{ {
self.set_fn( self.set_fn(
crate::engine::make_getter(name.as_ref()).as_str(), crate::engine::make_getter(name.as_ref()).as_str(),
@ -1317,11 +1317,11 @@ impl Module {
/// ``` /// ```
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline(always)] #[inline(always)]
pub fn set_setter_fn<A, T, F, S>(&mut self, name: impl AsRef<str>, func: F) -> u64 pub fn set_setter_fn<A, const C: bool, T, F>(&mut self, name: impl AsRef<str>, func: F) -> u64
where where
A: Variant + Clone, A: Variant + Clone,
T: Variant + Clone, T: Variant + Clone,
F: RegisterNativeFunction<(Mut<A>, T), 2, (), RhaiResultOf<S>> + SendSync + 'static, F: RegisterNativeFunction<(Mut<A>, T), 2, C, (), true> + SendSync + 'static,
{ {
self.set_fn( self.set_fn(
crate::engine::make_setter(name.as_ref()).as_str(), crate::engine::make_setter(name.as_ref()).as_str(),
@ -1363,11 +1363,16 @@ impl Module {
/// ``` /// ```
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
#[inline(always)] #[inline(always)]
pub fn set_getter_setter_fn<A: Variant + Clone, T: Variant + Clone, S1, S2>( pub fn set_getter_setter_fn<
A: Variant + Clone,
const C1: bool,
const C2: bool,
T: Variant + Clone,
>(
&mut self, &mut self,
name: impl AsRef<str>, name: impl AsRef<str>,
getter: impl RegisterNativeFunction<(Mut<A>,), 1, T, RhaiResultOf<S1>> + SendSync + 'static, getter: impl RegisterNativeFunction<(Mut<A>,), 1, C1, T, true> + SendSync + 'static,
setter: impl RegisterNativeFunction<(Mut<A>, T), 2, (), RhaiResultOf<S2>> + SendSync + 'static, setter: impl RegisterNativeFunction<(Mut<A>, T), 2, C2, (), true> + SendSync + 'static,
) -> (u64, u64) { ) -> (u64, u64) {
( (
self.set_getter_fn(name.as_ref(), getter), self.set_getter_fn(name.as_ref(), getter),
@ -1404,12 +1409,12 @@ impl Module {
/// ``` /// ```
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline] #[inline]
pub fn set_indexer_get_fn<A, B, T, F, S>(&mut self, func: F) -> u64 pub fn set_indexer_get_fn<A, B, const C: bool, T, F>(&mut self, func: F) -> u64
where where
A: Variant + Clone, A: Variant + Clone,
B: Variant + Clone, B: Variant + Clone,
T: Variant + Clone, T: Variant + Clone,
F: RegisterNativeFunction<(Mut<A>, B), 2, T, RhaiResultOf<S>> + SendSync + 'static, F: RegisterNativeFunction<(Mut<A>, B), 2, C, T, true> + SendSync + 'static,
{ {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
if TypeId::of::<A>() == TypeId::of::<crate::Array>() { if TypeId::of::<A>() == TypeId::of::<crate::Array>() {
@ -1465,12 +1470,12 @@ impl Module {
/// ``` /// ```
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))] #[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[inline] #[inline]
pub fn set_indexer_set_fn<A, B, T, F, S>(&mut self, func: F) -> u64 pub fn set_indexer_set_fn<A, B, const C: bool, T, F>(&mut self, func: F) -> u64
where where
A: Variant + Clone, A: Variant + Clone,
B: Variant + Clone, B: Variant + Clone,
T: Variant + Clone, T: Variant + Clone,
F: RegisterNativeFunction<(Mut<A>, B, T), 3, (), RhaiResultOf<S>> + SendSync + 'static, F: RegisterNativeFunction<(Mut<A>, B, T), 3, C, (), true> + SendSync + 'static,
{ {
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
if TypeId::of::<A>() == TypeId::of::<crate::Array>() { if TypeId::of::<A>() == TypeId::of::<crate::Array>() {
@ -1537,15 +1542,13 @@ impl Module {
pub fn set_indexer_get_set_fn< pub fn set_indexer_get_set_fn<
A: Variant + Clone, A: Variant + Clone,
B: Variant + Clone, B: Variant + Clone,
const C1: bool,
const C2: bool,
T: Variant + Clone, T: Variant + Clone,
S1,
S2,
>( >(
&mut self, &mut self,
get_fn: impl RegisterNativeFunction<(Mut<A>, B), 2, T, RhaiResultOf<S1>> + SendSync + 'static, get_fn: impl RegisterNativeFunction<(Mut<A>, B), 2, C1, T, true> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<A>, B, T), 3, (), RhaiResultOf<S2>> set_fn: impl RegisterNativeFunction<(Mut<A>, B, T), 3, C2, (), true> + SendSync + 'static,
+ SendSync
+ 'static,
) -> (u64, u64) { ) -> (u64, u64) {
( (
self.set_indexer_get_fn(get_fn), self.set_indexer_get_fn(get_fn),