From fc349f67f85265bee15b0983c86a031bc421a58f Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 29 Jun 2021 23:17:31 +0800 Subject: [PATCH] Make more functions const. --- src/custom_syntax.rs | 2 +- src/engine.rs | 16 ++++++++-------- src/engine_settings.rs | 4 ++-- src/optimize.rs | 2 +- src/parse.rs | 2 +- tests/syntax.rs | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/custom_syntax.rs b/src/custom_syntax.rs index 766720a7..36c97003 100644 --- a/src/custom_syntax.rs +++ b/src/custom_syntax.rs @@ -127,7 +127,7 @@ impl Expression<'_> { } } -impl EvalContext<'_, '_, '_, '_, '_, '_, '_> { +impl EvalContext<'_, '_, '_, '_, '_, '_, '_, '_> { /// Evaluate an [expression tree][Expression]. /// /// # WARNING - Low Level API diff --git a/src/engine.rs b/src/engine.rs index 45b63da4..7cd672bb 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -740,21 +740,21 @@ impl Default for Limits { /// Context of a script evaluation process. #[derive(Debug)] -pub struct EvalContext<'a, 'x, 'px, 'm, 's, 't, 'pt> { +pub struct EvalContext<'a, 'x, 'px, 'm, 's, 'b, 't, 'pt> { pub(crate) engine: &'a Engine, pub(crate) scope: &'x mut Scope<'px>, pub(crate) mods: &'m mut Imports, pub(crate) state: &'s mut State, - pub(crate) lib: &'a [&'a Module], + pub(crate) lib: &'b [&'b Module], pub(crate) this_ptr: &'t mut Option<&'pt mut Dynamic>, pub(crate) level: usize, } -impl<'x, 'px> EvalContext<'_, 'x, 'px, '_, '_, '_, '_> { +impl<'x, 'px> EvalContext<'_, 'x, 'px, '_, '_, '_, '_, '_> { /// The current [`Engine`]. #[inline(always)] #[must_use] - pub fn engine(&self) -> &Engine { + pub const fn engine(&self) -> &Engine { self.engine } /// The current source. @@ -766,7 +766,7 @@ impl<'x, 'px> EvalContext<'_, 'x, 'px, '_, '_, '_, '_> { /// The current [`Scope`]. #[inline(always)] #[must_use] - pub fn scope(&self) -> &Scope { + pub const fn scope(&self) -> &Scope<'px> { self.scope } /// Mutable reference to the current [`Scope`]. @@ -788,7 +788,7 @@ impl<'x, 'px> EvalContext<'_, 'x, 'px, '_, '_, '_, '_> { #[cfg(not(feature = "no_module"))] #[inline(always)] #[must_use] - pub fn imports(&self) -> &Imports { + pub const fn imports(&self) -> &Imports { self.mods } /// Get an iterator over the namespaces containing definition of all script-defined functions. @@ -802,7 +802,7 @@ impl<'x, 'px> EvalContext<'_, 'x, 'px, '_, '_, '_, '_> { #[cfg(feature = "internals")] #[inline(always)] #[must_use] - pub fn namespaces(&self) -> &[&Module] { + pub const fn namespaces(&self) -> &[&Module] { self.lib } /// The current bound `this` pointer, if any. @@ -814,7 +814,7 @@ impl<'x, 'px> EvalContext<'_, 'x, 'px, '_, '_, '_, '_> { /// The current nesting level of function calls. #[inline(always)] #[must_use] - pub fn call_level(&self) -> usize { + pub const fn call_level(&self) -> usize { self.level } } diff --git a/src/engine_settings.rs b/src/engine_settings.rs index dbadafd9..e4176e21 100644 --- a/src/engine_settings.rs +++ b/src/engine_settings.rs @@ -29,7 +29,7 @@ impl Engine { #[cfg(not(feature = "no_optimize"))] #[inline(always)] #[must_use] - pub fn optimization_level(&self) -> crate::OptimizationLevel { + pub const fn optimization_level(&self) -> crate::OptimizationLevel { self.optimization_level } /// Set the maximum levels of function calls allowed for a script in order to avoid @@ -179,7 +179,7 @@ impl Engine { #[cfg(not(feature = "no_index"))] #[inline(always)] #[must_use] - pub fn max_array_size(&self) -> usize { + pub const fn max_array_size(&self) -> usize { if let Some(n) = self.limits.max_array_size { n.get() } else { diff --git a/src/optimize.rs b/src/optimize.rs index 60a3596b..7830efb7 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -68,7 +68,7 @@ impl<'a> State<'a> { ) -> Self { Self { changed: false, - variables: vec![], + variables: Vec::new(), propagate_constants: true, engine, lib, diff --git a/src/parse.rs b/src/parse.rs index 7786d489..0de53e8c 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -143,7 +143,7 @@ impl<'e> ParseState<'e> { /// /// Return `None` when the variable name is not found in the `stack`. #[inline(always)] - fn access_var(&mut self, name: &str, _pos: Position) -> Option { + pub fn access_var(&mut self, name: &str, _pos: Position) -> Option { let mut barrier = false; let index = self diff --git a/tests/syntax.rs b/tests/syntax.rs index 8c44ae4b..34c84115 100644 --- a/tests/syntax.rs +++ b/tests/syntax.rs @@ -1,4 +1,4 @@ -use rhai::{Dynamic, Engine, EvalAltResult, LexError, ParseError, ParseErrorType, Position, INT}; +use rhai::{Dynamic, Engine, EvalAltResult, LexError, ParseErrorType, Position, INT}; #[test] fn test_custom_syntax() -> Result<(), Box> {