Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit db17b1c

Browse files
committed
Merge #135: Implements Read on HexIterator
60f51d9 Implements Read on HexIterator (Riccardo Casatta) Pull request description: This would allow deserializing objects without allocating the intermediate byte vector like: ``` let tx = Transaction::consensus_decode(HexIterator::new("....").unwrap()).unwrap() ``` Especially useful for libraries like rust_bitcoincore_rpc in places like this https://github.com/rust-bitcoin/rust-bitcoincore-rpc/blob/54a427f2e45b0b8712d44a6995f6a03ad5f961f8/client/src/client.rs#L322-L326 CI is failing on my repo I think for an unrelated reason I am not fully grasping at the moment, @JeremyRubin could you have a look? ACKs for top commit: apoelstra: ACK 60f51d9 Tree-SHA512: 66a25cf3123c1a5d102fc80bdc9147fd2b2ebd0c9936380e00df542f9d9044af7f1522cbcb6eb5ba0241cb93c711e171f485faf236c70580cf01321433e5a9ed
2 parents bfee9eb + 60f51d9 commit db17b1c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/hex.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ use alloc::{string::String, vec::Vec};
2020
#[cfg(feature = "alloc")]
2121
use alloc::format;
2222

23+
#[cfg(feature = "std")]
24+
use std::io;
25+
#[cfg(all(not(feature = "std"), feature = "core2"))]
26+
use core2::io;
27+
2328
use core::{fmt, str};
2429
use Hash;
2530

@@ -135,6 +140,23 @@ impl<'a> Iterator for HexIterator<'a> {
135140
}
136141
}
137142

143+
#[cfg(any(feature = "std", feature = "core2"))]
144+
impl<'a> io::Read for HexIterator<'a> {
145+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
146+
let mut bytes_read = 0usize;
147+
for dst in buf {
148+
match self.next() {
149+
Some(Ok(src)) => {
150+
*dst = src;
151+
bytes_read += 1;
152+
},
153+
_ => break,
154+
}
155+
}
156+
Ok(bytes_read)
157+
}
158+
}
159+
138160
impl<'a> DoubleEndedIterator for HexIterator<'a> {
139161
fn next_back(&mut self) -> Option<Result<u8, Error>> {
140162
let lo = self.iter.next_back()?;

0 commit comments

Comments
 (0)