From 9c9f550200a9e8b939161934529f9c3dc104cdf4 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 2 Jul 2020 21:46:08 +0800 Subject: [PATCH] Minor refactor. --- README.md | 2 +- doc/src/about/features.md | 2 +- src/error.rs | 14 ++++++-------- src/module.rs | 8 ++------ src/result.rs | 14 +++++++------- 5 files changed, 17 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 952d9bf3..681ae280 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Features * Freely pass Rust variables/constants into a script via an external [`Scope`](https://schungx.github.io/rhai/rust/scope.html). * Easily [call a script-defined function](https://schungx.github.io/rhai/engine/call-fn.html) from Rust. * Fairly low compile-time overhead. -* Fairly efficient evaluation (1 million iterations in 0.25 sec on a single core, 2.3 GHz Linux VM). +* Fairly efficient evaluation (1 million iterations in 0.4 sec on a single core, 2.3 GHz Linux VM). * Relatively little `unsafe` code (yes there are some for performance reasons, and most `unsafe` code is limited to one single source file, all with names starting with `"unsafe_"`). * Re-entrant scripting engine can be made `Send + Sync` (via the [`sync`] feature). diff --git a/doc/src/about/features.md b/doc/src/about/features.md index c012353a..0768115f 100644 --- a/doc/src/about/features.md +++ b/doc/src/about/features.md @@ -22,7 +22,7 @@ Fast * Fairly low compile-time overhead. -* Fairly efficient evaluation (1 million iterations in 0.25 sec on a single core, 2.3 GHz Linux VM). +* Fairly efficient evaluation (1 million iterations in 0.4 sec on a single core, 2.3 GHz Linux VM). * Scripts are [optimized][script optimization] (useful for template-based machine-generated scripts) for repeated evaluations. diff --git a/src/error.rs b/src/error.rs index 58423ef9..61f2c58f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -49,7 +49,7 @@ impl fmt::Display for LexError { "Length of string literal exceeds the maximum limit ({})", max ), - Self::ImproperSymbol(s) => write!(f, "{}", s), + Self::ImproperSymbol(s) => f.write_str(s), } } } @@ -185,18 +185,16 @@ impl fmt::Display for ParseErrorType { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::BadInput(s) | ParseErrorType::MalformedCallExpr(s) => { - write!(f, "{}", if s.is_empty() { self.desc() } else { s }) + f.write_str(if s.is_empty() { self.desc() } else { s }) } Self::ForbiddenConstantExpr(s) => { write!(f, "Expecting a constant to assign to '{}'", s) } Self::UnknownOperator(s) => write!(f, "{}: '{}'", self.desc(), s), - Self::MalformedIndexExpr(s) => { - write!(f, "{}", if s.is_empty() { self.desc() } else { s }) - } + Self::MalformedIndexExpr(s) => f.write_str(if s.is_empty() { self.desc() } else { s }), - Self::MalformedInExpr(s) => write!(f, "{}", if s.is_empty() { self.desc() } else { s }), + Self::MalformedInExpr(s) => f.write_str(if s.is_empty() { self.desc() } else { s }), Self::DuplicatedProperty(s) => { write!(f, "Duplicated property '{}' for object map literal", s) @@ -222,12 +220,12 @@ impl fmt::Display for ParseErrorType { Self::MissingToken(token, s) => write!(f, "Expecting '{}' {}", token, s), - Self::AssignmentToConstant(s) if s.is_empty() => write!(f, "{}", self.desc()), + Self::AssignmentToConstant(s) if s.is_empty() => f.write_str(self.desc()), Self::AssignmentToConstant(s) => write!(f, "Cannot assign to constant '{}'", s), Self::LiteralTooLarge(typ, max) => { write!(f, "{} exceeds the maximum limit ({})", typ, max) } - _ => write!(f, "{}", self.desc()), + _ => f.write_str(self.desc()), } } } diff --git a/src/module.rs b/src/module.rs index 3cdeb711..c7198d83 100644 --- a/src/module.rs +++ b/src/module.rs @@ -876,12 +876,10 @@ impl Module { .functions .iter() .filter(|(_, (_, _, _, v))| match v { - CallableFunction::Pure(_) - | CallableFunction::Method(_) - | CallableFunction::Iterator(_) => true, CallableFunction::Script(ref f) => { filter(f.access, f.name.as_str(), f.params.len()) } + _ => true, }) .map(|(&k, v)| (k, v.clone())), ); @@ -897,10 +895,8 @@ impl Module { /// Filter out the functions, retaining only some based on a filter predicate. pub(crate) fn retain_functions(&mut self, filter: impl Fn(FnAccess, &str, usize) -> bool) { self.functions.retain(|_, (_, _, _, v)| match v { - CallableFunction::Pure(_) - | CallableFunction::Method(_) - | CallableFunction::Iterator(_) => true, CallableFunction::Script(ref f) => filter(f.access, f.name.as_str(), f.params.len()), + _ => true, }); self.all_functions.clear(); diff --git a/src/result.rs b/src/result.rs index 06bc80d3..d6351b25 100644 --- a/src/result.rs +++ b/src/result.rs @@ -197,16 +197,16 @@ impl fmt::Display for EvalAltResult { | Self::ErrorTooManyOperations(_) | Self::ErrorTooManyModules(_) | Self::ErrorStackOverflow(_) - | Self::ErrorTerminated(_) => write!(f, "{}", desc)?, + | Self::ErrorTerminated(_) => f.write_str(desc)?, - Self::ErrorRuntime(s, _) => write!(f, "{}", if s.is_empty() { desc } else { s })?, + Self::ErrorRuntime(s, _) => f.write_str(if s.is_empty() { desc } else { s })?, Self::ErrorAssignmentToConstant(s, _) => write!(f, "{}: '{}'", desc, s)?, Self::ErrorMismatchOutputType(s, _) => write!(f, "{}: {}", desc, s)?, - Self::ErrorArithmetic(s, _) => write!(f, "{}", s)?, + Self::ErrorArithmetic(s, _) => f.write_str(s)?, - Self::ErrorLoopBreak(_, _) => write!(f, "{}", desc)?, - Self::Return(_, _) => write!(f, "{}", desc)?, + Self::ErrorLoopBreak(_, _) => f.write_str(desc)?, + Self::Return(_, _) => f.write_str(desc)?, Self::ErrorBooleanArgMismatch(op, _) => { write!(f, "{} operator expects boolean operands", op)? @@ -215,7 +215,7 @@ impl fmt::Display for EvalAltResult { Self::ErrorArrayBounds(_, index, _) if *index < 0 => { write!(f, "{}: {} < 0", desc, index)? } - Self::ErrorArrayBounds(0, _, _) => write!(f, "{}", desc)?, + Self::ErrorArrayBounds(0, _, _) => f.write_str(desc)?, Self::ErrorArrayBounds(1, index, _) => write!( f, "Array index {} is out of bounds: only one element in the array", @@ -229,7 +229,7 @@ impl fmt::Display for EvalAltResult { Self::ErrorStringBounds(_, index, _) if *index < 0 => { write!(f, "{}: {} < 0", desc, index)? } - Self::ErrorStringBounds(0, _, _) => write!(f, "{}", desc)?, + Self::ErrorStringBounds(0, _, _) => f.write_str(desc)?, Self::ErrorStringBounds(1, index, _) => write!( f, "String index {} is out of bounds: only one character in the string",