Skip to content

Commit 01f548d

Browse files
perf: implement size_hint for iterators (#75)
1 parent 76fc7d3 commit 01f548d

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

src/pinyin.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ impl<'a> Iterator for PinyinStrIter<'a> {
144144
fn next(&mut self) -> Option<Self::Item> {
145145
self.0.next().map(|c| c.to_pinyin())
146146
}
147+
148+
#[inline]
149+
fn size_hint(&self) -> (usize, Option<usize>) {
150+
self.0.size_hint()
151+
}
147152
}
148153

149154
#[cfg(test)]

src/pinyin_multi.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ impl Iterator for PinyinMultiIter {
6060
self.index += 1;
6161
})
6262
}
63+
64+
fn size_hint(&self) -> (usize, Option<usize>) {
65+
let remaining = self.inner.count() - self.index;
66+
(remaining, Some(remaining))
67+
}
6368
}
6469

6570
/// 用于获取多音字信息的 trait
@@ -134,6 +139,11 @@ impl<'a> Iterator for PinyinMultiStrIter<'a> {
134139
fn next(&mut self) -> Option<Self::Item> {
135140
self.0.next().map(|c| c.to_pinyin_multi())
136141
}
142+
143+
#[inline]
144+
fn size_hint(&self) -> (usize, Option<usize>) {
145+
self.0.size_hint()
146+
}
137147
}
138148

139149
#[cfg(test)]

0 commit comments

Comments
 (0)