From b913b521dce563fefa38c9fe443855818ea31f27 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 23 Jul 2020 10:12:51 +0800 Subject: [PATCH] Improve currying. --- src/any.rs | 2 +- src/engine.rs | 38 +++++++++++++++++++------------------- src/fn_native.rs | 6 +++--- src/utils.rs | 2 +- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/any.rs b/src/any.rs index 6ff986b3..7f637187 100644 --- a/src/any.rs +++ b/src/any.rs @@ -735,7 +735,7 @@ impl Dynamic { pub(crate) fn take_immutable_string(self) -> Result { match self.0 { Union::Str(s) => Ok(s), - Union::FnPtr(f) => Ok(f.take_fn_name()), + Union::FnPtr(f) => Ok(f.take_data().0), _ => Err(self.type_name()), } } diff --git a/src/engine.rs b/src/engine.rs index fdfe4ad8..692c3400 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -167,7 +167,7 @@ impl Target<'_> { .as_char() .map_err(|_| EvalAltResult::ErrorCharMismatch(Position::none()))?; - let mut chars: StaticVec = s.chars().collect(); + let mut chars = s.chars().collect::>(); let ch = chars[*index]; // See if changed - if so, update the String @@ -1025,7 +1025,7 @@ impl Engine { lib: &Module, target: &mut Target, expr: &Expr, - mut idx_val: Dynamic, + idx_val: Dynamic, level: usize, ) -> Result<(Dynamic, bool), Box> { let ((name, native, pos), _, hash, _, def_val) = match expr { @@ -1038,13 +1038,13 @@ impl Engine { // Get a reference to the mutation target Dynamic let obj = target.as_mut(); - let idx = idx_val.downcast_mut::>().unwrap(); + let mut idx = idx_val.cast::>(); let mut fn_name = name.as_ref(); let (result, updated) = if fn_name == KEYWORD_FN_PTR_CALL && obj.is::() { // FnPtr call let fn_ptr = obj.downcast_ref::().unwrap(); - let mut curry: StaticVec<_> = fn_ptr.curry().iter().cloned().collect(); + let mut curry = fn_ptr.curry().iter().cloned().collect::>(); // Redirect function name let fn_name = fn_ptr.fn_name(); // Recalculate hash @@ -1062,16 +1062,16 @@ impl Engine { ) } else if fn_name == KEYWORD_FN_PTR_CALL && idx.len() > 0 && idx[0].is::() { // FnPtr call on object - let fn_ptr = idx[0].downcast_ref::().unwrap(); - let mut curry: StaticVec<_> = fn_ptr.curry().iter().cloned().collect(); + let fn_ptr = idx.remove(0).cast::(); + let mut curry = fn_ptr.curry().iter().cloned().collect::>(); // Redirect function name let fn_name = fn_ptr.get_fn_name().clone(); // Recalculate hash - let hash = calc_fn_hash(empty(), &fn_name, curry.len() + idx.len() - 1, empty()); + let hash = calc_fn_hash(empty(), &fn_name, curry.len() + idx.len(), empty()); // Replace the first argument with the object pointer, adding the curried arguments let mut arg_values = once(obj) .chain(curry.iter_mut()) - .chain(idx.iter_mut().skip(1)) + .chain(idx.iter_mut()) .collect::>(); let args = arg_values.as_mut(); @@ -1085,7 +1085,12 @@ impl Engine { Ok(( FnPtr::new_unchecked( fn_ptr.get_fn_name().clone(), - fn_ptr.curry().iter().chain(idx.iter()).cloned().collect(), + fn_ptr + .curry() + .iter() + .cloned() + .chain(idx.into_iter()) + .collect(), ) .into(), false, @@ -1947,7 +1952,7 @@ impl Engine { ))); } - let fn_ptr = fn_ptr.downcast_ref::().unwrap(); + let (fn_name, fn_curry) = fn_ptr.cast::().take_data(); let curry: StaticVec<_> = args_expr .iter() @@ -1956,13 +1961,8 @@ impl Engine { .collect::>()?; return Ok(FnPtr::new_unchecked( - fn_ptr.get_fn_name().clone(), - fn_ptr - .curry() - .iter() - .cloned() - .chain(curry.into_iter()) - .collect(), + fn_name, + fn_curry.into_iter().chain(curry.into_iter()).collect(), ) .into()); } @@ -2010,7 +2010,7 @@ impl Engine { let fn_ptr = fn_name.cast::(); curry = fn_ptr.curry().iter().cloned().collect(); // Redirect function name - redirected = fn_ptr.take_fn_name(); + redirected = fn_ptr.take_data().0; name = &redirected; // Skip the first argument args_expr = &args_expr.as_ref()[1..]; @@ -2227,7 +2227,7 @@ impl Engine { Expr::Custom(x) => { let func = (x.0).1.as_ref(); - let ep: StaticVec<_> = (x.0).0.iter().map(|e| e.into()).collect(); + let ep = (x.0).0.iter().map(|e| e.into()).collect::>(); let mut context = EvalContext { mods, state, diff --git a/src/fn_native.rs b/src/fn_native.rs index 39aa6ea1..303c6278 100644 --- a/src/fn_native.rs +++ b/src/fn_native.rs @@ -66,9 +66,9 @@ impl FnPtr { pub(crate) fn get_fn_name(&self) -> &ImmutableString { &self.0 } - /// Get the name of the function. - pub(crate) fn take_fn_name(self) -> ImmutableString { - self.0 + /// Get the underlying data of the function pointer. + pub(crate) fn take_data(self) -> (ImmutableString, Vec) { + (self.0, self.1) } /// Get the curried data. pub(crate) fn curry(&self) -> &[Dynamic] { diff --git a/src/utils.rs b/src/utils.rs index 12add69d..845996c8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -387,7 +387,7 @@ impl StaticVec { let value = self.extract_from_list(index); // Move all items one slot to the left - for x in index + 1..self.len - 1 { + for x in index + 1..self.len { let orig_value = self.extract_from_list(x); self.set_into_list(x - 1, orig_value, false); }