From 6791ef64da499224fc70d00fd13da613204ec67a Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 24 Nov 2022 11:00:47 +0800 Subject: [PATCH] Remove crate:: from types. --- src/packages/array_basic.rs | 8 +++-- src/packages/bit_field.rs | 4 +-- src/packages/blob_basic.rs | 6 ++-- src/packages/debugging.rs | 12 ++++--- src/packages/iter_basic.rs | 13 +++----- src/packages/math_basic.rs | 4 --- src/packages/string_basic.rs | 8 ++--- src/packages/string_more.rs | 64 +++++++++++++++++------------------- src/parser.rs | 16 ++++----- 9 files changed, 62 insertions(+), 73 deletions(-) diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 527b1001..9309a2f6 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -230,6 +230,8 @@ pub mod array_functions { // Check if array will be over max size limit #[cfg(not(feature = "unchecked"))] { + use crate::types::dynamic::Union; + if _ctx.engine().max_array_size() > 0 && len > _ctx.engine().max_array_size() { return Err( ERR::ErrorDataTooLarge("Size of array".to_string(), Position::NONE).into(), @@ -237,10 +239,10 @@ pub mod array_functions { } let check_sizes = match item.0 { - crate::types::dynamic::Union::Str(..) => true, - crate::types::dynamic::Union::Array(..) => true, + Union::Str(..) => true, + Union::Array(..) => true, #[cfg(not(feature = "no_object"))] - crate::types::dynamic::Union::Map(..) => true, + Union::Map(..) => true, _ => false, }; diff --git a/src/packages/bit_field.rs b/src/packages/bit_field.rs index 5ecde54d..9da781cf 100644 --- a/src/packages/bit_field.rs +++ b/src/packages/bit_field.rs @@ -143,7 +143,7 @@ mod bit_field_functions { } // 2^bits - 1 - let mask = ((2 as UNSIGNED_INT).pow(bits as u32) - 1) as crate::INT; + let mask = ((2 as UNSIGNED_INT).pow(bits as u32) - 1) as INT; Ok(((value & (mask << bit)) >> bit) & mask) } @@ -230,7 +230,7 @@ mod bit_field_functions { } // 2^bits - 1 - let mask = ((2 as UNSIGNED_INT).pow(bits as u32) - 1) as crate::INT; + let mask = ((2 as UNSIGNED_INT).pow(bits as u32) - 1) as INT; *value &= !(mask << bit); *value |= (new_value & mask) << bit; diff --git a/src/packages/blob_basic.rs b/src/packages/blob_basic.rs index 18e970b9..e8b0b9e8 100644 --- a/src/packages/blob_basic.rs +++ b/src/packages/blob_basic.rs @@ -5,7 +5,7 @@ use crate::module::ModuleFlags; use crate::plugin::*; use crate::{ def_package, Array, Blob, Dynamic, ExclusiveRange, InclusiveRange, NativeCallContext, Position, - RhaiResultOf, INT, INT_BYTES, MAX_USIZE_INT, + RhaiResultOf, ERR, INT, INT_BYTES, MAX_USIZE_INT, }; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -363,9 +363,7 @@ pub mod blob_functions { // Check if blob will be over max size limit if _ctx.engine().max_array_size() > 0 && len > _ctx.engine().max_array_size() { - return Err( - crate::ERR::ErrorDataTooLarge("Size of BLOB".to_string(), Position::NONE).into(), - ); + return Err(ERR::ErrorDataTooLarge("Size of BLOB".to_string(), Position::NONE).into()); } if len > blob.len() { diff --git a/src/packages/debugging.rs b/src/packages/debugging.rs index aaf01d01..1256102a 100644 --- a/src/packages/debugging.rs +++ b/src/packages/debugging.rs @@ -34,16 +34,18 @@ mod debugging_functions { #[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_index"))] pub fn back_trace(ctx: NativeCallContext) -> Array { + use crate::debugger::CallStackFrame; + ctx.global_runtime_state() .debugger .call_stack() .iter() .rev() - .filter(|crate::debugger::CallStackFrame { fn_name, args, .. }| { + .filter(|CallStackFrame { fn_name, args, .. }| { fn_name.as_str() != "back_trace" || !args.is_empty() }) .map( - |frame @ crate::debugger::CallStackFrame { + |frame @ CallStackFrame { fn_name: _fn_name, args: _args, source: _source, @@ -53,6 +55,8 @@ mod debugging_functions { #[cfg(not(feature = "no_object"))] { + use crate::INT; + let mut map = Map::new(); map.insert("display".into(), display.into()); map.insert("fn_name".into(), _fn_name.into()); @@ -63,10 +67,10 @@ mod debugging_functions { map.insert("source".into(), source.into()); } if !_pos.is_none() { - map.insert("line".into(), (_pos.line().unwrap() as crate::INT).into()); + map.insert("line".into(), (_pos.line().unwrap() as INT).into()); map.insert( "position".into(), - (_pos.position().unwrap_or(0) as crate::INT).into(), + (_pos.position().unwrap_or(0) as INT).into(), ); } Dynamic::from_map(map) diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index a611d0d1..b7a60dd5 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -2,7 +2,7 @@ use crate::eval::calc_index; use crate::module::ModuleFlags; use crate::plugin::*; use crate::{ - def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, INT, INT_BITS, MAX_USIZE_INT, + def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, ERR, INT, INT_BITS, MAX_USIZE_INT, }; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -66,14 +66,11 @@ impl StepRange { if let Some(n) = add(from, step) { #[cfg(not(feature = "unchecked"))] if n == from { - return Err(crate::ERR::ErrorInFunctionCall( + return Err(ERR::ErrorInFunctionCall( "range".to_string(), String::new(), - crate::ERR::ErrorArithmetic( - "step value cannot be zero".to_string(), - Position::NONE, - ) - .into(), + ERR::ErrorArithmetic("step value cannot be zero".to_string(), Position::NONE) + .into(), Position::NONE, ) .into()); @@ -128,7 +125,7 @@ pub struct BitRange(INT, usize); impl BitRange { pub fn new(value: INT, from: INT, len: INT) -> RhaiResultOf { let from = calc_index(INT_BITS, from, true, || { - crate::ERR::ErrorBitFieldBounds(INT_BITS, from, Position::NONE).into() + ERR::ErrorBitFieldBounds(INT_BITS, from, Position::NONE).into() })?; let len = if len < 0 { diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index 8e52946d..f5434279 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -159,8 +159,6 @@ mod int_functions { #[cfg(not(feature = "no_float"))] #[export_module] mod trig_functions { - use crate::FLOAT; - /// Return the sine of the floating-point number in radians. pub fn sin(x: FLOAT) -> FLOAT { x.sin() @@ -223,8 +221,6 @@ mod trig_functions { #[cfg(not(feature = "no_float"))] #[export_module] mod float_functions { - use crate::FLOAT; - /// Return the natural number _e_. #[rhai_fn(name = "E")] pub fn e() -> FLOAT { diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index 266a485c..686951f7 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -1,6 +1,6 @@ use crate::module::ModuleFlags; use crate::plugin::*; -use crate::{def_package, FnPtr, SmartString, INT}; +use crate::{def_package, FnPtr, ImmutableString, SmartString, INT}; use std::any::TypeId; use std::fmt::{Binary, LowerHex, Octal, Write}; #[cfg(feature = "no_std")] @@ -26,7 +26,7 @@ def_package! { // Register characters iterator #[cfg(not(feature = "no_index"))] lib.set_iter(TypeId::of::(), |value| Box::new( - value.cast::().chars().map(Into::into).collect::().into_iter() + value.cast::().chars().map(Into::into).collect::().into_iter() )); } } @@ -38,7 +38,7 @@ pub fn print_with_func( fn_name: &str, ctx: &NativeCallContext, value: &mut Dynamic, -) -> crate::ImmutableString { +) -> ImmutableString { match ctx.call_native_fn_raw(fn_name, true, &mut [value]) { Ok(result) if result.is_string() => { result.into_immutable_string().expect("`ImmutableString`") @@ -50,8 +50,6 @@ pub fn print_with_func( #[export_module] mod print_debug_functions { - use crate::ImmutableString; - /// Convert the value of the `item` into a string. #[rhai_fn(name = "print", pure)] pub fn print_generic(ctx: NativeCallContext, item: &mut Dynamic) -> ImmutableString { diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 2f00e672..5670e421 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -1,8 +1,8 @@ use crate::module::ModuleFlags; use crate::plugin::*; use crate::{ - def_package, Dynamic, ExclusiveRange, InclusiveRange, RhaiResultOf, StaticVec, INT, - MAX_USIZE_INT, + def_package, Dynamic, ExclusiveRange, ImmutableString, InclusiveRange, RhaiResultOf, + SmartString, StaticVec, INT, MAX_USIZE_INT, ERR, Position }; #[cfg(feature = "no_std")] use std::prelude::v1::*; @@ -21,8 +21,6 @@ def_package! { #[export_module] mod string_functions { - use crate::{ImmutableString, SmartString}; - #[rhai_fn(name = "+", pure)] pub fn add_append( ctx: NativeCallContext, @@ -135,7 +133,7 @@ mod string_functions { } #[rhai_fn(name = "+=", name = "append")] pub fn add(string: &mut ImmutableString, utf8: Blob) { - let mut s = crate::SmartString::from(string.as_str()); + let mut s = SmartString::from(string.as_str()); if !utf8.is_empty() { s.push_str(&String::from_utf8_lossy(&utf8)); *string = s.into(); @@ -155,6 +153,25 @@ mod string_functions { s.into() } + + /// Convert the string into an UTF-8 encoded byte-stream as a BLOB. + /// + /// # Example + /// + /// ```rhai + /// let text = "朝には紅顔ありて夕べには白骨となる"; + /// + /// let bytes = text.to_blob(); + /// + /// print(bytes.len()); // prints 51 + /// ``` + pub fn to_blob(string: &str) -> Blob { + if string.is_empty() { + Blob::new() + } else { + string.as_bytes().into() + } + } } /// Return the length of the string, in number of characters. @@ -196,25 +213,6 @@ mod string_functions { string.len() as INT } } - /// Convert the string into an UTF-8 encoded byte-stream as a BLOB. - /// - /// # Example - /// - /// ```rhai - /// let text = "朝には紅顔ありて夕べには白骨となる"; - /// - /// let bytes = text.to_blob(); - /// - /// print(bytes.len()); // prints 51 - /// ``` - #[cfg(not(feature = "no_index"))] - pub fn to_blob(string: &str) -> crate::Blob { - if string.is_empty() { - crate::Blob::new() - } else { - string.as_bytes().into() - } - } /// Remove all occurrences of a sub-string from the string. /// /// # Example @@ -1226,9 +1224,9 @@ mod string_functions { // Check if string will be over max size limit if _ctx.engine().max_string_size() > 0 && len > _ctx.engine().max_string_size() { - return Err(crate::ERR::ErrorDataTooLarge( + return Err(ERR::ErrorDataTooLarge( "Length of string".to_string(), - crate::Position::NONE, + Position::NONE, ) .into()); } @@ -1244,9 +1242,9 @@ mod string_functions { if _ctx.engine().max_string_size() > 0 && string.len() > _ctx.engine().max_string_size() { - return Err(crate::ERR::ErrorDataTooLarge( + return Err(ERR::ErrorDataTooLarge( "Length of string".to_string(), - crate::Position::NONE, + Position::NONE, ) .into()); } @@ -1286,9 +1284,9 @@ mod string_functions { // Check if string will be over max size limit if _ctx.engine().max_string_size() > 0 && len > _ctx.engine().max_string_size() { - return Err(crate::ERR::ErrorDataTooLarge( + return Err(ERR::ErrorDataTooLarge( "Length of string".to_string(), - crate::Position::NONE, + Position::NONE, ) .into()); } @@ -1311,9 +1309,9 @@ mod string_functions { if _ctx.engine().max_string_size() > 0 && string.len() > _ctx.engine().max_string_size() { - return Err(crate::ERR::ErrorDataTooLarge( + return Err(ERR::ErrorDataTooLarge( "Length of string".to_string(), - crate::Position::NONE, + Position::NONE, ) .into()); } @@ -1324,7 +1322,7 @@ mod string_functions { #[cfg(not(feature = "no_index"))] pub mod arrays { - use crate::{Array, ImmutableString}; + use crate::Array; /// Split the string into two at the specified `index` position and return it both strings /// as an array. diff --git a/src/parser.rs b/src/parser.rs index a8559b5e..2ed24cd9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1454,15 +1454,13 @@ impl Engine { #[cfg(feature = "no_closure")] let options = self.options | (settings.options & LangOptions::STRICT_VAR); - let mut flags = (settings.flags + let flags = (settings.flags & !ParseSettingFlags::GLOBAL_LEVEL & ParseSettingFlags::BREAKABLE) | ParseSettingFlags::FN_SCOPE; #[cfg(not(feature = "no_closure"))] - { - flags |= ParseSettingFlags::CLOSURE_SCOPE; - } + let flags = flags | ParseSettingFlags::CLOSURE_SCOPE; let new_settings = ParseSettings { level: 0, @@ -3728,7 +3726,7 @@ impl Engine { &self, input: &mut TokenStream, state: &mut ParseState, - parent: &mut ParseState, + _parent: &mut ParseState, lib: &mut FnLib, settings: ParseSettings, ) -> ParseResult<(Expr, ScriptFnDef)> { @@ -3822,7 +3820,7 @@ impl Engine { #[cfg(not(feature = "no_closure"))] let expr = - Self::make_curry_from_externals(state, parent, lib, expr, externals, settings.pos); + Self::make_curry_from_externals(state, _parent, lib, expr, externals, settings.pos); Ok((expr, script)) } @@ -3837,11 +3835,9 @@ impl Engine { ) -> ParseResult { let mut functions = StraightHashMap::default(); - let mut options = self.options & !LangOptions::STMT_EXPR & !LangOptions::LOOP_EXPR; + let options = self.options & !LangOptions::STMT_EXPR & !LangOptions::LOOP_EXPR; #[cfg(not(feature = "no_function"))] - { - options &= !LangOptions::ANON_FN; - } + let options = options & !LangOptions::ANON_FN; let mut settings = ParseSettings { level: 0,