From 88cdc71db6fa0839ea12b135b4acbb0d0d70067e Mon Sep 17 00:00:00 2001 From: GrayJack Date: Mon, 25 Nov 2024 19:47:05 -0300 Subject: [PATCH] feat: Specialize `ExactSizeIterator::len` when possible --- Cargo.toml | 1 + src/lib.rs | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d56223b..6e13a17 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ license = "MIT" keywords = ["no_std", "bitset", "set"] categories = ["data-structures", "no-std"] edition = "2021" +exclude = ["/.fleet", "/.github", "/.idea", "/.vscode"] [package.metadata.docs.rs] all-features = true diff --git a/src/lib.rs b/src/lib.rs index a3a4ec9..41205f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1069,7 +1069,7 @@ impl Iterator for Drain<'_, T, N> { } fn size_hint(&self) -> (usize, Option) { - let len = self.inner.count_ones() as usize; + let len = self.inner.len(); (len, Some(len)) } } @@ -1131,7 +1131,7 @@ impl Iterator for IntoIter { } fn size_hint(&self) -> (usize, Option) { - let len = self.0.count_ones() as usize; + let len = self.0.len(); (len, Some(len)) } } @@ -1155,7 +1155,11 @@ impl DoubleEndedIterator for IntoIter { } impl FusedIterator for IntoIter {} -impl ExactSizeIterator for IntoIter {} +impl ExactSizeIterator for IntoIter { + fn len(&self) -> usize { + self.0.len() + } +} /// An iterator over the items of a `BitSet`. /// @@ -1236,7 +1240,11 @@ impl DoubleEndedIterator for Iter<'_, T, N> { } impl FusedIterator for Iter<'_, T, N> {} -impl ExactSizeIterator for Iter<'_, T, N> {} +impl ExactSizeIterator for Iter<'_, T, N> { + fn len(&self) -> usize { + self.borrow.len() - self.passed_count + } +} /// A lazy iterator producing elements in the difference of `BitSet`s. ///