Add contains to Scope.

This commit is contained in:
Stephen Chung 2020-03-19 20:55:49 +08:00
parent 6a6c5f30de
commit 702b2010f2

View File

@ -139,6 +139,16 @@ impl<'a> Scope<'a> {
self.0.truncate(size); self.0.truncate(size);
} }
/// Does the scope contain the variable?
pub fn contains(&self, key: &str) -> bool {
self.0
.iter()
.enumerate()
.rev() // Always search a Scope in reverse order
.find(|(_, ScopeEntry { name, .. })| name == key)
.is_some()
}
/// Find a variable in the Scope, starting from the last. /// Find a variable in the Scope, starting from the last.
pub fn get(&self, key: &str) -> Option<(usize, &str, VariableType, Dynamic)> { pub fn get(&self, key: &str) -> Option<(usize, &str, VariableType, Dynamic)> {
self.0 self.0