pref: remove Rc/Arc::from_box

This commit is contained in:
quake 2022-09-06 14:51:32 +09:00
parent 563f18a04b
commit 8ee501e4d9
5 changed files with 22 additions and 33 deletions

View File

@ -6,7 +6,7 @@ use crate::engine::{KEYWORD_THIS, OP_CONCAT};
use crate::eval::FnResolutionCacheEntry; use crate::eval::FnResolutionCacheEntry;
use crate::func::{ use crate::func::{
calc_fn_params_hash, combine_hashes, gen_fn_call_signature, get_builtin_binary_op_fn, calc_fn_params_hash, combine_hashes, gen_fn_call_signature, get_builtin_binary_op_fn,
CallableFunction, FnAny, CallableFunction,
}; };
use crate::types::dynamic::AccessMode; use crate::types::dynamic::AccessMode;
use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, ERR}; use crate::{Dynamic, Engine, Module, Position, RhaiResult, RhaiResultOf, Scope, ERR};
@ -269,7 +269,7 @@ impl Engine {
if let Some(f) = func { if let Some(f) = func {
&entry &entry
.insert(Some(FnResolutionCacheEntry { .insert(Some(FnResolutionCacheEntry {
func: CallableFunction::from_method(Box::new(f) as Box<FnAny>), func: CallableFunction::from_fn_builtin(f),
source: None, source: None,
})) }))
.as_ref() .as_ref()

View File

@ -1,7 +1,6 @@
//! Implement function-calling mechanism for [`Engine`]. //! Implement function-calling mechanism for [`Engine`].
use super::callable_function::CallableFunction; use super::callable_function::CallableFunction;
use super::native::FnAny;
use super::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn}; use super::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn};
use crate::api::default_limits::MAX_DYNAMIC_PARAMETERS; use crate::api::default_limits::MAX_DYNAMIC_PARAMETERS;
use crate::ast::{Expr, FnCallHashes, Stmt}; use crate::ast::{Expr, FnCallHashes, Stmt};
@ -289,18 +288,14 @@ impl Engine {
get_builtin_op_assignment_fn(fn_name, *first_arg, rest_args[0]).map( get_builtin_op_assignment_fn(fn_name, *first_arg, rest_args[0]).map(
|f| FnResolutionCacheEntry { |f| FnResolutionCacheEntry {
func: CallableFunction::from_method( func: CallableFunction::from_fn_builtin(f),
Box::new(f) as Box<FnAny>
),
source: None, source: None,
}, },
) )
} else { } else {
get_builtin_binary_op_fn(fn_name, args[0], args[1]).map(|f| { get_builtin_binary_op_fn(fn_name, args[0], args[1]).map(|f| {
FnResolutionCacheEntry { FnResolutionCacheEntry {
func: CallableFunction::from_method( func: CallableFunction::from_fn_builtin(f),
Box::new(f) as Box<FnAny>
),
source: None, source: None,
} }
}) })

View File

@ -1,6 +1,6 @@
//! Module defining the standard Rhai function type. //! Module defining the standard Rhai function type.
use super::native::{FnAny, FnPlugin, IteratorFn, SendSync}; use super::native::{FnAny, FnBuiltin, FnPlugin, IteratorFn, SendSync};
use crate::ast::FnAccess; use crate::ast::FnAccess;
use crate::plugin::PluginFunction; use crate::plugin::PluginFunction;
use crate::Shared; use crate::Shared;
@ -197,23 +197,17 @@ impl CallableFunction {
Self::Script(..) => None, Self::Script(..) => None,
} }
} }
/// Create a new [`CallableFunction::Pure`]. /// Create a new [`CallableFunction::Method`] from `FnBuiltin`.
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn from_pure(func: Box<FnAny>) -> Self { pub fn from_fn_builtin(func: FnBuiltin) -> Self {
Self::Pure(func.into()) Self::Method(Shared::new(func))
}
/// Create a new [`CallableFunction::Method`].
#[inline(always)]
#[must_use]
pub fn from_method(func: Box<FnAny>) -> Self {
Self::Method(func.into())
} }
/// Create a new [`CallableFunction::Plugin`]. /// Create a new [`CallableFunction::Plugin`].
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn from_plugin(func: impl PluginFunction + 'static + SendSync) -> Self { pub fn from_plugin(func: impl PluginFunction + 'static + SendSync) -> Self {
Self::Plugin((Box::new(func) as Box<FnPlugin>).into()) Self::Plugin(Shared::new(func))
} }
} }

View File

