From 931b80f96f0314c43ac3b06085b30b39eebd3ba9 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 23 Jan 2024 16:00:04 +1100 Subject: [PATCH 1/2] Add CheckedHrpstring::fe32_iter function Add a function that returns an iterator that yields field elements from the data part of the parsed input bech32 string. --- src/primitives/decode.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/primitives/decode.rs b/src/primitives/decode.rs index 89a7edb93..f7a8be260 100644 --- a/src/primitives/decode.rs +++ b/src/primitives/decode.rs @@ -373,6 +373,16 @@ impl<'s> CheckedHrpstring<'s> { #[inline] pub fn data_part_ascii_no_checksum(&self) -> &[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>( + &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 @@ -658,14 +668,14 @@ impl<'s> Iterator for Fe32Iter<'s> { fn size_hint(&self) -> (usize, Option) { 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> { +pub struct AsciiToFe32Iter> { iter: I, } From 319d9e335b1dcb4f783a455b3f532e917ca5526c Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 23 Jan 2024 16:27:38 +1100 Subject: [PATCH 2/2] Remove generics from AsciiToFe32Iter The `AsciiToFe32Iter` always iterates over the input string, we can just use the lifetime from the string and a `iter::Copied` iterator. --- src/primitives/decode.rs | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/primitives/decode.rs b/src/primitives/decode.rs index f7a8be260..04e3470a9 100644 --- a/src/primitives/decode.rs +++ b/src/primitives/decode.rs @@ -377,9 +377,7 @@ impl<'s> CheckedHrpstring<'s> { /// /// Converts the ASCII bytes representing field elements to the respective field elements. #[inline] - pub fn fe32_iter>( - &self, - ) -> AsciiToFe32Iter>> { + pub fn fe32_iter>(&self) -> AsciiToFe32Iter { AsciiToFe32Iter { iter: self.ascii.iter().copied() } } @@ -639,7 +637,7 @@ fn check_characters(s: &str) -> Result { /// An iterator over a parsed HRP string data as bytes. pub struct ByteIter<'s> { - iter: FesToBytes>>>, + iter: FesToBytes>, } impl<'s> Iterator for ByteIter<'s> { @@ -657,7 +655,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: AsciiToFe32Iter<'s>, } impl<'s> Iterator for Fe32Iter<'s> { @@ -675,14 +673,11 @@ impl<'s> Iterator for Fe32Iter<'s> { /// /// 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`). -pub struct AsciiToFe32Iter> { - iter: I, +pub struct AsciiToFe32Iter<'s> { + iter: iter::Copied>, } -impl Iterator for AsciiToFe32Iter -where - I: Iterator, -{ +impl<'s> Iterator for AsciiToFe32Iter<'s> { type Item = Fe32; #[inline] fn next(&mut self) -> Option { self.iter.next().map(Fe32::from_char_unchecked) } @@ -693,10 +688,7 @@ where } } -impl ExactSizeIterator for AsciiToFe32Iter -where - I: Iterator + ExactSizeIterator, -{ +impl<'s> ExactSizeIterator for AsciiToFe32Iter<'s> { #[inline] fn len(&self) -> usize { self.iter.len() } }