From b9cbeb65d6ef9b87ac57bf541bea50f72607b042 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 11 Jun 2022 16:01:15 +0800 Subject: [PATCH] Use Option instead of once/empty. --- src/api/register.rs | 4 +--- src/func/args.rs | 6 +++--- src/func/hashing.rs | 19 +++++++++++++------ src/module/mod.rs | 7 +++---- src/packages/array_basic.rs | 16 ++++++++-------- src/serde/metadata.rs | 4 ++-- tests/call_fn.rs | 8 ++++---- 7 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/api/register.rs b/src/api/register.rs index 8d69f7d6..3c1a2ded 100644 --- a/src/api/register.rs +++ b/src/api/register.rs @@ -123,9 +123,7 @@ impl Engine { let param_type_names: crate::StaticVec<_> = F::param_names() .iter() .map(|ty| format!("_: {}", self.format_type_name(ty))) - .chain(std::iter::once( - self.format_type_name(F::return_type_name()).into(), - )) + .chain(Some(self.format_type_name(F::return_type_name()).into())) .collect(); #[cfg(feature = "metadata")] diff --git a/src/func/args.rs b/src/func/args.rs index 4d14280c..c80dfbbb 100644 --- a/src/func/args.rs +++ b/src/func/args.rs @@ -28,9 +28,9 @@ pub trait FuncArgs { /// /// impl FuncArgs for Options { /// fn parse>(self, args: &mut ARGS) { - /// args.extend(std::iter::once(self.foo.into())); - /// args.extend(std::iter::once(self.bar.into())); - /// args.extend(std::iter::once(self.baz.into())); + /// args.extend(Some(self.foo.into())); + /// args.extend(Some(self.bar.into())); + /// args.extend(Some(self.baz.into())); /// } /// } /// diff --git a/src/func/hashing.rs b/src/func/hashing.rs index 96bdba56..380da5db 100644 --- a/src/func/hashing.rs +++ b/src/func/hashing.rs @@ -5,7 +5,6 @@ use std::prelude::v1::*; use std::{ any::TypeId, hash::{BuildHasher, Hash, Hasher}, - iter::empty, }; /// Dummy hash value to map zeros to. This value can be anything. @@ -87,12 +86,16 @@ pub fn get_hasher() -> ahash::AHasher { /// The first module name is skipped. Hashing starts from the _second_ module in the chain. #[inline] #[must_use] -pub fn calc_qualified_var_hash<'a>(modules: impl Iterator, var_name: &str) -> u64 { +pub fn calc_qualified_var_hash<'a>( + modules: impl IntoIterator, + var_name: &str, +) -> u64 { let s = &mut get_hasher(); // We always skip the first module let mut len = 0; modules + .into_iter() .inspect(|_| len += 1) .skip(1) .for_each(|m| m.hash(s)); @@ -121,7 +124,7 @@ pub fn calc_qualified_var_hash<'a>(modules: impl Iterator, var_n #[inline] #[must_use] pub fn calc_qualified_fn_hash<'a>( - modules: impl Iterator, + modules: impl IntoIterator, fn_name: &str, num: usize, ) -> u64 { @@ -130,6 +133,7 @@ pub fn calc_qualified_fn_hash<'a>( // We always skip the first module let mut len = 0; modules + .into_iter() .inspect(|_| len += 1) .skip(1) .for_each(|m| m.hash(s)); @@ -154,7 +158,7 @@ pub fn calc_qualified_fn_hash<'a>( #[inline(always)] #[must_use] pub fn calc_fn_hash(fn_name: &str, num: usize) -> u64 { - calc_qualified_fn_hash(empty(), fn_name, num) + calc_qualified_fn_hash(None, fn_name, num) } /// Calculate a non-zero [`u64`] hash key from a list of parameter types. @@ -166,10 +170,13 @@ pub fn calc_fn_hash(fn_name: &str, num: usize) -> u64 { /// If the hash happens to be zero, it is mapped to `DEFAULT_HASH`. #[inline] #[must_use] -pub fn calc_fn_params_hash(params: impl Iterator) -> u64 { +pub fn calc_fn_params_hash(params: impl IntoIterator) -> u64 { let s = &mut get_hasher(); let mut len = 0; - params.inspect(|_| len += 1).for_each(|t| t.hash(s)); + params + .into_iter() + .inspect(|_| len += 1) + .for_each(|t| t.hash(s)); len.hash(s); match s.finish() { diff --git a/src/module/mod.rs b/src/module/mod.rs index c3b68758..1b80bc9f 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -17,7 +17,6 @@ use std::{ cmp::Ordering, collections::{BTreeMap, BTreeSet}, fmt, - iter::{empty, once}, ops::{Add, AddAssign}, }; @@ -214,7 +213,7 @@ impl FuncInfo { /// The first module name is skipped. Hashing starts from the _second_ module in the chain. #[inline] pub fn calc_native_fn_hash<'a>( - modules: impl Iterator, + modules: impl IntoIterator, fn_name: &str, params: &[TypeId], ) -> u64 { @@ -626,7 +625,7 @@ impl Module { let value = Dynamic::from(value); if self.indexed { - let hash_var = crate::calc_qualified_var_hash(once(""), &ident); + let hash_var = crate::calc_qualified_var_hash(Some(""), &ident); self.all_variables.insert(hash_var, value.clone()); } self.variables.insert(ident, value); @@ -981,7 +980,7 @@ impl Module { (names, return_type) }; - let hash_fn = calc_native_fn_hash(empty::<&str>(), name.as_ref(), ¶m_types); + let hash_fn = calc_native_fn_hash(None, name.as_ref(), ¶m_types); self.functions.insert( hash_fn, diff --git a/src/packages/array_basic.rs b/src/packages/array_basic.rs index 273f42bf..bda254e2 100644 --- a/src/packages/array_basic.rs +++ b/src/packages/array_basic.rs @@ -1397,11 +1397,11 @@ pub mod array_functions { /// ```rhai /// let x = [1, 2, 3, 4, 5]; /// - /// let y = x.reduce(|r, v| v + if r == () { 0 } else { r }); + /// let y = x.reduce(|r, v| v + (r ?? 0)); /// /// print(y); // prints 15 /// - /// let y = x.reduce(|r, v, i| v + i + if r == () { 0 } else { r }); + /// let y = x.reduce(|r, v, i| v + i + (r ?? 0)); /// /// print(y); // prints 25 /// ``` @@ -1423,10 +1423,10 @@ pub mod array_functions { /// /// ```rhai /// fn process(r, x) { - /// x + if r == () { 0 } else { r } + /// x + (r ?? 0) /// } /// fn process_extra(r, x, i) { - /// x + i + if r == () { 0 } else { r } + /// x + i + (r ?? 0) /// } /// /// let x = [1, 2, 3, 4, 5]; @@ -1556,11 +1556,11 @@ pub mod array_functions { /// ```rhai /// let x = [1, 2, 3, 4, 5]; /// - /// let y = x.reduce_rev(|r, v| v + if r == () { 0 } else { r }); + /// let y = x.reduce_rev(|r, v| v + (r ?? 0)); /// /// print(y); // prints 15 /// - /// let y = x.reduce_rev(|r, v, i| v + i + if r == () { 0 } else { r }); + /// let y = x.reduce_rev(|r, v, i| v + i + (r ?? 0)); /// /// print(y); // prints 25 /// ``` @@ -1583,10 +1583,10 @@ pub mod array_functions { /// /// ```rhai /// fn process(r, x) { - /// x + if r == () { 0 } else { r } + /// x + (r ?? 0) /// } /// fn process_extra(r, x, i) { - /// x + i + if r == () { 0 } else { r } + /// x + i + (r ?? 0) /// } /// /// let x = [1, 2, 3, 4, 5]; diff --git a/src/serde/metadata.rs b/src/serde/metadata.rs index 652f5122..8cd30eaa 100644 --- a/src/serde/metadata.rs +++ b/src/serde/metadata.rs @@ -7,7 +7,7 @@ use crate::{calc_fn_hash, Engine, AST}; use serde::{Deserialize, Serialize}; #[cfg(feature = "no_std")] use std::prelude::v1::*; -use std::{borrow::Cow, cmp::Ordering, collections::BTreeMap, iter::empty}; +use std::{borrow::Cow, cmp::Ordering, collections::BTreeMap}; #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -106,7 +106,7 @@ impl<'a> From<&'a FuncInfo> for FnMetadata<'a> { } else { ( FnType::Native, - calc_native_fn_hash(empty::<&str>(), &info.metadata.name, &info.param_types), + calc_native_fn_hash(None, &info.metadata.name, &info.param_types), ) }; diff --git a/tests/call_fn.rs b/tests/call_fn.rs index 0efeb14c..fa333a62 100644 --- a/tests/call_fn.rs +++ b/tests/call_fn.rs @@ -1,6 +1,6 @@ #![cfg(not(feature = "no_function"))] use rhai::{Dynamic, Engine, EvalAltResult, FnPtr, Func, FuncArgs, Scope, AST, INT}; -use std::{any::TypeId, iter::once}; +use std::any::TypeId; #[test] fn test_call_fn() -> Result<(), Box> { @@ -107,9 +107,9 @@ struct Options { impl FuncArgs for Options { fn parse>(self, container: &mut C) { - container.extend(once(self.foo.into())); - container.extend(once(self.bar.into())); - container.extend(once(self.baz.into())); + container.extend(Some(self.foo.into())); + container.extend(Some(self.bar.into())); + container.extend(Some(self.baz.into())); } }