Skip to content

Commit 719ec15

Browse files
committed
Merge rust-bitcoin#5256: Add an iterator that yields the bytes of an encoded object
b7cb088 Add iterator tests for EncodableByteIter (Mitchell Bagot) 4fa296a Add an iterator to yield the bytes of an encoded object (Mitchell Bagot) Pull request description: The Encoder trait currently provides an iterator-style interface for objects to yield byte slices. Since other utilities, like hex::BytesToHexIter rely on byte-by-byte input to function, providing a method of yielding raw bytes for Encodables increases the versatility of the encoding on Encodable objects. Add EncodableByteIter struct to iterate the bytes from an Encodable as u8 values, and tests to demonstrate function alongside hex::BytesToHexIter Closes rust-bitcoin#5190 ACKs for top commit: apoelstra: ACK b7cb088; successfully ran local tests tcharding: ACK b7cb088 Tree-SHA512: f6cc2428ad0a452f17004b066799e33d401d83b4961ba85c806f84dc60238aa7d20f0c08c57571ca2ec3695a86e06d37e60e2b54dbba965218e1c9e396c8374c
2 parents 74e2ad3 + b7cb088 commit 719ec15

File tree

10 files changed

+146
-1
lines changed

10 files changed

+146
-1
lines changed

Cargo-minimal.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ name = "bitcoin-consensus-encoding"
7777
version = "1.0.0-rc.2"
7878
dependencies = [
7979
"bitcoin-internals",
80+
"hex-conservative 0.3.0",
8081
]
8182

8283
[[package]]

Cargo-recent.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ name = "bitcoin-consensus-encoding"
7676
version = "1.0.0-rc.2"
7777
dependencies = [
7878
"bitcoin-internals",
79+
"hex-conservative 0.3.0",
7980
]
8081

8182
[[package]]

