Use more const functions.

This commit is contained in:
Stephen Chung 2021-11-27 14:24:06 +08:00
parent 280b5b405e
commit abe6b4a29b
7 changed files with 19 additions and 14 deletions

View File

@ -19,7 +19,7 @@ categories = ["no-std", "embedded", "wasm", "parser-implementations"]
smallvec = { version = "1.7", default-features = false, features = ["union", "const_new" ] } smallvec = { version = "1.7", default-features = false, features = ["union", "const_new" ] }
ahash = { version = "0.7", default-features = false } ahash = { version = "0.7", default-features = false }
num-traits = { version = "0.2", 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 } rhai_codegen = { version = "1.2", path = "codegen", default-features = false }
[features] [features]

View File

@ -1159,7 +1159,7 @@ impl Engine {
progress: None, progress: None,
#[cfg(not(feature = "no_optimize"))] #[cfg(not(feature = "no_optimize"))]
optimization_level: Default::default(), optimization_level: crate::OptimizationLevel::default(),
#[cfg(not(feature = "unchecked"))] #[cfg(not(feature = "unchecked"))]
limits: Limits::new(), limits: Limits::new(),

View File

@ -49,7 +49,7 @@ impl BuildHasher for StraightHasherBuilder {
#[inline(always)] #[inline(always)]
#[must_use] #[must_use]
pub fn get_hasher() -> ahash::AHasher { pub fn get_hasher() -> ahash::AHasher {
Default::default() ahash::AHasher::default()
} }
/// Calculate a [`u64`] hash key from a namespace-qualified variable name. /// Calculate a [`u64`] hash key from a namespace-qualified variable name.

View File

@ -121,7 +121,7 @@ impl FileModuleResolver {
base_path: None, base_path: None,
extension: extension.into(), extension: extension.into(),
cache_enabled: true, cache_enabled: true,
cache: Default::default(), cache: BTreeMap::new().into(),
} }
} }
@ -150,7 +150,7 @@ impl FileModuleResolver {
base_path: Some(path.into()), base_path: Some(path.into()),
extension: extension.into(), extension: extension.into(),
cache_enabled: true, cache_enabled: true,
cache: Default::default(), cache: BTreeMap::new().into(),
} }
} }

View File

@ -1119,7 +1119,7 @@ pub fn optimize_into_ast(
optimization_level: OptimizationLevel, optimization_level: OptimizationLevel,
) -> AST { ) -> AST {
let level = if cfg!(feature = "no_optimize") { let level = if cfg!(feature = "no_optimize") {
Default::default() OptimizationLevel::default()
} else { } else {
optimization_level optimization_level
}; };
@ -1140,7 +1140,7 @@ pub fn optimize_into_ast(
.map(|fn_def| crate::ast::ScriptFnDef { .map(|fn_def| crate::ast::ScriptFnDef {
name: fn_def.name.clone(), name: fn_def.name.clone(),
access: fn_def.access, access: fn_def.access,
body: crate::ast::StmtBlock::empty(Position::NONE), body: crate::ast::StmtBlock::NONE,
params: fn_def.params.clone(), params: fn_def.params.clone(),
lib: None, lib: None,
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]

View File

@ -57,13 +57,18 @@ pub struct IdentifierBuilder(
impl IdentifierBuilder { impl IdentifierBuilder {
/// Create a new 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] #[inline]
#[must_use] #[must_use]
pub fn new() -> Self { pub fn new() -> Self {
Self( Self(std::collections::BTreeSet::new())
#[cfg(feature = "no_smartstring")]
std::collections::BTreeSet::new(),
)
} }
/// Get an identifier from a text string. /// Get an identifier from a text string.
#[inline] #[inline]
@ -2013,8 +2018,8 @@ fn parse_custom_syntax(
if syntax.scope_may_be_changed { if syntax.scope_may_be_changed {
// Add a barrier variable to the stack so earlier variables will not be matched. // Add a barrier variable to the stack so earlier variables will not be matched.
// Variable searches stop at the first barrier. // Variable searches stop at the first barrier.
let empty = state.get_identifier(SCOPE_SEARCH_BARRIER_MARKER); let marker = state.get_identifier(SCOPE_SEARCH_BARRIER_MARKER);
state.stack.push((empty, AccessMode::ReadWrite)); state.stack.push((marker, AccessMode::ReadWrite));
} }
let parse_func = syntax.parse.as_ref(); let parse_func = syntax.parse.as_ref();

View File

@ -539,7 +539,7 @@ impl ImmutableString {
/// Create a new [`ImmutableString`]. /// Create a new [`ImmutableString`].
#[inline(always)] #[inline(always)]
pub fn new() -> Self { pub fn new() -> Self {
Self(SmartString::new().into()) Self(SmartString::new_const().into())
} }
/// Consume the [`ImmutableString`] and convert it into a [`String`]. /// Consume the [`ImmutableString`] and convert it into a [`String`].
/// If there are other references to the same string, a cloned copy is returned. /// If there are other references to the same string, a cloned copy is returned.