From 4a7bf893e643ee0f2e4069ffac144c6ddc6db6b4 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 3 Aug 2020 23:13:09 +0800 Subject: [PATCH] Refine flatten clone for scope. --- src/scope.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/scope.rs b/src/scope.rs index 836108c7..61f6fa17 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -4,9 +4,7 @@ use crate::any::{Dynamic, Variant}; use crate::parser::{map_dynamic_to_expr, Expr}; use crate::token::Position; -use crate::stdlib::{ - borrow::Cow, boxed::Box, collections::HashMap, iter, string::String, vec::Vec, -}; +use crate::stdlib::{borrow::Cow, boxed::Box, iter, string::String, vec::Vec}; /// Type of an entry in the Scope. #[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)] @@ -394,15 +392,19 @@ impl<'a> Scope<'a> { /// Clone the Scope, keeping only the last instances of each variable name. /// Shadowed variables are omitted in the copy. pub(crate) fn flatten_clone(&self) -> Self { - let mut entries: HashMap<&str, Entry> = Default::default(); + let mut entries: Vec = Default::default(); self.0.iter().rev().for_each(|entry| { - entries - .entry(entry.name.as_ref()) - .or_insert_with(|| entry.clone()); + if entries + .iter() + .find(|Entry { name, .. }| &entry.name == name) + .is_none() + { + entries.push(entry.clone()); + } }); - Self(entries.into_iter().map(|(_, v)| v).collect()) + Self(entries) } /// Get an iterator to entries in the Scope.