api/consensus_encoding/all-features.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ impl<'e, T> core::marker::Sync for bitcoin_consensus_encoding::SliceEncoder<'e,
8787
impl<'e, T> core::marker::Unpin for bitcoin_consensus_encoding::SliceEncoder<'e, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Unpin
8888
impl<'e, T> core::panic::unwind_safe::RefUnwindSafe for bitcoin_consensus_encoding::SliceEncoder<'e, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::panic::unwind_safe::RefUnwindSafe, T: core::panic::unwind_safe::RefUnwindSafe
8989
impl<'e, T> core::panic::unwind_safe::UnwindSafe for bitcoin_consensus_encoding::SliceEncoder<'e, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::panic::unwind_safe::UnwindSafe, T: core::panic::unwind_safe::RefUnwindSafe
90+
impl<'s, T: bitcoin_consensus_encoding::Encodable + 's> bitcoin_consensus_encoding::EncodableByteIter<'s, T>
91+
impl<'s, T: bitcoin_consensus_encoding::Encodable + 's> core::iter::traits::iterator::Iterator for bitcoin_consensus_encoding::EncodableByteIter<'s, T>
92+
impl<'s, T> core::marker::Freeze for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Freeze
93+
impl<'s, T> core::marker::Send for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Send
94+
impl<'s, T> core::marker::Sync for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Sync
95+
impl<'s, T> core::marker::Unpin for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Unpin
96+
impl<'s, T> core::panic::unwind_safe::RefUnwindSafe for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::panic::unwind_safe::RefUnwindSafe
97+
impl<'s, T> core::panic::unwind_safe::UnwindSafe for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::panic::unwind_safe::UnwindSafe
9098
impl<'sl> bitcoin_consensus_encoding::BytesEncoder<'sl>
9199
impl<'sl> core::marker::Freeze for bitcoin_consensus_encoding::BytesEncoder<'sl>
92100
impl<'sl> core::marker::Send for bitcoin_consensus_encoding::BytesEncoder<'sl>
@@ -360,6 +368,8 @@ pub fn bitcoin_consensus_encoding::Decoder::end(self) -> core::result::Result<Se
360368
pub fn bitcoin_consensus_encoding::Decoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
361369
pub fn bitcoin_consensus_encoding::Decoder::read_limit(&self) -> usize
362370
pub fn bitcoin_consensus_encoding::Encodable::encoder(&self) -> Self::Encoder
371+
pub fn bitcoin_consensus_encoding::EncodableByteIter<'s, T>::new(encodable: &'s T) -> Self
372+
pub fn bitcoin_consensus_encoding::EncodableByteIter<'s, T>::next(&mut self) -> core::option::Option<Self::Item>
363373
pub fn bitcoin_consensus_encoding::Encoder2<A, B>::advance(&mut self) -> bool
364374
pub fn bitcoin_consensus_encoding::Encoder2<A, B>::current_chunk(&self) -> &[u8]
365375
pub fn bitcoin_consensus_encoding::Encoder3<A, B, C>::advance(&mut self) -> bool
@@ -414,6 +424,7 @@ pub struct bitcoin_consensus_encoding::Decoder2<A, B> where A: bitcoin_consensus
414424
pub struct bitcoin_consensus_encoding::Decoder3<A, B, C> where A: bitcoin_consensus_encoding::Decoder, B: bitcoin_consensus_encoding::Decoder, C: bitcoin_consensus_encoding::Decoder
415425
pub struct bitcoin_consensus_encoding::Decoder4<A, B, C, D> where A: bitcoin_consensus_encoding::Decoder, B: bitcoin_consensus_encoding::Decoder, C: bitcoin_consensus_encoding::Decoder, D: bitcoin_consensus_encoding::Decoder
416426
pub struct bitcoin_consensus_encoding::Decoder6<A, B, C, D, E, F> where A: bitcoin_consensus_encoding::Decoder, B: bitcoin_consensus_encoding::Decoder, C: bitcoin_consensus_encoding::Decoder, D: bitcoin_consensus_encoding::Decoder, E: bitcoin_consensus_encoding::Decoder, F: bitcoin_consensus_encoding::Decoder
427+
pub struct bitcoin_consensus_encoding::EncodableByteIter<'s, T: bitcoin_consensus_encoding::Encodable + 's>
417428
pub struct bitcoin_consensus_encoding::Encoder2<A, B>
418429
pub struct bitcoin_consensus_encoding::Encoder3<A, B, C>
419430
pub struct bitcoin_consensus_encoding::Encoder4<A, B, C, D>
@@ -445,5 +456,6 @@ pub type bitcoin_consensus_encoding::Decoder6<A, B, C, D, E, F>::Output = (<A as
445456
pub type bitcoin_consensus_encoding::Decoder::Error
446457
pub type bitcoin_consensus_encoding::Decoder::Output
447458
pub type bitcoin_consensus_encoding::Encodable::Encoder<'s> where Self: 's: bitcoin_consensus_encoding::Encoder
459+
pub type bitcoin_consensus_encoding::EncodableByteIter<'s, T>::Item = u8
448460
pub type bitcoin_consensus_encoding::VecDecoder<T>::Error = bitcoin_consensus_encoding::VecDecoderError<<<T as bitcoin_consensus_encoding::Decodable>::Decoder as bitcoin_consensus_encoding::Decoder>::Error>
449461
pub type bitcoin_consensus_encoding::VecDecoder<T>::Output = alloc::vec::Vec<T>

api/consensus_encoding/alloc-only.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ impl<'e, T> core::marker::Sync for bitcoin_consensus_encoding::SliceEncoder<'e,
8383
impl<'e, T> core::marker::Unpin for bitcoin_consensus_encoding::SliceEncoder<'e, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Unpin
8484
impl<'e, T> core::panic::unwind_safe::RefUnwindSafe for bitcoin_consensus_encoding::SliceEncoder<'e, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::panic::unwind_safe::RefUnwindSafe, T: core::panic::unwind_safe::RefUnwindSafe
8585
impl<'e, T> core::panic::unwind_safe::UnwindSafe for bitcoin_consensus_encoding::SliceEncoder<'e, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::panic::unwind_safe::UnwindSafe, T: core::panic::unwind_safe::RefUnwindSafe
86+
impl<'s, T: bitcoin_consensus_encoding::Encodable + 's> bitcoin_consensus_encoding::EncodableByteIter<'s, T>
87+
impl<'s, T: bitcoin_consensus_encoding::Encodable + 's> core::iter::traits::iterator::Iterator for bitcoin_consensus_encoding::EncodableByteIter<'s, T>
88+
impl<'s, T> core::marker::Freeze for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Freeze
89+
impl<'s, T> core::marker::Send for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Send
90+
impl<'s, T> core::marker::Sync for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Sync
91+
impl<'s, T> core::marker::Unpin for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Unpin
92+
impl<'s, T> core::panic::unwind_safe::RefUnwindSafe for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::panic::unwind_safe::RefUnwindSafe
93+
impl<'s, T> core::panic::unwind_safe::UnwindSafe for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::panic::unwind_safe::UnwindSafe
8694
impl<'sl> bitcoin_consensus_encoding::BytesEncoder<'sl>
8795
impl<'sl> core::marker::Freeze for bitcoin_consensus_encoding::BytesEncoder<'sl>
8896
impl<'sl> core::marker::Send for bitcoin_consensus_encoding::BytesEncoder<'sl>
@@ -333,6 +341,8 @@ pub fn bitcoin_consensus_encoding::Decoder::end(self) -> core::result::Result<Se
333341
pub fn bitcoin_consensus_encoding::Decoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
334342
pub fn bitcoin_consensus_encoding::Decoder::read_limit(&self) -> usize
335343
pub fn bitcoin_consensus_encoding::Encodable::encoder(&self) -> Self::Encoder
344+
pub fn bitcoin_consensus_encoding::EncodableByteIter<'s, T>::new(encodable: &'s T) -> Self
345+
pub fn bitcoin_consensus_encoding::EncodableByteIter<'s, T>::next(&mut self) -> core::option::Option<Self::Item>
336346
pub fn bitcoin_consensus_encoding::Encoder2<A, B>::advance(&mut self) -> bool
337347
pub fn bitcoin_consensus_encoding::Encoder2<A, B>::current_chunk(&self) -> &[u8]
338348
pub fn bitcoin_consensus_encoding::Encoder3<A, B, C>::advance(&mut self) -> bool
@@ -379,6 +389,7 @@ pub struct bitcoin_consensus_encoding::Decoder2<A, B> where A: bitcoin_consensus
379389
pub struct bitcoin_consensus_encoding::Decoder3<A, B, C> where A: bitcoin_consensus_encoding::Decoder, B: bitcoin_consensus_encoding::Decoder, C: bitcoin_consensus_encoding::Decoder
380390
pub struct bitcoin_consensus_encoding::Decoder4<A, B, C, D> where A: bitcoin_consensus_encoding::Decoder, B: bitcoin_consensus_encoding::Decoder, C: bitcoin_consensus_encoding::Decoder, D: bitcoin_consensus_encoding::Decoder
381391
pub struct bitcoin_consensus_encoding::Decoder6<A, B, C, D, E, F> where A: bitcoin_consensus_encoding::Decoder, B: bitcoin_consensus_encoding::Decoder, C: bitcoin_consensus_encoding::Decoder, D: bitcoin_consensus_encoding::Decoder, E: bitcoin_consensus_encoding::Decoder, F: bitcoin_consensus_encoding::Decoder
392+
pub struct bitcoin_consensus_encoding::EncodableByteIter<'s, T: bitcoin_consensus_encoding::Encodable + 's>
382393
pub struct bitcoin_consensus_encoding::Encoder2<A, B>
383394
pub struct bitcoin_consensus_encoding::Encoder3<A, B, C>
384395
pub struct bitcoin_consensus_encoding::Encoder4<A, B, C, D>
@@ -410,5 +421,6 @@ pub type bitcoin_consensus_encoding::Decoder6<A, B, C, D, E, F>::Output = (<A as
410421
pub type bitcoin_consensus_encoding::Decoder::Error
411422
pub type bitcoin_consensus_encoding::Decoder::Output
412423
pub type bitcoin_consensus_encoding::Encodable::Encoder<'s> where Self: 's: bitcoin_consensus_encoding::Encoder
424+
pub type bitcoin_consensus_encoding::EncodableByteIter<'s, T>::Item = u8
413425
pub type bitcoin_consensus_encoding::VecDecoder<T>::Error = bitcoin_consensus_encoding::VecDecoderError<<<T as bitcoin_consensus_encoding::Decodable>::Decoder as bitcoin_consensus_encoding::Decoder>::Error>
414426
pub type bitcoin_consensus_encoding::VecDecoder<T>::Output = alloc::vec::Vec<T>

api/consensus_encoding/no-features.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ impl<'e, T> core::marker::Sync for bitcoin_consensus_encoding::SliceEncoder<'e,
4949
impl<'e, T> core::marker::Unpin for bitcoin_consensus_encoding::SliceEncoder<'e, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Unpin
5050
impl<'e, T> core::panic::unwind_safe::RefUnwindSafe for bitcoin_consensus_encoding::SliceEncoder<'e, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::panic::unwind_safe::RefUnwindSafe, T: core::panic::unwind_safe::RefUnwindSafe
5151
impl<'e, T> core::panic::unwind_safe::UnwindSafe for bitcoin_consensus_encoding::SliceEncoder<'e, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::panic::unwind_safe::UnwindSafe, T: core::panic::unwind_safe::RefUnwindSafe
52+
impl<'s, T: bitcoin_consensus_encoding::Encodable + 's> bitcoin_consensus_encoding::EncodableByteIter<'s, T>
53+
impl<'s, T: bitcoin_consensus_encoding::Encodable + 's> core::iter::traits::iterator::Iterator for bitcoin_consensus_encoding::EncodableByteIter<'s, T>
54+
impl<'s, T> core::marker::Freeze for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Freeze
55+
impl<'s, T> core::marker::Send for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Send
56+
impl<'s, T> core::marker::Sync for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Sync
57+
impl<'s, T> core::marker::Unpin for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::marker::Unpin
58+
impl<'s, T> core::panic::unwind_safe::RefUnwindSafe for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::panic::unwind_safe::RefUnwindSafe
59+
impl<'s, T> core::panic::unwind_safe::UnwindSafe for bitcoin_consensus_encoding::EncodableByteIter<'s, T> where <T as bitcoin_consensus_encoding::Encodable>::Encoder: core::panic::unwind_safe::UnwindSafe
5260
impl<'sl> bitcoin_consensus_encoding::BytesEncoder<'sl>
5361
impl<'sl> core::marker::Freeze for bitcoin_consensus_encoding::BytesEncoder<'sl>
5462
impl<'sl> core::marker::Send for bitcoin_consensus_encoding::BytesEncoder<'sl>
@@ -267,6 +275,8 @@ pub fn bitcoin_consensus_encoding::Decoder::end(self) -> core::result::Result<Se
267275
pub fn bitcoin_consensus_encoding::Decoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
268276
pub fn bitcoin_consensus_encoding::Decoder::read_limit(&self) -> usize
269277
pub fn bitcoin_consensus_encoding::Encodable::encoder(&self) -> Self::Encoder
278+
pub fn bitcoin_consensus_encoding::EncodableByteIter<'s, T>::new(encodable: &'s T) -> Self
279+
pub fn bitcoin_consensus_encoding::EncodableByteIter<'s, T>::next(&mut self) -> core::option::Option<Self::Item>
270280
pub fn bitcoin_consensus_encoding::Encoder2<A, B>::advance(&mut self) -> bool
271281
pub fn bitcoin_consensus_encoding::Encoder2<A, B>::current_chunk(&self) -> &[u8]
272282
pub fn bitcoin_consensus_encoding::Encoder3<A, B, C>::advance(&mut self) -> bool
@@ -298,6 +308,7 @@ pub struct bitcoin_consensus_encoding::Decoder2<A, B> where A: bitcoin_consensus
298308
pub struct bitcoin_consensus_encoding::Decoder3<A, B, C> where A: bitcoin_consensus_encoding::Decoder, B: bitcoin_consensus_encoding::Decoder, C: bitcoin_consensus_encoding::Decoder
299309
pub struct bitcoin_consensus_encoding::Decoder4<A, B, C, D> where A: bitcoin_consensus_encoding::Decoder, B: bitcoin_consensus_encoding::Decoder, C: bitcoin_consensus_encoding::Decoder, D: bitcoin_consensus_encoding::Decoder
300310
pub struct bitcoin_consensus_encoding::Decoder6<A, B, C, D, E, F> where A: bitcoin_consensus_encoding::Decoder, B: bitcoin_consensus_encoding::Decoder, C: bitcoin_consensus_encoding::Decoder, D: bitcoin_consensus_encoding::Decoder, E: bitcoin_consensus_encoding::Decoder, F: bitcoin_consensus_encoding::Decoder
311+
pub struct bitcoin_consensus_encoding::EncodableByteIter<'s, T: bitcoin_consensus_encoding::Encodable + 's>
301312
pub struct bitcoin_consensus_encoding::Encoder2<A, B>
302313
pub struct bitcoin_consensus_encoding::Encoder3<A, B, C>
303314
pub struct bitcoin_consensus_encoding::Encoder4<A, B, C, D>
@@ -324,3 +335,4 @@ pub type bitcoin_consensus_encoding::Decoder6<A, B, C, D, E, F>::Output = (<A as
324335
pub type bitcoin_consensus_encoding::Decoder::Error
325336
pub type bitcoin_consensus_encoding::Decoder::Output
326337
pub type bitcoin_consensus_encoding::Encodable::Encoder<'s> where Self: 's: bitcoin_consensus_encoding::Encoder
338+
pub type bitcoin_consensus_encoding::EncodableByteIter<'s, T>::Item = u8

consensus_encoding/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ alloc = ["internals/alloc"]
2020
[dependencies]
2121
internals = { package = "bitcoin-internals", path = "../internals", version = "0.4.0" }
2222

23+
[dev-dependencies]
24+
hex = { package = "hex-conservative", version = "0.3.0" }
25+
2326
[package.metadata.docs.rs]
2427
all-features = true
2528
rustdoc-args = ["--cfg", "docsrs"]

consensus_encoding/src/encode/encoders.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,20 @@ mod tests {
592592
assert!(!e.advance());
593593
assert!(e.current_chunk().is_empty());
594594
}
595+
596+
#[test]
597+
fn iter_encoder() {
598+
let test_array = TestArray([1u8, 2, 3, 4]);
599+
let mut iter = crate::EncodableByteIter::new(&test_array);
600+
let mut byte = iter.next().unwrap();
601+
assert_eq!(byte, 1u8);
602+
byte = iter.next().unwrap();
603+
assert_eq!(byte, 2u8);
604+
byte = iter.next().unwrap();
605+
assert_eq!(byte, 3u8);
606+
byte = iter.next().unwrap();
607+
assert_eq!(byte, 4u8);
608+
let none = iter.next();
609+
assert_eq!(none, None);
610+
}
595611
}

