No need to return value for Scope::get.

This commit is contained in:
Stephen Chung 2020-04-27 11:24:58 +08:00
parent 07c5abcc02
commit 5afeba6fd1
2 changed files with 25 additions and 48 deletions

View File

@ -389,7 +389,7 @@ fn search_scope<'a>(
id: &str, id: &str,
begin: Position, begin: Position,
) -> Result<(&'a mut Dynamic, ScopeEntryType), Box<EvalAltResult>> { ) -> Result<(&'a mut Dynamic, ScopeEntryType), Box<EvalAltResult>> {
let (ScopeSource { typ, index, .. }, _) = scope let ScopeSource { typ, index, .. } = scope
.get(id) .get(id)
.ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(id.into(), begin)))?; .ok_or_else(|| Box::new(EvalAltResult::ErrorVariableNotFound(id.into(), begin)))?;
@ -1162,28 +1162,24 @@ impl Engine {
))) )))
} }
Some(( Some(
entry entry
@ @
ScopeSource { ScopeSource {
typ: ScopeEntryType::Normal, typ: ScopeEntryType::Normal,
.. ..
}, }) => {
_,
)) => {
// Avoid referencing scope which is used below as mut // Avoid referencing scope which is used below as mut
let entry = ScopeSource { name, ..entry }; let entry = ScopeSource { name, ..entry };
*scope.get_mut(entry) = rhs_val.clone(); *scope.get_mut(entry) = rhs_val.clone();
Ok(rhs_val) Ok(rhs_val)
} }
Some(( Some(
ScopeSource { ScopeSource {
typ: ScopeEntryType::Constant, typ: ScopeEntryType::Constant,
.. ..
}, }) => Err(Box::new(EvalAltResult::ErrorAssignmentToConstant(
_,
)) => Err(Box::new(EvalAltResult::ErrorAssignmentToConstant(
name.to_string(), name.to_string(),
*op_pos, *op_pos,
))), ))),

View File

@ -291,35 +291,22 @@ impl<'a> Scope<'a> {
} }
/// Find an entry in the Scope, starting from the last. /// 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<EntryRef> {
self.0 self.0
.iter() .iter()
.enumerate() .enumerate()
.rev() // Always search a Scope in reverse order .rev() // Always search a Scope in reverse order
.find_map( .find_map(|(index, Entry { name: key, typ, .. })| {
|(
index,
Entry {
name: key,
typ,
value,
..
},
)| {
if name == key { if name == key {
Some(( Some(EntryRef {
EntryRef {
name: key, name: key,
index, index,
typ: *typ, typ: *typ,
}, })
value.clone(),
))
} else { } else {
None None
} }
}, })
)
} }
/// Get the value of an entry in the Scope, starting from the last. /// 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<T: Variant + Clone>(&mut self, name: &'a str, value: T) { pub fn set_value<T: Variant + Clone>(&mut self, name: &'a str, value: T) {
match self.get(name) { match self.get(name) {
Some(( Some(EntryRef {
EntryRef {
typ: EntryType::Constant, typ: EntryType::Constant,
.. ..
}, }) => panic!("variable {} is constant", name),
_, Some(EntryRef {
)) => panic!("variable {} is constant", name),
Some((
EntryRef {
index, index,
typ: EntryType::Normal, typ: EntryType::Normal,
.. ..
}, }) => self.0.get_mut(index).unwrap().value = Dynamic::from(value),
_,
)) => self.0.get_mut(index).unwrap().value = Dynamic::from(value),
None => self.push(name, value), None => self.push(name, value),
} }
} }