From 9813f657bb7659f0474a6996c2b81a752c4931ea Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 13 Aug 2022 18:40:14 +0800 Subject: [PATCH] Use ImmutableString for AST. --- .gitignore | 3 --- src/ast/script_fn.rs | 4 ++-- src/eval/global_state.rs | 20 ++++++++++---------- src/func/native.rs | 2 +- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 61b621fb..fe87b992 100644 --- a/.gitignore +++ b/.gitignore @@ -3,11 +3,8 @@ Cargo.lock .vscode/ .cargo/ benches/results -.rhai-repl-history.txt clippy.toml Rhai.toml -before* -after* **/*.bat doc/rhai-sync.json doc/rhai.json diff --git a/src/ast/script_fn.rs b/src/ast/script_fn.rs index 77b67107..1ce664e0 100644 --- a/src/ast/script_fn.rs +++ b/src/ast/script_fn.rs @@ -2,7 +2,7 @@ #![cfg(not(feature = "no_function"))] use super::{FnAccess, StmtBlock}; -use crate::{Identifier, ImmutableString, StaticVec}; +use crate::{ImmutableString, StaticVec}; #[cfg(feature = "no_std")] use std::prelude::v1::*; use std::{fmt, hash::Hash}; @@ -22,7 +22,7 @@ pub struct EncapsulatedEnviron { /// Functions defined within the same [`AST`][crate::AST]. pub lib: crate::Shared, /// Imported [modules][crate::Module]. - pub imports: Box<[(Identifier, crate::Shared)]>, + pub imports: Box<[(ImmutableString, crate::Shared)]>, /// Globally-defined constants. pub constants: Option, } diff --git a/src/eval/global_state.rs b/src/eval/global_state.rs index ebee0e0a..28c0589f 100644 --- a/src/eval/global_state.rs +++ b/src/eval/global_state.rs @@ -25,7 +25,7 @@ pub type GlobalConstants = pub struct GlobalRuntimeState<'a> { /// Stack of module names. #[cfg(not(feature = "no_module"))] - keys: crate::StaticVec, + keys: crate::StaticVec, /// Stack of imported [modules][crate::Module]. #[cfg(not(feature = "no_module"))] modules: crate::StaticVec>, @@ -159,7 +159,7 @@ impl GlobalRuntimeState<'_> { self.keys .iter() .rev() - .position(|key| key == name) + .position(|key| key.as_str() == name) .map(|i| len - 1 - i) } /// Push an imported [module][crate::Module] onto the stack. @@ -169,7 +169,7 @@ impl GlobalRuntimeState<'_> { #[inline(always)] pub fn push_import( &mut self, - name: impl Into, + name: impl Into, module: impl Into>, ) { self.keys.push(name.into()); @@ -205,7 +205,7 @@ impl GlobalRuntimeState<'_> { #[inline] pub(crate) fn iter_imports_raw( &self, - ) -> impl Iterator)> { + ) -> impl Iterator)> { self.keys.iter().rev().zip(self.modules.iter().rev()) } /// Get an iterator to the stack of globally-imported [modules][crate::Module] in forward order. @@ -216,7 +216,7 @@ impl GlobalRuntimeState<'_> { #[inline] pub fn scan_imports_raw( &self, - ) -> impl Iterator)> { + ) -> impl Iterator)> { self.keys.iter().zip(self.modules.iter()) } /// Does the specified function hash key exist in the stack of globally-imported @@ -310,9 +310,9 @@ impl GlobalRuntimeState<'_> { #[cfg(not(feature = "no_module"))] impl IntoIterator for GlobalRuntimeState<'_> { - type Item = (Identifier, crate::Shared); + type Item = (ImmutableString, crate::Shared); type IntoIter = std::iter::Zip< - std::iter::Rev>, + std::iter::Rev>, std::iter::Rev; 3]>>, >; @@ -327,9 +327,9 @@ impl IntoIterator for GlobalRuntimeState<'_> { #[cfg(not(feature = "no_module"))] impl<'a> IntoIterator for &'a GlobalRuntimeState<'_> { - type Item = (&'a Identifier, &'a crate::Shared); + type Item = (&'a ImmutableString, &'a crate::Shared); type IntoIter = std::iter::Zip< - std::iter::Rev>, + std::iter::Rev>, std::iter::Rev>>, >; @@ -341,7 +341,7 @@ impl<'a> IntoIterator for &'a GlobalRuntimeState<'_> { } #[cfg(not(feature = "no_module"))] -impl, M: Into>> Extend<(K, M)> +impl, M: Into>> Extend<(K, M)> for GlobalRuntimeState<'_> { #[inline] diff --git a/src/func/native.rs b/src/func/native.rs index a69ca830..6b3d46de 100644 --- a/src/func/native.rs +++ b/src/func/native.rs @@ -235,7 +235,7 @@ impl<'a> NativeCallContext<'a> { #[inline] pub(crate) fn iter_imports_raw( &self, - ) -> impl Iterator)> { + ) -> impl Iterator)> { self.global.iter().flat_map(|&m| m.iter_imports_raw()) } /// _(internals)_ The current [`GlobalRuntimeState`], if any.