consensus_encoding/src/encode/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,35 @@ macro_rules! encoder_newtype{
6464
}
6565
}
6666

67+
/// Yields bytes from any [`Encodable`] instance.
68+
pub struct EncodableByteIter<'s, T: Encodable + 's> {
69+
enc: T::Encoder<'s>,
70+
position: usize,
71+
}
72+
73+
impl<'s, T: Encodable + 's> EncodableByteIter<'s, T> {
74+
/// Constructs a new byte iterator around a provided encodable.
75+
pub fn new(encodable: &'s T) -> Self {
76+
Self { enc: encodable.encoder(), position: 0 }
77+
}
78+
}
79+
80+
impl<'s, T: Encodable + 's> Iterator for EncodableByteIter<'s, T> {
81+
type Item = u8;
82+
83+
fn next(&mut self) -> Option<Self::Item> {
84+
loop {
85+
if let Some(b) = self.enc.current_chunk().get(self.position) {
86+
self.position += 1;
87+
return Some(*b);
88+
} else if !self.enc.advance() {
89+
return None;
90+
}
91+
self.position = 0;
92+
}
93+
}
94+
}
95+
6796
/// Encodes an object into a vector.
6897
#[cfg(feature = "alloc")]
6998
pub fn encode_to_vec<T>(object: &T) -> Vec<u8>

