Refine flatten clone for scope.
This commit is contained in:
parent
2aa08c0dd9
commit
4a7bf893e6
18
src/scope.rs
18
src/scope.rs
@ -4,9 +4,7 @@ use crate::any::{Dynamic, Variant};
|
|||||||
use crate::parser::{map_dynamic_to_expr, Expr};
|
use crate::parser::{map_dynamic_to_expr, Expr};
|
||||||
use crate::token::Position;
|
use crate::token::Position;
|
||||||
|
|
||||||
use crate::stdlib::{
|
use crate::stdlib::{borrow::Cow, boxed::Box, iter, string::String, vec::Vec};
|
||||||
borrow::Cow, boxed::Box, collections::HashMap, iter, string::String, vec::Vec,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Type of an entry in the Scope.
|
/// Type of an entry in the Scope.
|
||||||
#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)]
|
#[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.
|
/// Clone the Scope, keeping only the last instances of each variable name.
|
||||||
/// Shadowed variables are omitted in the copy.
|
/// Shadowed variables are omitted in the copy.
|
||||||
pub(crate) fn flatten_clone(&self) -> Self {
|
pub(crate) fn flatten_clone(&self) -> Self {
|
||||||
let mut entries: HashMap<&str, Entry> = Default::default();
|
let mut entries: Vec<Entry> = Default::default();
|
||||||
|
|
||||||
self.0.iter().rev().for_each(|entry| {
|
self.0.iter().rev().for_each(|entry| {
|
||||||
entries
|
if entries
|
||||||
.entry(entry.name.as_ref())
|
.iter()
|
||||||
.or_insert_with(|| entry.clone());
|
.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.
|
/// Get an iterator to entries in the Scope.
|
||||||
|
Loading…
Reference in New Issue
Block a user