From 563f18a04b44f707423ad4397272f30d7ba047e9 Mon Sep 17 00:00:00 2001 From: quake Date: Tue, 6 Sep 2022 14:16:15 +0900 Subject: [PATCH] pref: use `ExactSizeIterator` --- src/func/hashing.rs | 32 ++++++++++++++------------------ src/module/mod.rs | 2 +- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/func/hashing.rs b/src/func/hashing.rs index 75331f66..1a79a684 100644 --- a/src/func/hashing.rs +++ b/src/func/hashing.rs @@ -96,18 +96,15 @@ pub fn get_hasher() -> ahash::AHasher { #[inline] #[must_use] pub fn calc_qualified_var_hash<'a>( - modules: impl IntoIterator, + 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)); + let iter = modules.into_iter(); + let len = iter.len(); + iter.skip(1).for_each(|m| m.hash(s)); len.hash(s); var_name.hash(s); @@ -133,19 +130,16 @@ pub fn calc_qualified_var_hash<'a>( #[inline] #[must_use] pub fn calc_qualified_fn_hash<'a>( - modules: impl IntoIterator, + modules: impl IntoIterator>, fn_name: &str, num: usize, ) -> 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)); + let iter = modules.into_iter(); + let len = iter.len(); + iter.skip(1).for_each(|m| m.hash(s)); len.hash(s); fn_name.hash(s); num.hash(s); @@ -179,11 +173,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 IntoIterator) -> u64 { +pub fn calc_fn_params_hash( + params: impl IntoIterator>, +) -> u64 { let s = &mut get_hasher(); - let mut len = 0; - params.into_iter().for_each(|t| { - len += 1; + let iter = params.into_iter(); + let len = iter.len(); + iter.for_each(|t| { t.hash(s); }); len.hash(s); diff --git a/src/module/mod.rs b/src/module/mod.rs index 51e1d0ea..0aa77a94 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -239,7 +239,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 IntoIterator, + modules: impl IntoIterator>, fn_name: &str, params: &[TypeId], ) -> u64 {