Skip to content

Commit

Permalink
feat: Specialize ExactSizeIterator::len when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayJack committed Nov 25, 2024
1 parent 3e0e8c1 commit 88cdc71
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ impl<T: PrimInt, const N: usize> Iterator for Drain<'_, T, N> {
}

fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.inner.count_ones() as usize;
let len = self.inner.len();
(len, Some(len))
}
}
Expand Down Expand Up @@ -1131,7 +1131,7 @@ impl<T: PrimInt, const N: usize> Iterator for IntoIter<T, N> {
}

fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.0.count_ones() as usize;
let len = self.0.len();
(len, Some(len))
}
}
Expand All @@ -1155,7 +1155,11 @@ impl<T: PrimInt, const N: usize> DoubleEndedIterator for IntoIter<T, N> {
}

impl<T: PrimInt, const N: usize> FusedIterator for IntoIter<T, N> {}
impl<T: PrimInt, const N: usize> ExactSizeIterator for IntoIter<T, N> {}
impl<T: PrimInt, const N: usize> ExactSizeIterator for IntoIter<T, N> {
fn len(&self) -> usize {
self.0.len()
}
}

/// An iterator over the items of a `BitSet`.
///
Expand Down Expand Up @@ -1236,7 +1240,11 @@ impl<T: PrimInt, const N: usize> DoubleEndedIterator for Iter<'_, T, N> {
}

impl<T: PrimInt, const N: usize> FusedIterator for Iter<'_, T, N> {}
impl<T: PrimInt, const N: usize> ExactSizeIterator for Iter<'_, T, N> {}
impl<T: PrimInt, const N: usize> 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.
///
Expand Down

0 comments on commit 88cdc71

Please sign in to comment.