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

This commit is contained in:
ConnorSkees 2020-02-17 09:33:52 -05:00
parent 585011c621
commit 36ec80e0da

View File

@ -69,18 +69,21 @@ impl Mixin {
Ok((name, Mixin::new(scope.clone(), args, body))) Ok((name, Mixin::new(scope.clone(), args, body)))
} }
pub fn args(mut self, args: &CallArgs) -> Mixin { pub fn args(mut self, args: &mut CallArgs) -> SassResult<Mixin> {
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(mut self, super_selector: &Selector) -> SassResult<Vec<Stmt>> { pub fn call(mut self, super_selector: &Selector) -> SassResult<Vec<Stmt>> {
@ -132,7 +135,7 @@ pub(crate) fn eat_include<I: Iterator<Item = Token>>(
devour_whitespace(toks); devour_whitespace(toks);
let args = if let Some(tok) = toks.next() { let mut args = if let Some(tok) = toks.next() {
match tok.kind { match tok.kind {
TokenKind::Symbol(Symbol::SemiColon) => CallArgs::new(), TokenKind::Symbol(Symbol::SemiColon) => CallArgs::new(),
TokenKind::Symbol(Symbol::OpenParen) => { TokenKind::Symbol(Symbol::OpenParen) => {
@ -153,6 +156,6 @@ pub(crate) fn eat_include<I: Iterator<Item = Token>>(
let mixin = scope.get_mixin(&name)?.clone(); let mixin = scope.get_mixin(&name)?.clone();
let rules = mixin.args(&args).call(super_selector)?; let rules = mixin.args(&mut args)?.call(super_selector)?;
Ok(rules) Ok(rules)
} }