Skip to content

Commit 091187e

Browse files
committed
Merge #174: Add CheckedHrpstring::fe32_iter function
319d9e3 Remove generics from AsciiToFe32Iter (Tobin C. Harding) 931b80f Add CheckedHrpstring::fe32_iter function (Tobin C. Harding) Pull request description: Simplify the `AsciiToFe32Iter`, make it public, and add a function to create it. ACKs for top commit: apoelstra: ACK 319d9e3 Tree-SHA512: 5ea007de59dafb59f2b209e4660367306f279c1ceac3dad52744a7298f99d6bc294dabae6c6fa7899f9223c4249e74dd68f54a03131f929812cf2039a820e34c
2 parents e6660be + 319d9e3 commit 091187e

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/primitives/decode.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ impl<'s> CheckedHrpstring<'s> {
373373
#[inline]
374374
pub fn data_part_ascii_no_checksum(&self) -> &'s [u8] { self.ascii }
375375

376+
/// Returns an iterator that yields the data part of the parsed bech32 encoded string as [`Fe32`]s.
377+
///
378+
/// Converts the ASCII bytes representing field elements to the respective field elements.
379+
#[inline]
380+
pub fn fe32_iter<I: Iterator<Item = u8>>(&self) -> AsciiToFe32Iter {
381+
AsciiToFe32Iter { iter: self.ascii.iter().copied() }
382+
}
383+
376384
/// Returns an iterator that yields the data part of the parsed bech32 encoded string.
377385
///
378386
/// Converts the ASCII bytes representing field elements to the respective field elements, then
@@ -631,7 +639,7 @@ fn check_characters(s: &str) -> Result<usize, CharError> {
631639

632640
/// An iterator over a parsed HRP string data as bytes.
633641
pub struct ByteIter<'s> {
634-
iter: FesToBytes<AsciiToFe32Iter<iter::Copied<slice::Iter<'s, u8>>>>,
642+
iter: FesToBytes<AsciiToFe32Iter<'s>>,
635643
}
636644

637645
impl<'s> Iterator for ByteIter<'s> {
@@ -649,7 +657,7 @@ impl<'s> ExactSizeIterator for ByteIter<'s> {
649657

650658
/// An iterator over a parsed HRP string data as field elements.
651659
pub struct Fe32Iter<'s> {
652-
iter: AsciiToFe32Iter<iter::Copied<slice::Iter<'s, u8>>>,
660+
iter: AsciiToFe32Iter<'s>,
653661
}
654662

655663
impl<'s> Iterator for Fe32Iter<'s> {
@@ -660,21 +668,18 @@ impl<'s> Iterator for Fe32Iter<'s> {
660668
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
661669
}
662670

663-
/// Helper iterator adaptor that maps an iterator of valid bech32 character ASCII bytes to an
671+
/// Iterator adaptor that maps an iterator of valid bech32 character ASCII bytes to an
664672
/// iterator of field elements.
665673
///
666674
/// # Panics
667675
///
668676
/// If any `u8` in the input iterator is out of range for an [`Fe32`]. Should only be used on data
669677
/// that has already been checked for validity (eg, by using `check_characters`).
670-
struct AsciiToFe32Iter<I: Iterator<Item = u8>> {
671-
iter: I,
678+
pub struct AsciiToFe32Iter<'s> {
679+
iter: iter::Copied<slice::Iter<'s, u8>>,
672680
}
673681

674-
impl<I> Iterator for AsciiToFe32Iter<I>
675-
where
676-
I: Iterator<Item = u8>,
677-
{
682+
impl<'s> Iterator for AsciiToFe32Iter<'s> {
678683
type Item = Fe32;
679684
#[inline]
680685
fn next(&mut self) -> Option<Fe32> { self.iter.next().map(Fe32::from_char_unchecked) }
@@ -685,10 +690,7 @@ where
685690
}
686691
}
687692

688-
impl<I> ExactSizeIterator for AsciiToFe32Iter<I>
689-
where
690-
I: Iterator<Item = u8> + ExactSizeIterator,
691-
{
693+
impl<'s> ExactSizeIterator for AsciiToFe32Iter<'s> {
692694
#[inline]
693695
fn len(&self) -> usize { self.iter.len() }
694696
}

0 commit comments

Comments
 (0)