Avoid allocations in metadata.

This commit is contained in:
Stephen Chung
2022-12-02 16:44:45 +08:00
parent e10576abff
commit e53be6e8ed
5 changed files with 86 additions and 79 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, R, S>(
pub fn with_fn<A, const N: usize, R, S>(
&mut self,
name: impl AsRef<str> + Into<Identifier>,
method: impl RegisterNativeFunction<A, R, S>,
method: impl RegisterNativeFunction<A, N, R, S>,
) -> &mut Self {
self.engine.register_fn(name, method);
self
@@ -152,7 +152,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
pub fn with_get<V: Variant + Clone, S>(
&mut self,
name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, S> + crate::func::SendSync + 'static,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S> + crate::func::SendSync + 'static,
) -> &mut Self {
self.engine.register_get(name, get_fn);
self
@@ -165,7 +165,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
pub fn with_set<V: Variant + Clone, S>(
&mut self,
name: impl AsRef<str>,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), S> + crate::func::SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S> + crate::func::SendSync + 'static,
) -> &mut Self {
self.engine.register_set(name, set_fn);
self
@@ -180,8 +180,8 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
pub fn with_get_set<V: Variant + Clone, S1, S2>(
&mut self,
name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, S1> + crate::func::SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), S2> + crate::func::SendSync + 'static,
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,
) -> &mut Self {
self.engine.register_get_set(name, get_fn, set_fn);
self
@@ -198,7 +198,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
#[inline(always)]
pub fn with_indexer_get<X: Variant + Clone, V: Variant + Clone, S>(
&mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, S> + crate::func::SendSync + 'static,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S> + crate::func::SendSync + 'static,
) -> &mut Self {
self.engine.register_indexer_get(get_fn);
self
@@ -210,7 +210,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
#[inline(always)]
pub fn with_indexer_set<X: Variant + Clone, V: Variant + Clone, S>(
&mut self,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), S> + crate::func::SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S> + crate::func::SendSync + 'static,
) -> &mut Self {
self.engine.register_indexer_set(set_fn);
self
@@ -222,8 +222,8 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
#[inline(always)]
pub fn with_indexer_get_set<X: Variant + Clone, V: Variant + Clone, S1, S2>(
&mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, S1> + crate::func::SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), S2> + crate::func::SendSync + 'static,
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,
) -> &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, R, S>(
pub fn register_result_fn<A, const N: usize, R, S>(
&mut self,
name: impl AsRef<str> + Into<Identifier>,
func: impl RegisterNativeFunction<A, R, RhaiResultOf<S>>,
func: impl RegisterNativeFunction<A, N, R, RhaiResultOf<S>>,
) -> &mut Self {
self.register_fn(name, func)
}
@@ -209,7 +209,7 @@ impl Engine {
pub fn register_get_result<T: Variant + Clone, V: Variant + Clone, S>(
&mut self,
name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, RhaiResultOf<S>>
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, RhaiResultOf<S>>
+ crate::func::SendSync
+ 'static,
) -> &mut Self {
@@ -230,7 +230,7 @@ impl Engine {
pub fn register_set_result<T: Variant + Clone, V: Variant + Clone, S>(
&mut self,
name: impl AsRef<str>,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), RhaiResultOf<S>>
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), RhaiResultOf<S>>
+ crate::func::SendSync
+ 'static,
) -> &mut Self {
@@ -257,7 +257,7 @@ impl Engine {
S,
>(
&mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, RhaiResultOf<S>>
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, RhaiResultOf<S>>
+ crate::func::SendSync
+ 'static,
) -> &mut Self {
@@ -282,7 +282,7 @@ impl Engine {
S,
>(
&mut self,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), RhaiResultOf<S>>
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), RhaiResultOf<S>>
+ crate::func::SendSync
+ 'static,
) -> &mut Self {
@@ -516,10 +516,10 @@ 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<N, A, F, R, S>(&mut self, name: N, method: F) -> &mut Self
pub fn with_result_fn<X, A, const N: usize, F, R, S>(&mut self, name: X, method: F) -> &mut Self
where
N: AsRef<str> + Into<Identifier>,
F: RegisterNativeFunction<A, R, RhaiResultOf<S>>,
X: AsRef<str> + Into<Identifier>,
F: RegisterNativeFunction<A, N, R, RhaiResultOf<S>>,
{
self.with_fn(name, method)
}
@@ -541,7 +541,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
pub fn with_get_result<V: Variant + Clone, S>(
&mut self,
name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, RhaiResultOf<S>>
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, RhaiResultOf<S>>
+ crate::func::SendSync
+ 'static,
) -> &mut Self {
@@ -563,7 +563,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
pub fn with_set_result<V: Variant + Clone, S>(
&mut self,
name: impl AsRef<str>,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), RhaiResultOf<S>>
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), RhaiResultOf<S>>
+ crate::func::SendSync
+ 'static,
) -> &mut Self {
@@ -586,7 +586,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
#[inline(always)]
pub fn with_indexer_get_result<X: Variant + Clone, V: Variant + Clone, S>(
&mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, RhaiResultOf<S>>
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, RhaiResultOf<S>>
+ crate::func::SendSync
+ 'static,
) -> &mut Self {
@@ -607,7 +607,7 @@ impl<'a, T: Variant + Clone> TypeBuilder<'a, T> {
#[inline(always)]
pub fn with_indexer_set_result<X: Variant + Clone, V: Variant + Clone, S>(
&mut self,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), RhaiResultOf<S>>
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), RhaiResultOf<S>>
+ crate::func::SendSync
+ 'static,
) -> &mut Self {

View File

@@ -56,7 +56,7 @@ impl Engine {
/// # }
/// ```
#[inline]
pub fn register_fn<A, R, S, F: RegisterNativeFunction<A, R, S>>(
pub fn register_fn<A, const N: usize, R, S, F: RegisterNativeFunction<A, N, R, S>>(
&mut self,
name: impl AsRef<str> + Into<Identifier>,
func: F,
@@ -302,7 +302,7 @@ impl Engine {
pub fn register_get<T: Variant + Clone, V: Variant + Clone, S>(
&mut self,
name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, S> + SendSync + 'static,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S> + SendSync + 'static,
) -> &mut Self {
self.register_fn(crate::engine::make_getter(name.as_ref()).as_str(), get_fn)
}
@@ -352,7 +352,7 @@ impl Engine {
pub fn register_set<T: Variant + Clone, V: Variant + Clone, S>(
&mut self,
name: impl AsRef<str>,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), S> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S> + SendSync + 'static,
) -> &mut Self {
self.register_fn(crate::engine::make_setter(name.as_ref()).as_str(), set_fn)
}
@@ -406,8 +406,8 @@ impl Engine {
pub fn register_get_set<T: Variant + Clone, V: Variant + Clone, S1, S2>(
&mut self,
name: impl AsRef<str>,
get_fn: impl RegisterNativeFunction<(Mut<T>,), V, S1> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), (), S2> + SendSync + 'static,
get_fn: impl RegisterNativeFunction<(Mut<T>,), 1, V, S1> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, V), 2, (), S2> + SendSync + 'static,
) -> &mut Self {
self.register_get(&name, get_fn).register_set(&name, set_fn)
}
@@ -464,7 +464,7 @@ impl Engine {
#[inline]
pub fn register_indexer_get<T: Variant + Clone, X: Variant + Clone, V: Variant + Clone, S>(
&mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, S> + SendSync + 'static,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S> + SendSync + 'static,
) -> &mut Self {
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
@@ -539,7 +539,7 @@ impl Engine {
#[inline]
pub fn register_indexer_set<T: Variant + Clone, X: Variant + Clone, V: Variant + Clone, S>(
&mut self,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), S> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S> + SendSync + 'static,
) -> &mut Self {
#[cfg(not(feature = "no_index"))]
if TypeId::of::<T>() == TypeId::of::<crate::Array>() {
@@ -621,8 +621,8 @@ impl Engine {
S2,
>(
&mut self,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), V, S1> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), (), S2> + SendSync + 'static,
get_fn: impl RegisterNativeFunction<(Mut<T>, X), 2, V, S1> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<T>, X, V), 3, (), S2> + SendSync + 'static,
) -> &mut Self {
self.register_indexer_get(get_fn)
.register_indexer_set(set_fn)