Use StaticVec to build arguments list.

This commit is contained in:
Stephen Chung 2020-05-10 16:56:17 +08:00
parent 80debbc432
commit 974512d650
3 changed files with 22 additions and 10 deletions

View File

@ -890,7 +890,7 @@ impl Engine {
match rhs {
// xxx.fn_name(arg_expr_list)
Expr::FnCall(x) if x.1.is_none() => {
let ((name, pos), modules, hash, args, def_val) = x.as_ref();
let ((name, pos), _, hash, _, def_val) = x.as_ref();
let mut args: Vec<_> = once(obj)
.chain(
@ -1394,7 +1394,7 @@ impl Engine {
let mut arg_values = args_expr
.iter()
.map(|expr| self.eval_expr(scope, state, expr, level))
.collect::<Result<Vec<_>, _>>()?;
.collect::<Result<StaticVec<_>, _>>()?;
let mut args: Vec<_> = arg_values.iter_mut().collect();
@ -1442,7 +1442,7 @@ impl Engine {
let mut arg_values = args_expr
.iter()
.map(|expr| self.eval_expr(scope, state, expr, level))
.collect::<Result<Vec<_>, _>>()?;
.collect::<Result<StaticVec<_>, _>>()?;
let mut args: Vec<_> = arg_values.iter_mut().collect();

View File

@ -3,14 +3,14 @@
#![allow(non_snake_case)]
use crate::any::{Dynamic, Variant};
use crate::stdlib::vec::Vec;
use crate::utils::StaticVec;
/// Trait that represents arguments to a function call.
/// Any data type that can be converted into a `Vec<Dynamic>` can be used
/// as arguments to a function call.
pub trait FuncArgs {
/// Convert to a `Vec<Dynamic>` of the function call arguments.
fn into_vec(self) -> Vec<Dynamic>;
fn into_vec(self) -> StaticVec<Dynamic>;
}
/// Macro to implement `FuncArgs` for tuples of standard types (each can be
@ -19,11 +19,11 @@ macro_rules! impl_args {
($($p:ident),*) => {
impl<$($p: Variant + Clone),*> FuncArgs for ($($p,)*)
{
fn into_vec(self) -> Vec<Dynamic> {
fn into_vec(self) -> StaticVec<Dynamic> {
let ($($p,)*) = self;
#[allow(unused_mut)]
let mut v = Vec::new();
let mut v = StaticVec::new();
$(v.push($p.into_dynamic());)*
v

View File

@ -9,7 +9,7 @@ use crate::stdlib::{
};
#[cfg(not(feature = "no_std"))]
use crate::stdlib::collections::hash_map::DefaultHasher;
use crate::stdlib::{collections::hash_map::DefaultHasher, iter::FromIterator};
#[cfg(feature = "no_std")]
use ahash::AHasher;
@ -48,7 +48,7 @@ pub fn calc_fn_spec<'a>(
/// This is essentially a knock-off of the [`staticvec`](https://crates.io/crates/staticvec) crate.
/// This simplified implementation here is to avoid pulling in another crate.
#[derive(Clone, Hash, Default)]
pub struct StaticVec<T: Default + Clone> {
pub struct StaticVec<T: Default> {
/// Total number of values held.
len: usize,
/// Static storage. 4 slots should be enough for most cases - i.e. four levels of indirection.
@ -57,7 +57,19 @@ pub struct StaticVec<T: Default + Clone> {
more: Vec<T>,
}
impl<T: Default + Clone> StaticVec<T> {
impl<T: Default> FromIterator<T> for StaticVec<T> {
fn from_iter<X: IntoIterator<Item = T>>(iter: X) -> Self {
let mut vec = StaticVec::new();
for x in iter {
vec.push(x);
}
vec
}
}
impl<T: Default> StaticVec<T> {
/// Create a new `StaticVec`.
pub fn new() -> Self {
Default::default()