Use .remove() over .get() to avoid a clone

This commit is contained in:
ConnorSkees 2020-02-17 08:13:15 -05:00
parent e7de93bd38
commit f7aacba76c
2 changed files with 13 additions and 10 deletions

View File

@ -1,9 +1,9 @@
use std::iter::Peekable; use std::iter::Peekable;
use crate::args::CallArgs; use crate::args::{CallArgs, eat_func_args, FuncArgs};
use crate::args::{eat_func_args, FuncArgs};
use crate::atrule::AtRule; use crate::atrule::AtRule;
use crate::common::{Pos, Scope, Symbol}; use crate::common::{Pos, Scope, Symbol};
use crate::error::SassResult;
use crate::utils::devour_whitespace; use crate::utils::devour_whitespace;
use crate::value::Value; use crate::value::Value;
use crate::{Token, TokenKind}; use crate::{Token, TokenKind};
@ -66,18 +66,21 @@ impl Function {
Ok((name, Function::new(scope.clone(), args, body))) Ok((name, Function::new(scope.clone(), args, body)))
} }
pub fn args(mut self, args: &CallArgs) -> Function { pub fn args(mut self, args: &mut CallArgs) -> SassResult<Function> {
for (idx, arg) in self.args.0.iter().enumerate() { for (idx, arg) in self.args.0.iter().enumerate() {
let val = match args.get(&format!("{}", idx)) { let val = match args.remove(&format!("{}", idx)) {
Some(v) => v.clone(), Some(v) => v,
None => match args.get(&arg.name) { None => match args.remove(&arg.name) {
Some(v) => v.clone(), Some(v) => v,
None => arg.default.clone().expect("missing variable!"), None => match &arg.default {
Some(v) => v.clone(),
None => return Err(format!("Missing argument ${}.", &arg.name).into())
}
}, },
}; };
self.scope.insert_var(&arg.name, val); self.scope.insert_var(&arg.name, val);
} }
self Ok(self)
} }
pub fn call(&self) -> Value { pub fn call(&self) -> Value {

View File

@ -260,7 +260,7 @@ impl Value {
} }
}, },
}; };
Ok(func.clone().args(&eat_call_args(toks, scope)?).call()) Ok(func.clone().args(&mut eat_call_args(toks, scope)?)?.call())
} }
_ => { _ => {
if let Ok(c) = crate::color::ColorName::try_from(s.as_ref()) { if let Ok(c) = crate::color::ColorName::try_from(s.as_ref()) {