@ -4,7 +4,7 @@
use super::call::FnCallArgs; use super::call::FnCallArgs;
use super::callable_function::CallableFunction; use super::callable_function::CallableFunction;
use super::native::{FnAny, SendSync}; use super::native::{SendSync, Shared};
use crate::types::dynamic::{DynamicWriteLock, Variant}; use crate::types::dynamic::{DynamicWriteLock, Variant};
use crate::{reify, Dynamic, NativeCallContext, RhaiResultOf}; use crate::{reify, Dynamic, NativeCallContext, RhaiResultOf};
#[cfg(feature = "no_std")] #[cfg(feature = "no_std")]
@ -120,7 +120,7 @@ macro_rules! check_constant {
macro_rules! def_register { macro_rules! def_register {
() => { () => {
def_register!(imp from_pure :); def_register!(imp Pure :);
}; };
(imp $abi:ident : $($par:ident => $arg:expr => $mark:ty => $param:ty => $let:stmt => $clone:expr),*) => { (imp $abi:ident : $($par:ident => $arg:expr => $mark:ty => $param:ty => $let:stmt => $clone:expr),*) => {
// ^ function ABI type // ^ function ABI type
@ -140,7 +140,7 @@ macro_rules! def_register {
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() } #[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() }
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { std::any::type_name::<RET>() } #[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { std::any::type_name::<RET>() }
#[inline(always)] fn into_callable_function(self) -> CallableFunction { #[inline(always)] fn into_callable_function(self) -> CallableFunction {
CallableFunction::$abi(Box::new(move |_ctx: NativeCallContext, args: &mut FnCallArgs| { CallableFunction::$abi(Shared::new(move |_ctx: NativeCallContext, args: &mut FnCallArgs| {
// The arguments are assumed to be of the correct number and types! // The arguments are assumed to be of the correct number and types!
check_constant!(_ctx, args); check_constant!(_ctx, args);
@ -152,7 +152,7 @@ macro_rules! def_register {
// Map the result // Map the result
Ok(Dynamic::from(r)) Ok(Dynamic::from(r))
}) as Box<FnAny>) }))
} }
} }
@ -166,7 +166,7 @@ macro_rules! def_register {
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() } #[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RET>() }
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { std::any::type_name::<RET>() } #[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { std::any::type_name::<RET>() }
#[inline(always)] fn into_callable_function(self) -> CallableFunction { #[inline(always)] fn into_callable_function(self) -> CallableFunction {
CallableFunction::$abi(Box::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| { CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
// The arguments are assumed to be of the correct number and types! // The arguments are assumed to be of the correct number and types!
check_constant!(ctx, args); check_constant!(ctx, args);
@ -178,7 +178,7 @@ macro_rules! def_register {
// Map the result // Map the result
Ok(Dynamic::from(r)) Ok(Dynamic::from(r))
}) as Box<FnAny>) }))
} }
} }
@ -192,7 +192,7 @@ macro_rules! def_register {
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RhaiResultOf<RET>>() } #[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RhaiResultOf<RET>>() }
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { std::any::type_name::<RhaiResultOf<RET>>() } #[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { std::any::type_name::<RhaiResultOf<RET>>() }
#[inline(always)] fn into_callable_function(self) -> CallableFunction { #[inline(always)] fn into_callable_function(self) -> CallableFunction {
CallableFunction::$abi(Box::new(move |_ctx: NativeCallContext, args: &mut FnCallArgs| { CallableFunction::$abi(Shared::new(move |_ctx: NativeCallContext, args: &mut FnCallArgs| {
// The arguments are assumed to be of the correct number and types! // The arguments are assumed to be of the correct number and types!
check_constant!(_ctx, args); check_constant!(_ctx, args);
@ -201,7 +201,7 @@ macro_rules! def_register {
// Call the function with each argument value // Call the function with each argument value
self($($arg),*).map(Dynamic::from) self($($arg),*).map(Dynamic::from)
}) as Box<FnAny>) }))
} }
} }
@ -215,7 +215,7 @@ macro_rules! def_register {
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RhaiResultOf<RET>>() } #[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<RhaiResultOf<RET>>() }
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { std::any::type_name::<RhaiResultOf<RET>>() } #[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { std::any::type_name::<RhaiResultOf<RET>>() }
#[inline(always)] fn into_callable_function(self) -> CallableFunction { #[inline(always)] fn into_callable_function(self) -> CallableFunction {
CallableFunction::$abi(Box::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| { CallableFunction::$abi(Shared::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
// The arguments are assumed to be of the correct number and types! // The arguments are assumed to be of the correct number and types!
check_constant!(ctx, args); check_constant!(ctx, args);
@ -224,15 +224,15 @@ macro_rules! def_register {
// 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)
}) as Box<FnAny>) }))
} }
} }
//def_register!(imp_pop $($par => $mark => $param),*); //def_register!(imp_pop $($par => $mark => $param),*);
}; };
($p0:ident $(, $p:ident)*) => { ($p0:ident $(, $p:ident)*) => {
def_register!(imp from_pure : $p0 => $p0 => $p0 => $p0 => let $p0 => by_value $(, $p => $p => $p => $p => let $p => by_value)*); def_register!(imp Pure : $p0 => $p0 => $p0 => $p0 => let $p0 => by_value $(, $p => $p => $p => $p => let $p => by_value)*);
def_register!(imp from_method : $p0 => &mut $p0 => Mut<$p0> => &mut $p0 => let mut $p0 => by_ref $(, $p => $p => $p => $p => let $p => by_value)*); def_register!(imp Method : $p0 => &mut $p0 => Mut<$p0> => &mut $p0 => let mut $p0 => by_ref $(, $p => $p => $p => $p => let $p => by_value)*);
// ^ CallableFunction constructor // ^ CallableFunction constructor
// ^ first parameter passed through // ^ first parameter passed through
// ^ others passed by value (by_value) // ^ others passed by value (by_value)

View File

@ -1263,7 +1263,7 @@ impl Module {
access, access,
None, None,
arg_types, arg_types,
CallableFunction::from_method(Box::new(f)), CallableFunction::Method(Shared::new(f)),
) )
} }