Skip to content

Commit

Permalink
fix: override size_hint for BitIterator to return the exact remai…
Browse files Browse the repository at this point in the history
…ning size (#6495)
  • Loading branch information
Beihao-Zhou authored Oct 2, 2024
1 parent c54f942 commit 581c647
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions arrow-buffer/src/util/bit_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ impl<'a> Iterator for BitIterator<'a> {
self.current_offset += 1;
Some(v)
}

fn size_hint(&self) -> (usize, Option<usize>) {
let remaining_bits = self.end_offset - self.current_offset;
(remaining_bits, Some(remaining_bits))
}
}

impl<'a> ExactSizeIterator for BitIterator<'a> {}
Expand Down Expand Up @@ -263,6 +268,30 @@ pub fn try_for_each_valid_idx<E, F: FnMut(usize) -> Result<(), E>>(
mod tests {
use super::*;

#[test]
fn test_bit_iterator_size_hint() {
let mut b = BitIterator::new(&[0b00000011], 0, 2);
assert_eq!(
b.size_hint(),
(2, Some(2)),
"Expected size_hint to be (2, Some(2))"
);

b.next();
assert_eq!(
b.size_hint(),
(1, Some(1)),
"Expected size_hint to be (1, Some(1)) after one bit consumed"
);

b.next();
assert_eq!(
b.size_hint(),
(0, Some(0)),
"Expected size_hint to be (0, Some(0)) after all bits consumed"
);
}

#[test]
fn test_bit_iterator() {
let mask = &[0b00010010, 0b00100011, 0b00000101, 0b00010001, 0b10010011];
Expand Down

0 comments on commit 581c647

Please sign in to comment.