Skip to content

Commit

Permalink
Merge rust-bitcoin#122: Fix HexToBytesIter::size_hint
Browse files Browse the repository at this point in the history
9787062 Fix `HexToBytesIter::size_hint` (Leo Nash)

Pull request description:

  `HexToBytesIter` iterates once for each pair of hex digits.

ACKs for top commit:
  apoelstra:
    ACK 9787062; successfully ran local tests
  tcharding:
    ACK 9787062

Tree-SHA512: a50f86da5f7355727604a5063aacf1eaac04c5cee17ef611b559055d03c272f6ed016d08cd00f724a7601efd78d4cfe10d94b8b6423b4678e7447a6d029c23ab
  • Loading branch information
apoelstra committed Oct 18, 2024
2 parents 0dfa2ff + 9787062 commit 322975c
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ impl<T: Iterator<Item = [u8; 2]> + ExactSizeIterator> Iterator for HexToBytesIte
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (min, max) = self.iter.size_hint();
(min / 2, max.map(|x| x / 2))
}
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }

#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
Expand Down Expand Up @@ -324,6 +321,12 @@ mod tests {
for (i, b) in HexToBytesIter::new(hex).unwrap().enumerate() {
assert_eq!(b.unwrap(), bytes[i]);
}

let mut iter = HexToBytesIter::new(hex).unwrap();
for i in (0..=bytes.len()).rev() {
assert_eq!(iter.len(), i);
let _ = iter.next();
}
}

#[test]
Expand All @@ -334,6 +337,27 @@ mod tests {
for (i, b) in HexToBytesIter::new(hex).unwrap().rev().enumerate() {
assert_eq!(b.unwrap(), bytes[i]);
}

let mut iter = HexToBytesIter::new(hex).unwrap().rev();
for i in (0..=bytes.len()).rev() {
assert_eq!(iter.len(), i);
let _ = iter.next();
}
}

#[test]
fn hex_to_digits_size_hint() {
let hex = "deadbeef";
let iter = HexDigitsIter::new_unchecked(hex.as_bytes());
// HexDigitsIter yields two digits at a time `[u8; 2]`.
assert_eq!(iter.size_hint(), (4, Some(4)));
}

#[test]
fn hex_to_bytes_size_hint() {
let hex = "deadbeef";
let iter = HexToBytesIter::new_unchecked(hex);
assert_eq!(iter.size_hint(), (4, Some(4)));
}

#[test]
Expand Down

0 comments on commit 322975c

Please sign in to comment.