consensus_encoding/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ pub use self::encode::encoders::{
4242
ArrayEncoder, BytesEncoder, CompactSizeEncoder, Encoder2, Encoder3, Encoder4, Encoder6,
4343
SliceEncoder,
4444
};
45-
pub use self::encode::{Encodable, Encoder};
45+
pub use self::encode::{Encodable, Encoder, EncodableByteIter};

consensus_encoding/tests/iter.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use hex::BytesToHexIter;
2+
3+
use bitcoin_consensus_encoding::{Encodable, ArrayEncoder, Encoder2, EncodableByteIter};
4+
5+
struct TestArray<const N: usize>([u8; N]);
6+
7+
impl<const N: usize> Encodable for TestArray<N> {
8+
type Encoder<'s>
9+
= ArrayEncoder<N>
10+
where
11+
Self: 's;
12+
13+
fn encoder(&self) -> Self::Encoder<'_> { ArrayEncoder::without_length_prefix(self.0) }
14+
}
15+
16+
struct TestCatArray<const N: usize, const M: usize>([u8; N], [u8; M]);
17+
18+
impl<const N: usize, const M: usize> Encodable for TestCatArray<N, M> {
19+
type Encoder<'s>
20+
= Encoder2<ArrayEncoder<N>, ArrayEncoder<M>>
21+
where
22+
Self: 's;
23+
24+
fn encoder(&self) -> Self::Encoder<'_> { Encoder2::new(
25+
ArrayEncoder::without_length_prefix(self.0),
26+
ArrayEncoder::without_length_prefix(self.1),
27+
)
28+
}
29+
}
30+
31+
#[test]
32+
fn hex_iter() {
33+
let data = TestArray([255u8, 240, 9, 135]);
34+
let byte_iter = EncodableByteIter::new(&data);
35+
let mut iter = BytesToHexIter::new(byte_iter, hex::Case::Upper);
36+
37+
let expect_str = "FFF00987";
38+
for byte in expect_str.chars() {
39+
let iter_byte = iter.next().unwrap();
40+
assert_eq!(iter_byte, byte);
41+
}
42+
let none = iter.next();
43+
assert_eq!(none, None);
44+
}
45+
46+
#[test]
47+
fn hex_iter_cat_encoder() {
48+
let data = TestCatArray([222u8, 173], [190u8, 239]);
49+
let byte_iter = EncodableByteIter::new(&data);
50+
let mut iter = BytesToHexIter::new(byte_iter, hex::Case::Lower);
51+
52+
let expect_str = "deadbeef";
53+
for byte in expect_str.chars() {
54+
let iter_byte = iter.next().unwrap();
55+
assert_eq!(iter_byte, byte);
56+
}
57+
let none = iter.next();
58+
assert_eq!(none, None);
59+
}

0 commit comments

Comments
 (0)