Reduce usage of Default::default()
This commit is contained in:
parent
5d3a22ab6f
commit
6510b617fe
@ -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
|
||||
------------
|
||||
|
@ -49,7 +49,7 @@ impl ExportedParams for ExportedModParams {
|
||||
|
||||
fn from_info(info: ExportInfo) -> syn::Result<Self> {
|
||||
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 {
|
||||
|
20
src/ast.rs
20
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]
|
||||
|
@ -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> ");
|
||||
|
@ -198,7 +198,7 @@ impl Engine {
|
||||
) -> Result<&mut Self, ParseError> {
|
||||
use markers::*;
|
||||
|
||||
let mut segments: StaticVec<ImmutableString> = Default::default();
|
||||
let mut segments = StaticVec::<ImmutableString>::new();
|
||||
|
||||
for s in symbols {
|
||||
let s = s.as_ref().trim();
|
||||
|
@ -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());
|
||||
|
@ -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<Map, Box<EvalAltResult>> {
|
||||
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<str>,
|
||||
args: impl crate::FuncArgs,
|
||||
) -> Result<T, Box<EvalAltResult>> {
|
||||
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<String> {
|
||||
let mut signatures: Vec<_> = Default::default();
|
||||
let mut signatures = Vec::new();
|
||||
|
||||
signatures.extend(self.global_namespace().gen_fn_signatures());
|
||||
|
||||
|
16
src/lib.rs
16
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<T> = 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<T> = 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<T> = 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<T> = smallvec::SmallVec<[T; 3]>;
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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())
|
||||
|
24
src/parse.rs
24
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<Identifier, crate::Dynamic> = Default::default();
|
||||
let mut map = StaticVec::<(Ident, Expr)>::new();
|
||||
let mut template = BTreeMap::<Identifier, crate::Dynamic>::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<Expr> = Default::default();
|
||||
let mut segments = StaticVec::<Expr>::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<Expr, ParseError> {
|
||||
let mut settings = settings;
|
||||
let mut keywords: StaticVec<Expr> = Default::default();
|
||||
let mut segments: StaticVec<_> = Default::default();
|
||||
let mut tokens: StaticVec<_> = Default::default();
|
||||
let mut keywords = StaticVec::<Expr>::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<String> = Default::default();
|
||||
let mut comments = StaticVec::<String>::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 {
|
||||
|
@ -142,7 +142,7 @@ impl<'d> Visitor<'d> for DynamicVisitor {
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
fn visit_seq<A: SeqAccess<'d>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
|
||||
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<M: MapAccess<'d>>(self, mut map: M) -> Result<Self::Value, M::Error> {
|
||||
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);
|
||||
|
@ -207,6 +207,13 @@ struct ModuleMetadata {
|
||||
pub functions: Vec<FnMetadata>,
|
||||
}
|
||||
|
||||
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<String> {
|
||||
let _ast = ast;
|
||||
let mut global: ModuleMetadata = Default::default();
|
||||
let mut global = ModuleMetadata::new();
|
||||
|
||||
if include_global {
|
||||
self.global_modules
|
||||
|
@ -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<u32> = 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() {
|
||||
|
Loading…
Reference in New Issue
Block a user