Skip to content

Commit

Permalink
Merge #174: Add CheckedHrpstring::fe32_iter function
Browse files Browse the repository at this point in the history
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
  • Loading branch information
apoelstra committed Jan 23, 2024
2 parents e6660be + 319d9e3 commit 091187e
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/primitives/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,14 @@ impl<'s> CheckedHrpstring<'s> {
#[inline]
pub fn data_part_ascii_no_checksum(&self) -> &'s [u8] { self.ascii }

/// Returns an iterator that yields the data part of the parsed bech32 encoded string as [`Fe32`]s.
///
/// Converts the ASCII bytes representing field elements to the respective field elements.
#[inline]
pub fn fe32_iter<I: Iterator<Item = u8>>(&self) -> AsciiToFe32Iter {
AsciiToFe32Iter { iter: self.ascii.iter().copied() }
}

/// Returns an iterator that yields the data part of the parsed bech32 encoded string.
///
/// Converts the ASCII bytes representing field elements to the respective field elements, then
Expand Down Expand Up @@ -631,7 +639,7 @@ fn check_characters(s: &str) -> Result<usize, CharError> {

/// An iterator over a parsed HRP string data as bytes.
pub struct ByteIter<'s> {
iter: FesToBytes<AsciiToFe32Iter<iter::Copied<slice::Iter<'s, u8>>>>,
iter: FesToBytes<AsciiToFe32Iter<'s>>,
}

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

/// An iterator over a parsed HRP string data as field elements.
pub struct Fe32Iter<'s> {
iter: AsciiToFe32Iter<iter::Copied<slice::Iter<'s, u8>>>,
iter: AsciiToFe32Iter<'s>,
}

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

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

impl<I> Iterator for AsciiToFe32Iter<I>
where
I: Iterator<Item = u8>,
{
impl<'s> Iterator for AsciiToFe32Iter<'s> {
type Item = Fe32;
#[inline]
fn next(&mut self) -> Option<Fe32> { self.iter.next().map(Fe32::from_char_unchecked) }
Expand All @@ -685,10 +690,7 @@ where
}
}

impl<I> ExactSizeIterator for AsciiToFe32Iter<I>
where
I: Iterator<Item = u8> + ExactSizeIterator,
{
impl<'s> ExactSizeIterator for AsciiToFe32Iter<'s> {
#[inline]
fn len(&self) -> usize { self.iter.len() }
}
Expand Down

0 comments on commit 091187e

Please sign in to comment.