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.
#[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,
name: impl AsRef<str> + Into<Identifier>,
method: impl RegisterNativeFunction<A, N, R, S>,
method: impl RegisterNativeFunction<A, N, C, R, L>,
) -> &mut Self {
self.engine.register_fn(name, method);
self
@@ -149,10 +149,10 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
///
/// Not available under `no_object`.
#[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,
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 {
self.engine.register_get(name, get_fn);
self
@@ -162,10 +162,10 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
///
/// Not available under `no_object`.
#[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,
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 {
self.engine.register_set(name, set_fn);
self
@@ -177,11 +177,19 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
///
/// Not available under `no_object`.
#[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,
name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S1> + crate::func::SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S2> + 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, C2, (), L2>
+ crate::func::SendSync
+ 'static,
) -> &mut Self {
self.engine.register_get_set(name, get_fn, set_fn);
self
@@ -196,9 +204,14 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
///
/// Not available under both `no_index` and `no_object`.
#[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,
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 {
self.engine.register_indexer_get(get_fn);
self
@@ -208,9 +221,16 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
///
/// Not available under both `no_index` and `no_object`.
#[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,
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 {
self.engine.register_indexer_set(set_fn);
self
@@ -220,10 +240,19 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
///
/// Not available under both `no_index` and `no_object`.
#[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,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S1> + crate::func::SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S2> + 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, C2, (), L2>
+ crate::func::SendSync
+ 'static,
) -> &mut Self {
self.engine.register_indexer_get_set(get_fn, set_fn);
self

View File

@@ -185,10 +185,10 @@ impl Engine {
/// This method will be removed in the next major version.
#[deprecated(since = "1.9.1", note = "use `register_fn` instead")]
#[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,
name: impl AsRef<str> + Into<Identifier>,
func: impl RegisterNativeFunction<A, N, R, RhaiResultOf<S>>,
func: impl RegisterNativeFunction<A, N, C, R, true>,
) -> &mut Self {
self.register_fn(name, func)
}
@@ -206,12 +206,10 @@ impl Engine {
#[deprecated(since = "1.9.1", note = "use `register_get` instead")]
#[cfg(not(feature = "no_object"))]
#[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,
name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, RhaiResultOf<S>>
+ crate::func::SendSync
+ 'static,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, C, V, true> + crate::func::SendSync + 'static,
) -> &mut Self {
self.register_get(name, get_fn)
}
@@ -227,10 +225,10 @@ impl Engine {
#[deprecated(since = "1.9.1", note = "use `register_set` instead")]
#[cfg(not(feature = "no_object"))]
#[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,
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
+ 'static,
) -> &mut Self {
@@ -254,10 +252,10 @@ impl Engine {
T: Variant + Clone,
X: Variant + Clone,
V: Variant + Clone,
S,
const C: bool,
>(
&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
+ 'static,
) -> &mut Self {
@@ -279,10 +277,10 @@ impl Engine {
T: Variant + Clone,
X: Variant + Clone,
V: Variant + Clone,
S,
const C: bool,
>(
&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
+ 'static,
) -> &mut Self {
@@ -516,10 +514,15 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
/// This method will be removed in the next major version.
#[deprecated(since = "1.9.1", note = "use `with_fn` instead")]
#[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
X: AsRef<str> + Into<Identifier>,
F: RegisterNativeFunction<A, N, R, RhaiResultOf<S>>,
S: AsRef<str> + Into<Identifier>,
R: Variant + Clone,
F: RegisterNativeFunction<A, N, C, R, true>,
{
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")]
#[cfg(not(feature = "no_object"))]
#[inline(always)]
pub fn with_get_result<V: Variant + Clone, S>(
pub fn with_get_result<V: Variant + Clone, const C: bool>(
&mut self,
name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, RhaiResultOf<S>>
+ crate::func::SendSync
+ 'static,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, C, V, true> + crate::func::SendSync + 'static,
) -> &mut Self {
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")]
#[cfg(not(feature = "no_object"))]
#[inline(always)]
pub fn with_set_result<V: Variant + Clone, S>(
pub fn with_set_result<V: Variant + Clone, const C: bool>(
&mut self,
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
+ 'static,
) -> &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")]
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[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,
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
+ 'static,
) -> &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")]
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[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,
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
+ 'static,
) -> &mut Self {

View File

@@ -56,7 +56,14 @@ impl Engine {
/// # }
/// ```
#[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,
name: impl AsRef<str> + Into<Identifier>,
func: F,
@@ -299,10 +306,10 @@ impl Engine {
/// ```
#[cfg(not(feature = "no_object"))]
#[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,
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 {
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"))]
#[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,
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 {
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"))]
#[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,
name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S1> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S2> + SendSync + 'static,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, C1, V, L1> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, C2, (), L2> + SendSync + 'static,
) -> &mut Self {
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")))]
#[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,
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 {
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
@@ -537,9 +557,15 @@ impl Engine {
/// ```
#[cfg(any(not(feature = "no_index"), not(feature = "no_object")))]
#[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,
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 {
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
@@ -616,13 +642,15 @@ impl Engine {
pub fn register_indexer_get_set<
T: Variant + Clone,
X: Variant + Clone,
const C1: bool,
const C2: bool,
V: Variant + Clone,
S1,
S2,
const L1: bool,
const L2: bool,
>(
&mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S1> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S2> + SendSync + 'static,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, C1, V, L1> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, C2, (), L2> + SendSync + 'static,
) -> &mut Self {
self.register_indexer_get(get_fn)
.register_indexer_set(set_fn)