diff --git a/CHANGELOG.md b/CHANGELOG.md index ea4728ae..760c5bb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Bug fixes * Custom syntax starting with a disabled standard keyword now works properly. * When calling `Engine::call_fn`, new variables defined during evaluation of the body script are removed and no longer spill into the function call. +* `NamespaceRef::new` is fixed. Enhancements ------------ diff --git a/codegen/src/module.rs b/codegen/src/module.rs index 7e7421a5..c5d20b51 100644 --- a/codegen/src/module.rs +++ b/codegen/src/module.rs @@ -49,7 +49,7 @@ impl ExportedParams for ExportedModParams { fn from_info(info: ExportInfo) -> syn::Result { let ExportInfo { items: attrs, .. } = info; - let mut name = Default::default(); + let mut name = String::new(); let mut skip = false; let mut scope = None; for attr in attrs { diff --git a/src/ast.rs b/src/ast.rs index 871c3efc..a79c8887 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -201,13 +201,7 @@ pub struct AST { impl Default for AST { #[inline(always)] fn default() -> Self { - Self { - source: None, - body: Default::default(), - functions: Default::default(), - #[cfg(not(feature = "no_module"))] - resolver: None, - } + Self::new_empty() } } @@ -227,6 +221,18 @@ impl AST { resolver: None, } } + /// Create an empty [`AST`]. + #[inline] + #[must_use] + pub fn new_empty() -> Self { + Self { + source: None, + body: Default::default(), + functions: Default::default(), + #[cfg(not(feature = "no_module"))] + resolver: None, + } + } /// Create a new [`AST`] with a source name. #[inline(always)] #[must_use] diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs index 62dc3869..f28da37b 100644 --- a/src/bin/rhai-repl.rs +++ b/src/bin/rhai-repl.rs @@ -163,9 +163,9 @@ fn main() { // REPL loop let mut input = String::new(); - let mut main_ast: AST = Default::default(); - let mut ast_u: AST = Default::default(); - let mut ast: AST = Default::default(); + let mut main_ast = AST::new_empty(); + let mut ast_u = AST::new_empty(); + let mut ast = AST::new_empty(); 'main_loop: loop { print!("rhai-repl> "); diff --git a/src/custom_syntax.rs b/src/custom_syntax.rs index 678afce3..eaee3c27 100644 --- a/src/custom_syntax.rs +++ b/src/custom_syntax.rs @@ -198,7 +198,7 @@ impl Engine { ) -> Result<&mut Self, ParseError> { use markers::*; - let mut segments: StaticVec = Default::default(); + let mut segments = StaticVec::::new(); for s in symbols { let s = s.as_ref().trim(); diff --git a/src/engine.rs b/src/engine.rs index 3953ad1c..bc7ff71c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -2766,7 +2766,7 @@ impl Engine { } #[cfg(not(feature = "no_object"))] _ => { - let mut err_map: Map = Default::default(); + let mut err_map = Map::new(); let err_pos = err.take_position(); err_map.insert("message".into(), err.to_string().into()); diff --git a/src/engine_api.rs b/src/engine_api.rs index 40c24c17..1e786638 100644 --- a/src/engine_api.rs +++ b/src/engine_api.rs @@ -961,7 +961,7 @@ impl Engine { let remainder = iter.next().expect("name contains separator").trim(); if !root.contains_key(sub_module) { - let mut m: Module = Default::default(); + let mut m = Module::new(); register_static_module_raw(m.sub_modules_mut(), remainder, module); m.build_index(); root.insert(sub_module.into(), m.into()); @@ -1351,7 +1351,7 @@ impl Engine { json: &str, has_null: bool, ) -> Result> { - let mut scope = Default::default(); + let mut scope = Scope::new(); let json_text = json.trim_start(); let scripts = if json_text.starts_with(Token::MapStart.literal_syntax()) { [json_text, ""] @@ -1860,7 +1860,7 @@ impl Engine { name: impl AsRef, args: impl crate::FuncArgs, ) -> Result> { - let mut arg_values: crate::StaticVec<_> = Default::default(); + let mut arg_values = crate::StaticVec::new(); args.parse(&mut arg_values); let mut args: crate::StaticVec<_> = arg_values.iter_mut().collect(); let name = name.as_ref(); @@ -2053,7 +2053,7 @@ impl Engine { #[inline] #[must_use] pub fn gen_fn_signatures(&self, include_packages: bool) -> Vec { - let mut signatures: Vec<_> = Default::default(); + let mut signatures = Vec::new(); signatures.extend(self.global_namespace().gen_fn_signatures()); diff --git a/src/lib.rs b/src/lib.rs index 3a8ada23..d99f68f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -264,7 +264,7 @@ pub use module::NamespaceRef; /// being the third number, be reached, then, lobbest thou thy `SmallVec` towards thy heap, who, /// being slow and cache-naughty in My sight, shall snuff it." /// -/// # Explanation on the Number Three +/// # Why Three /// /// `StaticVec` is used frequently to keep small lists of items in inline (non-heap) storage in /// order to improve cache friendliness and reduce indirections. @@ -274,14 +274,14 @@ pub use module::NamespaceRef; /// in that matter) contain fewer than 4 arguments, the exception being closures that capture a /// large number of external variables. /// -/// In addition, most script blocks either contain many statements, or just a few lines; +/// In addition, most script blocks either contain many statements, or just one or two lines; /// most scripts load fewer than 4 external modules; most module paths contain fewer than 4 levels -/// (e.g. `std::collections::map::HashMap` is 4 levels). +/// (e.g. `std::collections::map::HashMap` is 4 levels and it is just about as long as they get). #[cfg(not(feature = "internals"))] type StaticVec = smallvec::SmallVec<[T; 3]>; -/// _(internals)_ Alias to [`smallvec`](https://crates.io/crates/smallvec), which is a specialized -/// [`Vec`] backed by a small, inline, fixed-size array when there are ≤ 3 items stored. +/// _(internals)_ Alias to [`smallvec::SmallVec<[T; 3]>`](https://crates.io/crates/smallvec), +/// which is a [`Vec`] backed by a small, inline, fixed-size array when there are ≤ 3 items stored. /// Exported under the `internals` feature only. /// /// # History @@ -299,7 +299,7 @@ type StaticVec = smallvec::SmallVec<[T; 3]>; /// being the third number, be reached, then, lobbest thou thy `SmallVec` towards thy heap, who, /// being slow and cache-naughty in My sight, shall snuff it." /// -/// # Explanation on the Number Three +/// # Why Three /// /// `StaticVec` is used frequently to keep small lists of items in inline (non-heap) storage in /// order to improve cache friendliness and reduce indirections. @@ -309,9 +309,9 @@ type StaticVec = smallvec::SmallVec<[T; 3]>; /// in that matter) contain fewer than 4 arguments, the exception being closures that capture a /// large number of external variables. /// -/// In addition, most script blocks either contain many statements, or just a few lines; +/// In addition, most script blocks either contain many statements, or just one or two lines; /// most scripts load fewer than 4 external modules; most module paths contain fewer than 4 levels -/// (e.g. `std::collections::map::HashMap` is 4 levels). +/// (e.g. `std::collections::map::HashMap` is 4 levels and it is just about as long as they get). #[cfg(feature = "internals")] pub type StaticVec = smallvec::SmallVec<[T; 3]>; diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index a8422e98..3062f48b 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -179,7 +179,7 @@ mod array_functions { if n as usize > array.len() { mem::take(array) } else { - let mut result: Array = Default::default(); + let mut result = Array::new(); result.extend(array.drain(array.len() - n as usize..)); result } @@ -189,7 +189,7 @@ mod array_functions { } else if start as usize >= array.len() { Default::default() } else { - let mut result: Array = Default::default(); + let mut result = Array::new(); result.extend(array.drain(start as usize..)); result } diff --git a/src/packages/fn_basic.rs b/src/packages/fn_basic.rs index e4496762..4a92e1cc 100644 --- a/src/packages/fn_basic.rs +++ b/src/packages/fn_basic.rs @@ -92,7 +92,7 @@ fn collect_fn_metadata(ctx: NativeCallContext) -> crate::Array { .map(|&s| s.into()) .collect(); - let mut list: Array = Default::default(); + let mut list = Array::new(); ctx.iter_namespaces() .flat_map(|m| m.iter_script_fn()) diff --git a/src/parse.rs b/src/parse.rs index 5ecca6a5..3dc45951 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -82,7 +82,7 @@ pub struct ParseState<'e> { /// Interned strings. interned_strings: IdentifierBuilder, /// Encapsulates a local stack with variable names to simulate an actual runtime scope. - stack: Vec<(Identifier, AccessMode)>, + stack: StaticVec<(Identifier, AccessMode)>, /// Size of the local variables stack upon entry of the current block scope. entry_stack_len: usize, /// Tracks a list of external variables (variables that are not explicitly declared in the scope). @@ -124,7 +124,7 @@ impl<'e> ParseState<'e> { #[cfg(not(feature = "no_closure"))] allow_capture: true, interned_strings: Default::default(), - stack: Vec::with_capacity(16), + stack: Default::default(), entry_stack_len: 0, #[cfg(not(feature = "no_module"))] modules: Default::default(), @@ -828,8 +828,8 @@ fn parse_map_literal( // #{ ... settings.pos = eat_token(input, Token::MapStart); - let mut map: StaticVec<(Ident, Expr)> = Default::default(); - let mut template: BTreeMap = Default::default(); + let mut map = StaticVec::<(Ident, Expr)>::new(); + let mut template = BTreeMap::::new(); loop { const MISSING_RBRACE: &str = "to end this object map literal"; @@ -1179,7 +1179,7 @@ fn parse_primary( // Interpolated string Token::InterpolatedString(_) => { - let mut segments: StaticVec = Default::default(); + let mut segments = StaticVec::::new(); if let (Token::InterpolatedString(s), pos) = input.next().expect(NEVER_ENDS) { segments.push(Expr::StringConstant(s.into(), pos)); @@ -1389,7 +1389,7 @@ fn parse_primary( if let Some((ref mut namespace, _)) = namespace { namespace.push(var_name_def); } else { - let mut ns: NamespaceRef = Default::default(); + let mut ns = NamespaceRef::new(); ns.push(var_name_def); namespace = Some((ns, 42)); } @@ -1977,9 +1977,9 @@ fn parse_custom_syntax( pos: Position, ) -> Result { let mut settings = settings; - let mut keywords: StaticVec = Default::default(); - let mut segments: StaticVec<_> = Default::default(); - let mut tokens: StaticVec<_> = Default::default(); + let mut keywords = StaticVec::::new(); + let mut segments = StaticVec::new(); + let mut tokens = StaticVec::new(); // Adjust the variables stack if syntax.scope_may_be_changed { @@ -2699,7 +2699,7 @@ fn parse_stmt( #[cfg(not(feature = "no_function"))] #[cfg(feature = "metadata")] let comments = { - let mut comments: StaticVec = Default::default(); + let mut comments = StaticVec::::new(); let mut comments_pos = Position::NONE; // Handle doc-comments. @@ -2982,7 +2982,7 @@ fn parse_fn( (_, pos) => return Err(PERR::FnMissingParams(name).into_err(*pos)), }; - let mut params: StaticVec<_> = Default::default(); + let mut params = StaticVec::new(); if !match_token(input, Token::RightParen).0 { let sep_err = format!("to separate the parameters of function '{}'", name); @@ -3115,7 +3115,7 @@ fn parse_anon_fn( #[cfg(not(feature = "unchecked"))] settings.ensure_level_within_max_limit(state.max_expr_depth)?; - let mut params_list: StaticVec<_> = Default::default(); + let mut params_list = StaticVec::new(); if input.next().expect(NEVER_ENDS).0 != Token::Or && !match_token(input, Token::Pipe).0 { loop { diff --git a/src/serde/deserialize.rs b/src/serde/deserialize.rs index 78bc91b6..de970d54 100644 --- a/src/serde/deserialize.rs +++ b/src/serde/deserialize.rs @@ -142,7 +142,7 @@ impl<'d> Visitor<'d> for DynamicVisitor { #[cfg(not(feature = "no_index"))] fn visit_seq>(self, mut seq: A) -> Result { - let mut arr: Array = Default::default(); + let mut arr = Array::new(); while let Some(v) = seq.next_element()? { arr.push(v); @@ -153,7 +153,7 @@ impl<'d> Visitor<'d> for DynamicVisitor { #[cfg(not(feature = "no_object"))] fn visit_map>(self, mut map: M) -> Result { - let mut m: Map = Default::default(); + let mut m = Map::new(); while let Some((k, v)) = map.next_entry::<&str, _>()? { m.insert(k.into(), v); diff --git a/src/serde/metadata.rs b/src/serde/metadata.rs index 68a96e34..2ff11a9f 100644 --- a/src/serde/metadata.rs +++ b/src/serde/metadata.rs @@ -207,6 +207,13 @@ struct ModuleMetadata { pub functions: Vec, } +impl ModuleMetadata { + #[inline(always)] + pub fn new() -> Self { + Default::default() + } +} + impl From<&crate::Module> for ModuleMetadata { fn from(module: &crate::Module) -> Self { let mut functions: Vec<_> = module.iter_fn().map(|f| f.into()).collect(); @@ -239,7 +246,7 @@ impl Engine { include_global: bool, ) -> serde_json::Result { let _ast = ast; - let mut global: ModuleMetadata = Default::default(); + let mut global = ModuleMetadata::new(); if include_global { self.global_modules diff --git a/src/token.rs b/src/token.rs index e1ff0045..98980ca5 100644 --- a/src/token.rs +++ b/src/token.rs @@ -1421,7 +1421,7 @@ fn get_next_token_inner( // digit ... ('0'..='9', _) => { - let mut result: smallvec::SmallVec<[char; 16]> = Default::default(); + let mut result = smallvec::SmallVec::<[char; 16]>::new(); let mut radix_base: Option = None; let mut valid: fn(char) -> bool = is_numeric_digit; result.push(c); @@ -1951,7 +1951,7 @@ fn get_identifier( start_pos: Position, first_char: char, ) -> Option<(Token, Position)> { - let mut result: smallvec::SmallVec<[char; 8]> = Default::default(); + let mut result = smallvec::SmallVec::<[char; 8]>::new(); result.push(first_char); while let Some(next_char) = stream.peek_next() {