From 1d1e473ac45f5fb1418ddbbf3188ac6a534f4151 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 3 Jan 2022 23:16:47 +0800 Subject: [PATCH] Minor refactor. --- src/ast/ast.rs | 7 +++---- src/engine.rs | 14 ++++++-------- src/func/call.rs | 20 +++++++------------- src/func/callable_function.rs | 2 +- src/packages/arithmetic.rs | 2 +- src/packages/fn_basic.rs | 13 ++++++++----- src/packages/math_basic.rs | 11 ++--------- src/packages/time_basic.rs | 12 ++++++------ 8 files changed, 34 insertions(+), 47 deletions(-) diff --git a/src/ast/ast.rs b/src/ast/ast.rs index 1f9ec131..3a7b96b9 100644 --- a/src/ast/ast.rs +++ b/src/ast/ast.rs @@ -124,10 +124,9 @@ impl AST { #[inline(always)] #[must_use] pub fn source(&self) -> Option<&str> { - if self.source.is_empty() { - None - } else { - Some(&self.source) + match self.source.as_str() { + "" => None, + s => Some(s), } } /// Get a reference to the source. diff --git a/src/engine.rs b/src/engine.rs index edfb6b70..c6d44849 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -890,10 +890,9 @@ impl<'x, 'px, 'pt> EvalContext<'_, 'x, 'px, '_, '_, '_, '_, 'pt> { #[inline(always)] #[must_use] pub fn source(&self) -> Option<&str> { - if self.global.source.is_empty() { - None - } else { - Some(&self.global.source) + match self.global.source.as_str() { + "" => None, + s => Some(s), } } /// The current [`Scope`]. @@ -3241,10 +3240,9 @@ impl Engine { { use crate::ModuleResolver; - let source = if global.source.is_empty() { - None - } else { - Some(global.source.as_str()) + let source = match global.source.as_str() { + "" => None, + s => Some(s), }; let path_pos = expr.position(); diff --git a/src/func/call.rs b/src/func/call.rs index 99d241cc..fcc252fa 100644 --- a/src/func/call.rs +++ b/src/func/call.rs @@ -155,7 +155,7 @@ impl Engine { ) -> String { format!( "{}{}{} ({})", - namespace.map_or(String::new(), |ns| ns.to_string()), + namespace.map_or_else(|| String::new(), |ns| ns.to_string()), if namespace.is_some() { Token::DoubleColon.literal_syntax() } else { @@ -372,14 +372,9 @@ impl Engine { } // Run external function - let source = if source.is_empty() { - if parent_source.is_empty() { - None - } else { - Some(parent_source.as_str()) - } - } else { - Some(source.as_str()) + let source = match (source.as_str(), parent_source.as_str()) { + ("", "") => None, + ("", s) | (s, _) => Some(s), }; let context = (self, name, source, &*global, lib, pos).into(); @@ -424,10 +419,9 @@ impl Engine { pos, ) })?; - let source = if global.source.is_empty() { - None - } else { - Some(global.source.as_str()) + let source = match global.source.as_str() { + "" => None, + s => Some(s), }; (debug(&text, source, pos).into(), false) } else { diff --git a/src/func/callable_function.rs b/src/func/callable_function.rs index 65504c42..0402ba87 100644 --- a/src/func/callable_function.rs +++ b/src/func/callable_function.rs @@ -50,7 +50,7 @@ impl fmt::Display for CallableFunction { Self::Plugin(_) => write!(f, "PluginFunction"), #[cfg(not(feature = "no_function"))] - CallableFunction::Script(s) => fmt::Display::fmt(s, f), + Self::Script(s) => fmt::Display::fmt(s, f), } } } diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index 72f441f7..8c700ed4 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -9,7 +9,7 @@ use std::prelude::v1::*; #[cfg(not(feature = "no_float"))] use num_traits::Float; -#[inline(never)] +#[inline] pub fn make_err(msg: impl Into) -> RhaiError { ERR::ErrorArithmetic(msg.into(), Position::NONE).into() } diff --git a/src/packages/fn_basic.rs b/src/packages/fn_basic.rs index 6037761f..66be2772 100644 --- a/src/packages/fn_basic.rs +++ b/src/packages/fn_basic.rs @@ -44,7 +44,7 @@ fn collect_fn_metadata(ctx: NativeCallContext) -> crate::Array { fn make_metadata( dict: &BTreeSet, namespace: Option, - f: &ScriptFnDef, + func: &ScriptFnDef, ) -> Map { const DICT: &str = "key exists"; @@ -53,10 +53,13 @@ fn collect_fn_metadata(ctx: NativeCallContext) -> crate::Array { if let Some(ns) = namespace { map.insert(dict.get("namespace").expect(DICT).clone(), ns.into()); } - map.insert(dict.get("name").expect(DICT).clone(), f.name.clone().into()); + map.insert( + dict.get("name").expect(DICT).clone(), + func.name.clone().into(), + ); map.insert( dict.get("access").expect(DICT).clone(), - match f.access { + match func.access { FnAccess::Public => dict.get("public").expect(DICT).clone(), FnAccess::Private => dict.get("private").expect(DICT).clone(), } @@ -64,11 +67,11 @@ fn collect_fn_metadata(ctx: NativeCallContext) -> crate::Array { ); map.insert( dict.get("is_anonymous").expect(DICT).clone(), - f.name.starts_with(crate::engine::FN_ANONYMOUS).into(), + func.name.starts_with(crate::engine::FN_ANONYMOUS).into(), ); map.insert( dict.get("params").expect(DICT).clone(), - f.params + func.params .iter() .cloned() .map(Into::into) diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index 0b902666..d664033e 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -18,13 +18,6 @@ use rust_decimal::Decimal; #[cfg(feature = "decimal")] use super::arithmetic::make_err; -#[allow(dead_code)] -#[cfg(feature = "only_i32")] -pub const MAX_INT: INT = i32::MAX; -#[allow(dead_code)] -#[cfg(not(feature = "only_i32"))] -pub const MAX_INT: INT = i64::MAX; - macro_rules! gen_conversion_as_functions { ($root:ident => $func_name:ident ( $($arg_type:ident),+ ) -> $result_type:ty) => { pub mod $root { $(pub mod $arg_type { @@ -264,7 +257,7 @@ mod float_functions { } #[rhai_fn(name = "to_int", return_raw)] pub fn f32_to_int(x: f32) -> RhaiResultOf { - if cfg!(not(feature = "unchecked")) && x > (MAX_INT as f32) { + if cfg!(not(feature = "unchecked")) && x > (INT::MAX as f32) { Err( ERR::ErrorArithmetic(format!("Integer overflow: to_int({})", x), Position::NONE) .into(), @@ -275,7 +268,7 @@ mod float_functions { } #[rhai_fn(name = "to_int", return_raw)] pub fn f64_to_int(x: f64) -> RhaiResultOf { - if cfg!(not(feature = "unchecked")) && x > (MAX_INT as f64) { + if cfg!(not(feature = "unchecked")) && x > (INT::MAX as f64) { Err( ERR::ErrorArithmetic(format!("Integer overflow: to_int({})", x), Position::NONE) .into(), diff --git a/src/packages/time_basic.rs b/src/packages/time_basic.rs index f4a6e7df..270b89a7 100644 --- a/src/packages/time_basic.rs +++ b/src/packages/time_basic.rs @@ -1,6 +1,6 @@ #![cfg(not(feature = "no_std"))] -use super::{arithmetic::make_err as make_arithmetic_err, math_basic::MAX_INT}; +use super::arithmetic::make_err as make_arithmetic_err; use crate::plugin::*; use crate::{def_package, Dynamic, EvalAltResult, RhaiResult, RhaiResultOf, INT}; @@ -42,7 +42,7 @@ mod time_functions { { let seconds = timestamp.elapsed().as_secs(); - if cfg!(not(feature = "unchecked")) && seconds > (MAX_INT as u64) { + if cfg!(not(feature = "unchecked")) && seconds > (INT::MAX as u64) { Err(make_arithmetic_err(format!( "Integer overflow for timestamp.elapsed: {}", seconds @@ -69,7 +69,7 @@ mod time_functions { if timestamp2 > timestamp1 { let seconds = (timestamp2 - timestamp1).as_secs(); - if cfg!(not(feature = "unchecked")) && seconds > (MAX_INT as u64) { + if cfg!(not(feature = "unchecked")) && seconds > (INT::MAX as u64) { Err(make_arithmetic_err(format!( "Integer overflow for timestamp duration: -{}", seconds @@ -80,7 +80,7 @@ mod time_functions { } else { let seconds = (timestamp1 - timestamp2).as_secs(); - if cfg!(not(feature = "unchecked")) && seconds > (MAX_INT as u64) { + if cfg!(not(feature = "unchecked")) && seconds > (INT::MAX as u64) { Err(make_arithmetic_err(format!( "Integer overflow for timestamp duration: {}", seconds @@ -97,7 +97,7 @@ mod time_functions { if seconds < 0.0 { subtract_impl(timestamp, -seconds) } else if cfg!(not(feature = "unchecked")) { - if seconds > (MAX_INT as FLOAT) { + if seconds > (INT::MAX as FLOAT) { Err(make_arithmetic_err(format!( "Integer overflow for timestamp add: {}", seconds @@ -120,7 +120,7 @@ mod time_functions { if seconds < 0.0 { add_impl(timestamp, -seconds) } else if cfg!(not(feature = "unchecked")) { - if seconds > (MAX_INT as FLOAT) { + if seconds > (INT::MAX as FLOAT) { Err(make_arithmetic_err(format!( "Integer overflow for timestamp add: {}", seconds