From abe6b4a29bd1e3e05e966f52f82066b3bc2406fc Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 27 Nov 2021 14:24:06 +0800 Subject: [PATCH] Use more const functions. --- Cargo.toml | 2 +- src/engine.rs | 2 +- src/func/hashing.rs | 2 +- src/module/resolvers/file.rs | 4 ++-- src/optimizer.rs | 4 ++-- src/parser.rs | 17 +++++++++++------ src/types/immutable_string.rs | 2 +- 7 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 11012109..0055ed88 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ categories = ["no-std", "embedded", "wasm", "parser-implementations"] smallvec = { version = "1.7", default-features = false, features = ["union", "const_new" ] } ahash = { version = "0.7", default-features = false } num-traits = { version = "0.2", default-features = false } -smartstring = { version = "0.2.7", default-features = false } +smartstring = { version = "0.2.8", default-features = false } rhai_codegen = { version = "1.2", path = "codegen", default-features = false } [features] diff --git a/src/engine.rs b/src/engine.rs index 01ddc4f4..f35509e2 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1159,7 +1159,7 @@ impl Engine { progress: None, #[cfg(not(feature = "no_optimize"))] - optimization_level: Default::default(), + optimization_level: crate::OptimizationLevel::default(), #[cfg(not(feature = "unchecked"))] limits: Limits::new(), diff --git a/src/func/hashing.rs b/src/func/hashing.rs index 8d360c04..a681fa04 100644 --- a/src/func/hashing.rs +++ b/src/func/hashing.rs @@ -49,7 +49,7 @@ impl BuildHasher for StraightHasherBuilder { #[inline(always)] #[must_use] pub fn get_hasher() -> ahash::AHasher { - Default::default() + ahash::AHasher::default() } /// Calculate a [`u64`] hash key from a namespace-qualified variable name. diff --git a/src/module/resolvers/file.rs b/src/module/resolvers/file.rs index 7af23527..5af89136 100644 --- a/src/module/resolvers/file.rs +++ b/src/module/resolvers/file.rs @@ -121,7 +121,7 @@ impl FileModuleResolver { base_path: None, extension: extension.into(), cache_enabled: true, - cache: Default::default(), + cache: BTreeMap::new().into(), } } @@ -150,7 +150,7 @@ impl FileModuleResolver { base_path: Some(path.into()), extension: extension.into(), cache_enabled: true, - cache: Default::default(), + cache: BTreeMap::new().into(), } } diff --git a/src/optimizer.rs b/src/optimizer.rs index af1740cd..620923b7 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -1119,7 +1119,7 @@ pub fn optimize_into_ast( optimization_level: OptimizationLevel, ) -> AST { let level = if cfg!(feature = "no_optimize") { - Default::default() + OptimizationLevel::default() } else { optimization_level }; @@ -1140,7 +1140,7 @@ pub fn optimize_into_ast( .map(|fn_def| crate::ast::ScriptFnDef { name: fn_def.name.clone(), access: fn_def.access, - body: crate::ast::StmtBlock::empty(Position::NONE), + body: crate::ast::StmtBlock::NONE, params: fn_def.params.clone(), lib: None, #[cfg(not(feature = "no_module"))] diff --git a/src/parser.rs b/src/parser.rs index 279e2593..f6bd9259 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -57,13 +57,18 @@ pub struct IdentifierBuilder( impl IdentifierBuilder { /// Create a new IdentifierBuilder. + #[cfg(not(feature = "no_smartstring"))] + #[inline] + #[must_use] + pub const fn new() -> Self { + Self() + } + /// Create a new IdentifierBuilder. + #[cfg(feature = "no_smartstring")] #[inline] #[must_use] pub fn new() -> Self { - Self( - #[cfg(feature = "no_smartstring")] - std::collections::BTreeSet::new(), - ) + Self(std::collections::BTreeSet::new()) } /// Get an identifier from a text string. #[inline] @@ -2013,8 +2018,8 @@ fn parse_custom_syntax( if syntax.scope_may_be_changed { // Add a barrier variable to the stack so earlier variables will not be matched. // Variable searches stop at the first barrier. - let empty = state.get_identifier(SCOPE_SEARCH_BARRIER_MARKER); - state.stack.push((empty, AccessMode::ReadWrite)); + let marker = state.get_identifier(SCOPE_SEARCH_BARRIER_MARKER); + state.stack.push((marker, AccessMode::ReadWrite)); } let parse_func = syntax.parse.as_ref(); diff --git a/src/types/immutable_string.rs b/src/types/immutable_string.rs index 5b446eb0..be5d4c91 100644 --- a/src/types/immutable_string.rs +++ b/src/types/immutable_string.rs @@ -539,7 +539,7 @@ impl ImmutableString { /// Create a new [`ImmutableString`]. #[inline(always)] pub fn new() -> Self { - Self(SmartString::new().into()) + Self(SmartString::new_const().into()) } /// Consume the [`ImmutableString`] and convert it into a [`String`]. /// If there are other references to the same string, a cloned copy is returned.