From b11ac18c307439e8994afdd75d286d8eca5f7fcc Mon Sep 17 00:00:00 2001 From: hsqStephenZhang Date: Mon, 6 Apr 2026 11:19:11 +0200 Subject: [PATCH] perf: implement size_hint for iterators --- src/pinyin.rs | 5 +++++ src/pinyin_multi.rs | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/pinyin.rs b/src/pinyin.rs index b745722..a495f5b 100644 --- a/src/pinyin.rs +++ b/src/pinyin.rs @@ -144,6 +144,11 @@ impl<'a> Iterator for PinyinStrIter<'a> { fn next(&mut self) -> Option { self.0.next().map(|c| c.to_pinyin()) } + + #[inline] + fn size_hint(&self) -> (usize, Option) { + self.0.size_hint() + } } #[cfg(test)] diff --git a/src/pinyin_multi.rs b/src/pinyin_multi.rs index cac5f7d..ca7c714 100644 --- a/src/pinyin_multi.rs +++ b/src/pinyin_multi.rs @@ -60,6 +60,11 @@ impl Iterator for PinyinMultiIter { self.index += 1; }) } + + fn size_hint(&self) -> (usize, Option) { + let remaining = self.inner.count() - self.index; + (remaining, Some(remaining)) + } } /// 用于获取多音字信息的 trait @@ -134,6 +139,11 @@ impl<'a> Iterator for PinyinMultiStrIter<'a> { fn next(&mut self) -> Option { self.0.next().map(|c| c.to_pinyin_multi()) } + + #[inline] + fn size_hint(&self) -> (usize, Option) { + self.0.size_hint() + } } #[cfg(test)]