From 6880d449009b6e2bdee10619457907e976a7c4a5 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 25 Oct 2022 08:31:13 +0800 Subject: [PATCH] Remove unnecessary data. --- src/eval/expr.rs | 2 +- src/eval/stmt.rs | 4 ++-- src/types/scope.rs | 33 ++++++++++++++++++++------------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/eval/expr.rs b/src/eval/expr.rs index c8d17896..6480da35 100644 --- a/src/eval/expr.rs +++ b/src/eval/expr.rs @@ -189,7 +189,7 @@ impl Engine { let var_name = expr.get_variable_name(true).expect("`Expr::Variable`"); match scope.get_index(var_name) { - Some((index, _)) => index, + Some(index) => index, None => { return match self.global_modules.iter().find_map(|m| m.get_var(var_name)) { Some(val) => Ok((val.into(), var_pos)), diff --git a/src/eval/stmt.rs b/src/eval/stmt.rs index 4e26d7d3..18f5465f 100644 --- a/src/eval/stmt.rs +++ b/src/eval/stmt.rs @@ -959,7 +959,7 @@ impl Engine { Stmt::Export(x, ..) => { let (Ident { name, pos, .. }, Ident { name: alias, .. }) = &**x; // Mark scope variables as public - if let Some((index, ..)) = scope.get_index(name) { + if let Some(index) = scope.get_index(name) { let alias = if alias.is_empty() { name } else { alias }.clone(); scope.add_alias_by_index(index, alias.into()); Ok(Dynamic::UNIT) @@ -971,7 +971,7 @@ impl Engine { // Share statement #[cfg(not(feature = "no_closure"))] Stmt::Share(name, pos) => { - if let Some((index, ..)) = scope.get_index(name) { + if let Some(index) = scope.get_index(name) { let val = scope.get_mut_by_index(index); if !val.is_shared() { diff --git a/src/types/scope.rs b/src/types/scope.rs index a3787421..05d566a8 100644 --- a/src/types/scope.rs +++ b/src/types/scope.rs @@ -407,7 +407,7 @@ impl Scope<'_> { /// Find an entry in the [`Scope`], starting from the last. #[inline] #[must_use] - pub(crate) fn get_index(&self, name: &str) -> Option<(usize, AccessMode)> { + pub(crate) fn get_index(&self, name: &str) -> Option { let len = self.len(); self.names @@ -417,7 +417,7 @@ impl Scope<'_> { .find_map(|(i, key)| { if name == key { let index = len - 1 - i; - Some((index, self.values[index].access_mode())) + Some(index) } else { None } @@ -467,10 +467,11 @@ impl Scope<'_> { #[inline] #[must_use] pub fn is_constant(&self, name: &str) -> Option { - self.get_index(name).map(|(.., access)| match access { - AccessMode::ReadWrite => false, - AccessMode::ReadOnly => true, - }) + self.get_index(name) + .map(|n| match self.values[n].access_mode() { + AccessMode::ReadWrite => false, + AccessMode::ReadOnly => true, + }) } /// Update the value of the named entry in the [`Scope`] if it already exists and is not constant. /// Push a new entry with the value into the [`Scope`] if the name doesn't exist or if the @@ -503,7 +504,10 @@ impl Scope<'_> { name: impl AsRef + Into, value: impl Variant + Clone, ) -> &mut Self { - match self.get_index(name.as_ref()) { + match self + .get_index(name.as_ref()) + .map(|n| (n, self.values[n].access_mode())) + { None | Some((.., AccessMode::ReadOnly)) => { self.push(name, value); } @@ -542,7 +546,10 @@ impl Scope<'_> { name: impl AsRef + Into, value: impl Variant + Clone, ) -> &mut Self { - match self.get_index(name.as_ref()) { + match self + .get_index(name.as_ref()) + .map(|n| (n, self.values[n].access_mode())) + { None => { self.push(name, value); } @@ -576,7 +583,7 @@ impl Scope<'_> { #[inline(always)] #[must_use] pub fn get(&self, name: &str) -> Option<&Dynamic> { - self.get_index(name).map(|(index, _)| &self.values[index]) + self.get_index(name).map(|index| &self.values[index]) } /// Remove the last entry in the [`Scope`] by the specified name and return its value. /// @@ -607,7 +614,7 @@ impl Scope<'_> { #[inline(always)] #[must_use] pub fn remove(&mut self, name: &str) -> Option { - self.get_index(name).and_then(|(index, _)| { + self.get_index(name).and_then(|index| { self.names.remove(index); self.aliases.remove(index); self.values.remove(index).try_cast() @@ -640,8 +647,8 @@ impl Scope<'_> { #[must_use] pub fn get_mut(&mut self, name: &str) -> Option<&mut Dynamic> { self.get_index(name) - .and_then(move |(index, access)| match access { - AccessMode::ReadWrite => Some(self.get_mut_by_index(index)), + .and_then(move |n| match self.values[n].access_mode() { + AccessMode::ReadWrite => Some(self.get_mut_by_index(n)), AccessMode::ReadOnly => None, }) } @@ -685,7 +692,7 @@ impl Scope<'_> { name: impl AsRef + Into, alias: impl Into, ) { - if let Some((index, ..)) = self.get_index(name.as_ref()) { + if let Some(index) = self.get_index(name.as_ref()) { let alias = match alias.into() { x if x.is_empty() => name.into(), x => x,