From 5afeba6fd1961d331ae9c480694302c546b39257 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 27 Apr 2020 11:24:58 +0800 Subject: [PATCH] No need to return value for Scope::get. --- src/engine.rs | 14 +++++------- src/scope.rs | 59 +++++++++++++++++---------------------------------- 2 files changed, 25 insertions(+), 48 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index e7aacb79..9611a9ce 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -389,7 +389,7 @@ fn search_scope<'a>( id: &str, begin: Position, ) -> Result<(&'a mut Dynamic, ScopeEntryType), Box> { - let (ScopeSource { typ, index, .. }, _) = scope + let ScopeSource { typ, index, .. } = scope .get(id) .ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(id.into(), begin)))?; @@ -1162,28 +1162,24 @@ impl Engine { ))) } - Some(( + Some( entry @ ScopeSource { typ: ScopeEntryType::Normal, .. - }, - _, - )) => { + }) => { // Avoid referencing scope which is used below as mut let entry = ScopeSource { name, ..entry }; *scope.get_mut(entry) = rhs_val.clone(); Ok(rhs_val) } - Some(( + Some( ScopeSource { typ: ScopeEntryType::Constant, .. - }, - _, - )) => Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( + }) => Err(Box::new(EvalAltResult::ErrorAssignmentToConstant( name.to_string(), *op_pos, ))), diff --git a/src/scope.rs b/src/scope.rs index cea43327..5dd23e73 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -291,35 +291,22 @@ impl<'a> Scope<'a> { } /// Find an entry in the Scope, starting from the last. - pub(crate) fn get(&self, name: &str) -> Option<(EntryRef, Dynamic)> { + pub(crate) fn get(&self, name: &str) -> Option { self.0 .iter() .enumerate() .rev() // Always search a Scope in reverse order - .find_map( - |( - index, - Entry { + .find_map(|(index, Entry { name: key, typ, .. })| { + if name == key { + Some(EntryRef { name: key, - typ, - value, - .. - }, - )| { - if name == key { - Some(( - EntryRef { - name: key, - index, - typ: *typ, - }, - value.clone(), - )) - } else { - None - } - }, - ) + index, + typ: *typ, + }) + } else { + None + } + }) } /// Get the value of an entry in the Scope, starting from the last. @@ -365,21 +352,15 @@ impl<'a> Scope<'a> { /// ``` pub fn set_value(&mut self, name: &'a str, value: T) { match self.get(name) { - Some(( - EntryRef { - typ: EntryType::Constant, - .. - }, - _, - )) => panic!("variable {} is constant", name), - Some(( - EntryRef { - index, - typ: EntryType::Normal, - .. - }, - _, - )) => self.0.get_mut(index).unwrap().value = Dynamic::from(value), + Some(EntryRef { + typ: EntryType::Constant, + .. + }) => panic!("variable {} is constant", name), + Some(EntryRef { + index, + typ: EntryType::Normal, + .. + }) => self.0.get_mut(index).unwrap().value = Dynamic::from(value), None => self.push(name, value), } }