Use type alias
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
//! Built-in implementations for common operators.
|
||||
|
||||
use super::call::FnCallArgs;
|
||||
use super::native::FnBuiltin;
|
||||
use crate::engine::OP_CONTAINS;
|
||||
use crate::{
|
||||
Dynamic, ExclusiveRange, ImmutableString, InclusiveRange, NativeCallContext, RhaiResult, INT,
|
||||
};
|
||||
use crate::{Dynamic, ExclusiveRange, ImmutableString, InclusiveRange, INT};
|
||||
use std::any::TypeId;
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
@@ -51,11 +50,7 @@ fn is_numeric(type_id: TypeId) -> bool {
|
||||
///
|
||||
/// The return function will be registered as a _method_, so the first parameter cannot be consumed.
|
||||
#[must_use]
|
||||
pub fn get_builtin_binary_op_fn(
|
||||
op: &str,
|
||||
x: &Dynamic,
|
||||
y: &Dynamic,
|
||||
) -> Option<fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult> {
|
||||
pub fn get_builtin_binary_op_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<FnBuiltin> {
|
||||
let type1 = x.type_id();
|
||||
let type2 = y.type_id();
|
||||
|
||||
@@ -516,11 +511,7 @@ pub fn get_builtin_binary_op_fn(
|
||||
///
|
||||
/// The return function is registered as a _method_, so the first parameter cannot be consumed.
|
||||
#[must_use]
|
||||
pub fn get_builtin_op_assignment_fn(
|
||||
op: &str,
|
||||
x: &Dynamic,
|
||||
y: &Dynamic,
|
||||
) -> Option<fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult> {
|
||||
pub fn get_builtin_op_assignment_fn(op: &str, x: &Dynamic, y: &Dynamic) -> Option<FnBuiltin> {
|
||||
let type1 = x.type_id();
|
||||
let type2 = y.type_id();
|
||||
|
||||
|
@@ -4,17 +4,16 @@ use super::callable_function::CallableFunction;
|
||||
use super::native::FnAny;
|
||||
use super::{get_builtin_binary_op_fn, get_builtin_op_assignment_fn};
|
||||
use crate::api::default_limits::MAX_DYNAMIC_PARAMETERS;
|
||||
use crate::ast::FnCallHashes;
|
||||
use crate::ast::{Expr, FnCallHashes, Stmt};
|
||||
use crate::engine::{
|
||||
EvalState, FnResolutionCacheEntry, Imports, KEYWORD_DEBUG, KEYWORD_EVAL, KEYWORD_FN_PTR,
|
||||
KEYWORD_FN_PTR_CALL, KEYWORD_FN_PTR_CURRY, KEYWORD_IS_DEF_VAR, KEYWORD_PRINT, KEYWORD_TYPE_OF,
|
||||
};
|
||||
use crate::module::NamespaceRef;
|
||||
use crate::module::Namespace;
|
||||
use crate::tokenizer::Token;
|
||||
use crate::{
|
||||
ast::{Expr, Stmt},
|
||||
calc_fn_hash, calc_fn_params_hash, combine_hashes, Dynamic, Engine, EvalAltResult, FnPtr,
|
||||
Identifier, ImmutableString, Module, Position, RhaiResult, Scope, StaticVec,
|
||||
Identifier, ImmutableString, Module, Position, RhaiResult, RhaiResultOf, Scope, StaticVec,
|
||||
};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
@@ -108,7 +107,7 @@ pub fn ensure_no_data_race(
|
||||
fn_name: impl AsRef<str>,
|
||||
args: &FnCallArgs,
|
||||
is_method_call: bool,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<()> {
|
||||
if let Some((n, _)) = args
|
||||
.iter()
|
||||
.enumerate()
|
||||
@@ -131,7 +130,7 @@ impl Engine {
|
||||
#[must_use]
|
||||
fn gen_call_signature(
|
||||
&self,
|
||||
namespace: Option<&NamespaceRef>,
|
||||
namespace: Option<&Namespace>,
|
||||
fn_name: impl AsRef<str>,
|
||||
args: &[&mut Dynamic],
|
||||
) -> String {
|
||||
@@ -318,7 +317,7 @@ impl Engine {
|
||||
is_ref_mut: bool,
|
||||
is_op_assign: bool,
|
||||
pos: Position,
|
||||
) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<(Dynamic, bool)> {
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
self.inc_operations(&mut mods.num_operations, pos)?;
|
||||
|
||||
@@ -499,8 +498,8 @@ impl Engine {
|
||||
pos: Position,
|
||||
scope: Option<&mut Scope>,
|
||||
level: usize,
|
||||
) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
|
||||
fn no_method_err(name: &str, pos: Position) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<(Dynamic, bool)> {
|
||||
fn no_method_err(name: &str, pos: Position) -> RhaiResultOf<(Dynamic, bool)> {
|
||||
let msg = format!("'{0}' should not be called this way. Try {0}(...);", name);
|
||||
Err(EvalAltResult::ErrorRuntime(msg.into(), pos).into())
|
||||
}
|
||||
@@ -733,7 +732,7 @@ impl Engine {
|
||||
(call_args, call_arg_pos): &mut (StaticVec<Dynamic>, Position),
|
||||
pos: Position,
|
||||
level: usize,
|
||||
) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<(Dynamic, bool)> {
|
||||
let fn_name = fn_name.as_ref();
|
||||
let is_ref_mut = target.is_ref();
|
||||
|
||||
@@ -893,7 +892,7 @@ impl Engine {
|
||||
level: usize,
|
||||
arg_expr: &Expr,
|
||||
constants: &[Dynamic],
|
||||
) -> Result<(Dynamic, Position), Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<(Dynamic, Position)> {
|
||||
match arg_expr {
|
||||
Expr::Stack(slot, pos) => Ok((constants[*slot].clone(), *pos)),
|
||||
ref arg => self
|
||||
@@ -992,7 +991,7 @@ impl Engine {
|
||||
// Append the new curried arguments to the existing list.
|
||||
let fn_curry = a_expr.iter().skip(1).try_fold(
|
||||
fn_curry,
|
||||
|mut curried, expr| -> Result<_, Box<EvalAltResult>> {
|
||||
|mut curried, expr| -> RhaiResultOf<_> {
|
||||
let (value, _) = self.get_arg_value(
|
||||
scope, mods, state, lib, this_ptr, level, expr, constants,
|
||||
)?;
|
||||
@@ -1181,7 +1180,7 @@ impl Engine {
|
||||
state: &mut EvalState,
|
||||
lib: &[&Module],
|
||||
this_ptr: &mut Option<&mut Dynamic>,
|
||||
namespace: &NamespaceRef,
|
||||
namespace: &Namespace,
|
||||
fn_name: impl AsRef<str>,
|
||||
args_expr: &[Expr],
|
||||
constants: &[Dynamic],
|
||||
|
@@ -3,8 +3,9 @@
|
||||
#![cfg(not(feature = "no_function"))]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use crate::parser::ParseResult;
|
||||
use crate::types::dynamic::Variant;
|
||||
use crate::{Engine, EvalAltResult, ParseError, Scope, SmartString, AST};
|
||||
use crate::{Engine, RhaiResultOf, Scope, SmartString, AST};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
|
||||
@@ -77,11 +78,7 @@ pub trait Func<ARGS, RET> {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
fn create_from_script(
|
||||
self,
|
||||
script: &str,
|
||||
entry_point: &str,
|
||||
) -> Result<Self::Output, ParseError>;
|
||||
fn create_from_script(self, script: &str, entry_point: &str) -> ParseResult<Self::Output>;
|
||||
}
|
||||
|
||||
macro_rules! def_anonymous_fn {
|
||||
@@ -92,9 +89,9 @@ macro_rules! def_anonymous_fn {
|
||||
impl<$($par: Variant + Clone,)* RET: Variant + Clone> Func<($($par,)*), RET> for Engine
|
||||
{
|
||||
#[cfg(feature = "sync")]
|
||||
type Output = Box<dyn Fn($($par),*) -> Result<RET, Box<EvalAltResult>> + Send + Sync>;
|
||||
type Output = Box<dyn Fn($($par),*) -> RhaiResultOf<RET> + Send + Sync>;
|
||||
#[cfg(not(feature = "sync"))]
|
||||
type Output = Box<dyn Fn($($par),*) -> Result<RET, Box<EvalAltResult>>>;
|
||||
type Output = Box<dyn Fn($($par),*) -> RhaiResultOf<RET>>;
|
||||
|
||||
#[inline]
|
||||
fn create_from_ast(self, ast: AST, entry_point: &str) -> Self::Output {
|
||||
@@ -103,7 +100,7 @@ macro_rules! def_anonymous_fn {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn create_from_script(self, script: &str, entry_point: &str) -> Result<Self::Output, ParseError> {
|
||||
fn create_from_script(self, script: &str, entry_point: &str) -> ParseResult<Self::Output> {
|
||||
let ast = self.compile(script)?;
|
||||
Ok(Func::<($($par,)*), RET>::create_from_ast(self, ast, entry_point))
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ use crate::tokenizer::{Token, TokenizeState};
|
||||
use crate::types::dynamic::Variant;
|
||||
use crate::{
|
||||
calc_fn_hash, Dynamic, Engine, EvalAltResult, EvalContext, FuncArgs, Module, Position,
|
||||
RhaiResult, StaticVec,
|
||||
RhaiResult, RhaiResultOf, StaticVec,
|
||||
};
|
||||
use std::any::type_name;
|
||||
#[cfg(feature = "no_std")]
|
||||
@@ -233,7 +233,7 @@ impl<'a> NativeCallContext<'a> {
|
||||
&self,
|
||||
fn_name: impl AsRef<str>,
|
||||
args: impl FuncArgs,
|
||||
) -> Result<T, Box<EvalAltResult>> {
|
||||
) -> RhaiResultOf<T> {
|
||||
let mut arg_values = StaticVec::new_const();
|
||||
args.parse(&mut arg_values);
|
||||
|
||||
@@ -277,7 +277,7 @@ impl<'a> NativeCallContext<'a> {
|
||||
is_ref_mut: bool,
|
||||
is_method_call: bool,
|
||||
args: &mut [&mut Dynamic],
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
) -> RhaiResult {
|
||||
let fn_name = fn_name.as_ref();
|
||||
|
||||
let hash = if is_method_call {
|
||||
@@ -363,6 +363,9 @@ pub type FnAny = dyn Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult;
|
||||
#[cfg(feature = "sync")]
|
||||
pub type FnAny = dyn Fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult + Send + Sync;
|
||||
|
||||
/// A trail object for built-in functions.
|
||||
pub type FnBuiltin = fn(NativeCallContext, &mut FnCallArgs) -> RhaiResult;
|
||||
|
||||
/// A standard function that gets an iterator from a type.
|
||||
pub type IteratorFn = fn(Dynamic) -> Box<dyn Iterator<Item = Dynamic>>;
|
||||
|
||||
@@ -405,12 +408,9 @@ pub type OnParseTokenCallback =
|
||||
/// A standard callback function for variable access.
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub type OnVarCallback =
|
||||
Box<dyn Fn(&str, usize, &EvalContext) -> Result<Option<Dynamic>, Box<EvalAltResult>> + 'static>;
|
||||
Box<dyn Fn(&str, usize, &EvalContext) -> RhaiResultOf<Option<Dynamic>> + 'static>;
|
||||
/// A standard callback function for variable access.
|
||||
#[cfg(feature = "sync")]
|
||||
pub type OnVarCallback = Box<
|
||||
dyn Fn(&str, usize, &EvalContext) -> Result<Option<Dynamic>, Box<EvalAltResult>>
|
||||
+ Send
|
||||
+ Sync
|
||||
+ 'static,
|
||||
dyn Fn(&str, usize, &EvalContext) -> RhaiResultOf<Option<Dynamic>> + Send + Sync + 'static,
|
||||
>;
|
||||
|
@@ -10,7 +10,7 @@ pub use crate::{
|
||||
use std::prelude::v1::*;
|
||||
pub use std::{any::TypeId, mem};
|
||||
|
||||
pub type RhaiResult = Result<Dynamic, Box<EvalAltResult>>;
|
||||
pub type RhaiResult = crate::RhaiResult;
|
||||
|
||||
#[cfg(not(features = "no_module"))]
|
||||
pub use rhai_codegen::*;
|
||||
|
@@ -8,7 +8,7 @@ use super::native::{FnAny, SendSync};
|
||||
use crate::r#unsafe::unsafe_try_cast;
|
||||
use crate::tokenizer::Position;
|
||||
use crate::types::dynamic::{DynamicWriteLock, Variant};
|
||||
use crate::{Dynamic, EvalAltResult, NativeCallContext};
|
||||
use crate::{Dynamic, EvalAltResult, NativeCallContext, RhaiResultOf};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
use std::{any::TypeId, mem};
|
||||
@@ -169,14 +169,14 @@ macro_rules! def_register {
|
||||
}
|
||||
|
||||
impl<
|
||||
FN: Fn($($param),*) -> Result<RET, Box<EvalAltResult>> + SendSync + 'static,
|
||||
FN: Fn($($param),*) -> RhaiResultOf<RET> + SendSync + 'static,
|
||||
$($par: Variant + Clone,)*
|
||||
RET: Variant + Clone
|
||||
> RegisterNativeFunction<($($mark,)*), Result<RET, Box<EvalAltResult>>> for FN {
|
||||
> RegisterNativeFunction<($($mark,)*), RhaiResultOf<RET>> for FN {
|
||||
#[inline(always)] fn param_types() -> Box<[TypeId]> { vec![$(TypeId::of::<$par>()),*].into_boxed_slice() }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { vec![$(std::any::type_name::<$par>()),*].into_boxed_slice() }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<Result<RET, Box<EvalAltResult>>>() }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { std::any::type_name::<Result<RET, Box<EvalAltResult>>>() }
|
||||
#[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>>() }
|
||||
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
|
||||
CallableFunction::$abi(Box::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
|
||||
if args.len() == 2 && args[0].is_read_only() && is_setter(ctx.fn_name()) {
|
||||
@@ -194,14 +194,14 @@ macro_rules! def_register {
|
||||
}
|
||||
|
||||
impl<
|
||||
FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> Result<RET, Box<EvalAltResult>> + SendSync + 'static,
|
||||
FN: for<'a> Fn(NativeCallContext<'a>, $($param),*) -> RhaiResultOf<RET> + SendSync + 'static,
|
||||
$($par: Variant + Clone,)*
|
||||
RET: Variant + Clone
|
||||
> RegisterNativeFunction<(NativeCallContext<'static>, $($mark,)*), Result<RET, Box<EvalAltResult>>> for FN {
|
||||
> RegisterNativeFunction<(NativeCallContext<'static>, $($mark,)*), RhaiResultOf<RET>> for FN {
|
||||
#[inline(always)] fn param_types() -> Box<[TypeId]> { vec![$(TypeId::of::<$par>()),*].into_boxed_slice() }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn param_names() -> Box<[&'static str]> { vec![$(std::any::type_name::<$par>()),*].into_boxed_slice() }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type() -> TypeId { TypeId::of::<Result<RET, Box<EvalAltResult>>>() }
|
||||
#[cfg(feature = "metadata")] #[inline(always)] fn return_type_name() -> &'static str { std::any::type_name::<Result<RET, Box<EvalAltResult>>>() }
|
||||
#[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>>() }
|
||||
#[inline(always)] fn into_callable_function(self) -> CallableFunction {
|
||||
CallableFunction::$abi(Box::new(move |ctx: NativeCallContext, args: &mut FnCallArgs| {
|
||||
if args.len() == 2 && args[0].is_read_only() && is_setter(ctx.fn_name()) {
|
||||
|
@@ -5,7 +5,9 @@ use super::call::FnCallArgs;
|
||||
use crate::ast::ScriptFnDef;
|
||||
use crate::engine::{EvalState, Imports};
|
||||
use crate::r#unsafe::unsafe_cast_var_name_to_lifetime;
|
||||
use crate::{Dynamic, Engine, EvalAltResult, Module, Position, RhaiResult, Scope, StaticVec};
|
||||
use crate::{
|
||||
Dynamic, Engine, EvalAltResult, Module, Position, RhaiError, RhaiResult, Scope, StaticVec,
|
||||
};
|
||||
use std::mem;
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
@@ -39,7 +41,7 @@ impl Engine {
|
||||
name: String,
|
||||
fn_def: &ScriptFnDef,
|
||||
mods: &Imports,
|
||||
err: Box<EvalAltResult>,
|
||||
err: RhaiError,
|
||||
pos: Position,
|
||||
) -> RhaiResult {
|
||||
Err(EvalAltResult::ErrorInFunctionCall(
|
||||
|
Reference in New Issue
Block a user