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

@@ -1237,14 +1237,14 @@ impl Module {
/// assert!(module.contains_fn(hash));
/// ```
#[inline(always)]
pub fn set_native_fn<A, T, F, S>(
pub fn set_native_fn<A, const N: usize, T, F, S>(
&mut self,
name: impl AsRef<str> + Into<Identifier>,
func: F,
) -> u64
where
T: Variant + Clone,
F: RegisterNativeFunction<A, T, RhaiResultOf<S>>,
F: RegisterNativeFunction<A, N, T, RhaiResultOf<S>>,
{
self.set_fn(
name,
@@ -1280,7 +1280,7 @@ impl Module {
where
A: Variant + Clone,
T: Variant + Clone,
F: RegisterNativeFunction<(Mut<A>,), T, RhaiResultOf<S>> + SendSync + 'static,
F: RegisterNativeFunction<(Mut<A>,), 1, T, RhaiResultOf<S>> + SendSync + 'static,
{
self.set_fn(
crate::engine::make_getter(name.as_ref()).as_str(),
@@ -1321,7 +1321,7 @@ impl Module {
where
A: Variant + Clone,
T: Variant + Clone,
F: RegisterNativeFunction<(Mut<A>, T), (), RhaiResultOf<S>> + SendSync + 'static,
F: RegisterNativeFunction<(Mut<A>, T), 2, (), RhaiResultOf<S>> + SendSync + 'static,
{
self.set_fn(
crate::engine::make_setter(name.as_ref()).as_str(),
@@ -1366,8 +1366,8 @@ impl Module {
pub fn set_getter_setter_fn<A: Variant + Clone, T: Variant + Clone, S1, S2>(
&mut self,
name: impl AsRef<str>,
getter: impl RegisterNativeFunction<(Mut<A>,), T, RhaiResultOf<S1>> + SendSync + 'static,
setter: impl RegisterNativeFunction<(Mut<A>, T), (), RhaiResultOf<S2>> + SendSync + 'static,
getter: impl RegisterNativeFunction<(Mut<A>,), 1, T, RhaiResultOf<S1>> + SendSync + 'static,
setter: impl RegisterNativeFunction<(Mut<A>, T), 2, (), RhaiResultOf<S2>> + SendSync + 'static,
) -> (u64, u64) {
(
self.set_getter_fn(name.as_ref(), getter),
@@ -1409,7 +1409,7 @@ impl Module {
A: Variant + Clone,
B: Variant + Clone,
T: Variant + Clone,
F: RegisterNativeFunction<(Mut<A>, B), T, RhaiResultOf<S>> + SendSync + 'static,
F: RegisterNativeFunction<(Mut<A>, B), 2, T, RhaiResultOf<S>> + SendSync + 'static,
{
#[cfg(not(feature = "no_index"))]
if TypeId::of::<A>() == TypeId::of::<crate::Array>() {
@@ -1470,7 +1470,7 @@ impl Module {
A: Variant + Clone,
B: Variant + Clone,
T: Variant + Clone,
F: RegisterNativeFunction<(Mut<A>, B, T), (), RhaiResultOf<S>> + SendSync + 'static,
F: RegisterNativeFunction<(Mut<A>, B, T), 3, (), RhaiResultOf<S>> + SendSync + 'static,
{
#[cfg(not(feature = "no_index"))]
if TypeId::of::<A>() == TypeId::of::<crate::Array>() {
@@ -1542,8 +1542,10 @@ impl Module {
S2,
>(
&mut self,
get_fn: impl RegisterNativeFunction<(Mut<A>, B), T, RhaiResultOf<S1>> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<A>, B, T), (), RhaiResultOf<S2>> + SendSync + 'static,
get_fn: impl RegisterNativeFunction<(Mut<A>, B), 2, T, RhaiResultOf<S1>> + SendSync + 'static,
set_fn: impl RegisterNativeFunction<(Mut<A>, B, T), 3, (), RhaiResultOf<S2>>
+ SendSync
+ 'static,
) -> (u64, u64) {
(
self.set_indexer_get_fn(get_fn),