Streamline code.
This commit is contained in:
@@ -1513,7 +1513,7 @@ impl FloatWrapper {
|
||||
#[derive(Debug, Clone, Hash)]
|
||||
pub enum Expr {
|
||||
/// Dynamic constant.
|
||||
/// Used to hold either an [`Array`] or [`Map`] literal for quick cloning.
|
||||
/// Used to hold either an [`Array`] or [`Map`][crate::Map] literal for quick cloning.
|
||||
/// All other primitive data types should use the appropriate variants for better speed.
|
||||
DynamicConstant(Box<Dynamic>, Position),
|
||||
/// Boolean constant.
|
||||
@@ -1855,7 +1855,7 @@ mod tests {
|
||||
assert_eq!(size_of::<Option<ast::Expr>>(), 16);
|
||||
assert_eq!(size_of::<ast::Stmt>(), 40);
|
||||
assert_eq!(size_of::<Option<ast::Stmt>>(), 40);
|
||||
assert_eq!(size_of::<FnPtr>(), 32);
|
||||
assert_eq!(size_of::<FnPtr>(), 80);
|
||||
assert_eq!(size_of::<Scope>(), 288);
|
||||
assert_eq!(size_of::<LexError>(), 56);
|
||||
assert_eq!(size_of::<ParseError>(), 16);
|
||||
|
||||
@@ -997,11 +997,11 @@ impl Engine {
|
||||
|
||||
// Check if the variable is `this`
|
||||
if name.as_str() == KEYWORD_THIS {
|
||||
if let Some(val) = this_ptr {
|
||||
return Ok(((*val).into(), *pos));
|
||||
return if let Some(val) = this_ptr {
|
||||
Ok(((*val).into(), *pos))
|
||||
} else {
|
||||
return EvalAltResult::ErrorUnboundThis(*pos).into();
|
||||
}
|
||||
EvalAltResult::ErrorUnboundThis(*pos).into()
|
||||
};
|
||||
}
|
||||
|
||||
// Check if it is directly indexed
|
||||
|
||||
@@ -1232,7 +1232,7 @@ impl Engine {
|
||||
Self::read_file(path).and_then(|contents| Ok(self.compile_with_scope(scope, &contents)?))
|
||||
}
|
||||
/// Parse a JSON string into an [object map][`Map`].
|
||||
/// This is a light-weight alternative to using, say, [`serde_json`][https://crates.io/crates/serde_json] to deserialize the JSON.
|
||||
/// This is a light-weight alternative to using, say, [`serde_json`] to deserialize the JSON.
|
||||
///
|
||||
/// The JSON string must be an object hash. It cannot be a simple scalar value.
|
||||
///
|
||||
|
||||
@@ -26,7 +26,7 @@ pub trait FuncArgs {
|
||||
/// }
|
||||
///
|
||||
/// impl FuncArgs for Options {
|
||||
/// fn parse<C: Extend<Dynamic>>(self, container: &mut C) {
|
||||
/// fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER) {
|
||||
/// container.extend(std::iter::once(self.foo.into()));
|
||||
/// container.extend(std::iter::once(self.bar.into()));
|
||||
/// container.extend(std::iter::once(self.baz.into()));
|
||||
@@ -51,11 +51,12 @@ pub trait FuncArgs {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
fn parse<T: Extend<Dynamic>>(self, container: &mut T);
|
||||
fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER);
|
||||
}
|
||||
|
||||
impl<T: Variant + Clone> FuncArgs for Vec<T> {
|
||||
fn parse<C: Extend<Dynamic>>(self, container: &mut C) {
|
||||
#[inline(always)]
|
||||
fn parse<CONTAINER: Extend<Dynamic>>(self, container: &mut CONTAINER) {
|
||||
container.extend(self.into_iter().map(Variant::into_dynamic));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1132,13 +1132,10 @@ impl Engine {
|
||||
|
||||
// Append the new curried arguments to the existing list.
|
||||
|
||||
args_expr.iter().skip(1).try_for_each(
|
||||
|expr| -> Result<(), Box<EvalAltResult>> {
|
||||
fn_curry
|
||||
.push(self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)?);
|
||||
Ok(())
|
||||
},
|
||||
)?;
|
||||
args_expr.iter().skip(1).try_for_each(|expr| {
|
||||
self.eval_expr(scope, mods, state, lib, this_ptr, expr, level)
|
||||
.map(|value| fn_curry.push(value))
|
||||
})?;
|
||||
|
||||
return Ok(FnPtr::new_unchecked(name, fn_curry).into());
|
||||
}
|
||||
|
||||
@@ -10,12 +10,11 @@ use crate::stdlib::{
|
||||
iter::empty,
|
||||
mem,
|
||||
string::String,
|
||||
vec::Vec,
|
||||
};
|
||||
use crate::token::is_valid_identifier;
|
||||
use crate::{
|
||||
calc_fn_hash, Dynamic, Engine, EvalAltResult, EvalContext, ImmutableString, Module, Position,
|
||||
RhaiResult,
|
||||
RhaiResult, StaticVec,
|
||||
};
|
||||
|
||||
/// Trait that maps to `Send + Sync` only under the `sync` feature.
|
||||
@@ -251,7 +250,7 @@ pub type FnCallArgs<'a> = [&'a mut Dynamic];
|
||||
/// A general function pointer, which may carry additional (i.e. curried) argument values
|
||||
/// to be passed onto a function during a call.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FnPtr(ImmutableString, Vec<Dynamic>);
|
||||
pub struct FnPtr(ImmutableString, StaticVec<Dynamic>);
|
||||
|
||||
impl FnPtr {
|
||||
/// Create a new function pointer.
|
||||
@@ -261,7 +260,10 @@ impl FnPtr {
|
||||
}
|
||||
/// Create a new function pointer without checking its parameters.
|
||||
#[inline(always)]
|
||||
pub(crate) fn new_unchecked(name: impl Into<ImmutableString>, curry: Vec<Dynamic>) -> Self {
|
||||
pub(crate) fn new_unchecked(
|
||||
name: impl Into<ImmutableString>,
|
||||
curry: StaticVec<Dynamic>,
|
||||
) -> Self {
|
||||
Self(name.into(), curry)
|
||||
}
|
||||
/// Get the name of the function.
|
||||
@@ -276,7 +278,7 @@ impl FnPtr {
|
||||
}
|
||||
/// Get the underlying data of the function pointer.
|
||||
#[inline(always)]
|
||||
pub(crate) fn take_data(self) -> (ImmutableString, Vec<Dynamic>) {
|
||||
pub(crate) fn take_data(self) -> (ImmutableString, StaticVec<Dynamic>) {
|
||||
(self.0, self.1)
|
||||
}
|
||||
/// Get the curried arguments.
|
||||
|
||||
@@ -61,6 +61,8 @@ pub fn by_value<T: Variant + Clone>(data: &mut Dynamic) -> T {
|
||||
|
||||
/// Trait to register custom Rust functions.
|
||||
pub trait RegisterNativeFunction<Args, Result> {
|
||||
/// Convert this function into a [`CallableFunction`].
|
||||
fn into_callable_function(self) -> CallableFunction;
|
||||
/// Get the type ID's of this function's parameters.
|
||||
fn param_types() -> Box<[TypeId]>;
|
||||
/// Get the type names of this function's parameters.
|
||||
@@ -69,8 +71,6 @@ pub trait RegisterNativeFunction<Args, Result> {
|
||||
fn return_type() -> TypeId;
|
||||
/// Get the type name of this function's return value.
|
||||
fn return_type_name() -> &'static str;
|
||||
/// Convert this function into a [`CallableFunction`].
|
||||
fn into_callable_function(self) -> CallableFunction;
|
||||
}
|
||||
|
||||
macro_rules! def_register {
|
||||
|
||||
@@ -71,6 +71,7 @@ impl<'a> IntoIterator for Scope<'a> {
|
||||
type Item = (Cow<'a, str>, Dynamic);
|
||||
type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
|
||||
|
||||
#[inline(always)]
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
Box::new(
|
||||
self.values
|
||||
|
||||
Reference in New Issue
Block a user