Add a `bitcoin_io` crate
In order to support standard (de)serialization of structs, the
`rust-bitcoin` ecosystem uses the standard `std::io::{Read,Write}`
traits. This works great for environments with `std`, however sadly
the `std::io` module has not yet been added to the `core` crate.
Thus, in `no-std`, the `rust-bitcoin` ecosystem has historically
used the `core2` crate to provide copies of the `std::io` module
without any major dependencies. Sadly, its one dependency,
`memchr`, recently broke our MSRV.
Worse, because we didn't want to take on any excess dependencies
for `std` builds, `rust-bitcoin` has had to have
mutually-exclusive `std` and `no-std` builds. This breaks general
assumptions about how features work in Rust, causing substantial
pain for applications far downstream of `rust-bitcoin` crates.
Here, we add a new `bitcoin_io` crate, making it an unconditional
dependency and using its `io` module in the in-repository crates
in place of `std::io` and `core2::io`. As it is not substantial
additional code, the `hashes` io implementations are no longer
feature-gated.
This doesn't actually accomplish anything on its own, only adding
the new crate which still depends on `core2`.
2023-10-04 05:51:26 +00:00
|
|
|
//! Rust-Bitcoin IO Library
|
|
|
|
//!
|
2023-12-06 00:09:18 +00:00
|
|
|
//! The `std::io` module is not exposed in `no-std` Rust so building `no-std` applications which
|
|
|
|
//! require reading and writing objects via standard traits is not generally possible. Thus, this
|
|
|
|
//! library exists to export a minmal version of `std::io`'s traits which we use in `rust-bitcoin`
|
|
|
|
//! so that we can support `no-std` applications.
|
Add a `bitcoin_io` crate
In order to support standard (de)serialization of structs, the
`rust-bitcoin` ecosystem uses the standard `std::io::{Read,Write}`
traits. This works great for environments with `std`, however sadly
the `std::io` module has not yet been added to the `core` crate.
Thus, in `no-std`, the `rust-bitcoin` ecosystem has historically
used the `core2` crate to provide copies of the `std::io` module
without any major dependencies. Sadly, its one dependency,
`memchr`, recently broke our MSRV.
Worse, because we didn't want to take on any excess dependencies
for `std` builds, `rust-bitcoin` has had to have
mutually-exclusive `std` and `no-std` builds. This breaks general
assumptions about how features work in Rust, causing substantial
pain for applications far downstream of `rust-bitcoin` crates.
Here, we add a new `bitcoin_io` crate, making it an unconditional
dependency and using its `io` module in the in-repository crates
in place of `std::io` and `core2::io`. As it is not substantial
additional code, the `hashes` io implementations are no longer
feature-gated.
This doesn't actually accomplish anything on its own, only adding
the new crate which still depends on `core2`.
2023-10-04 05:51:26 +00:00
|
|
|
//!
|
2023-12-06 00:09:18 +00:00
|
|
|
//! These traits are not one-for-one drop-ins, but are as close as possible while still implementing
|
Add a `bitcoin_io` crate
In order to support standard (de)serialization of structs, the
`rust-bitcoin` ecosystem uses the standard `std::io::{Read,Write}`
traits. This works great for environments with `std`, however sadly
the `std::io` module has not yet been added to the `core` crate.
Thus, in `no-std`, the `rust-bitcoin` ecosystem has historically
used the `core2` crate to provide copies of the `std::io` module
without any major dependencies. Sadly, its one dependency,
`memchr`, recently broke our MSRV.
Worse, because we didn't want to take on any excess dependencies
for `std` builds, `rust-bitcoin` has had to have
mutually-exclusive `std` and `no-std` builds. This breaks general
assumptions about how features work in Rust, causing substantial
pain for applications far downstream of `rust-bitcoin` crates.
Here, we add a new `bitcoin_io` crate, making it an unconditional
dependency and using its `io` module in the in-repository crates
in place of `std::io` and `core2::io`. As it is not substantial
additional code, the `hashes` io implementations are no longer
feature-gated.
This doesn't actually accomplish anything on its own, only adding
the new crate which still depends on `core2`.
2023-10-04 05:51:26 +00:00
|
|
|
//! `std::io`'s traits without unnecessary complexity.
|
|
|
|
|
2023-11-28 00:58:52 +00:00
|
|
|
#![cfg_attr(not(feature = "std"), no_std)]
|
Add a `bitcoin_io` crate
In order to support standard (de)serialization of structs, the
`rust-bitcoin` ecosystem uses the standard `std::io::{Read,Write}`
traits. This works great for environments with `std`, however sadly
the `std::io` module has not yet been added to the `core` crate.
Thus, in `no-std`, the `rust-bitcoin` ecosystem has historically
used the `core2` crate to provide copies of the `std::io` module
without any major dependencies. Sadly, its one dependency,
`memchr`, recently broke our MSRV.
Worse, because we didn't want to take on any excess dependencies
for `std` builds, `rust-bitcoin` has had to have
mutually-exclusive `std` and `no-std` builds. This breaks general
assumptions about how features work in Rust, causing substantial
pain for applications far downstream of `rust-bitcoin` crates.
Here, we add a new `bitcoin_io` crate, making it an unconditional
dependency and using its `io` module in the in-repository crates
in place of `std::io` and `core2::io`. As it is not substantial
additional code, the `hashes` io implementations are no longer
feature-gated.
This doesn't actually accomplish anything on its own, only adding
the new crate which still depends on `core2`.
2023-10-04 05:51:26 +00:00
|
|
|
// Experimental features we need.
|
|
|
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
|
|
|
|
2023-09-09 23:31:48 +00:00
|
|
|
#[cfg(any(feature = "alloc", feature = "std"))]
|
|
|
|
extern crate alloc;
|
|
|
|
|
2023-11-28 01:09:35 +00:00
|
|
|
mod error;
|
2023-11-28 00:43:35 +00:00
|
|
|
mod macros;
|
|
|
|
|
2023-12-07 23:41:41 +00:00
|
|
|
use core::cmp;
|
2023-12-17 00:59:05 +00:00
|
|
|
use core::convert::TryInto;
|
2023-12-07 23:40:40 +00:00
|
|
|
|
2023-11-28 01:09:35 +00:00
|
|
|
#[rustfmt::skip] // Keep public re-exports separate.
|
|
|
|
pub use self::error::{Error, ErrorKind};
|
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
pub type Result<T> = core::result::Result<T, Error>;
|
2023-09-11 17:57:07 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
/// A generic trait describing an input stream. See [`std::io::Read`] for more info.
|
|
|
|
pub trait Read {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
|
|
|
|
while !buf.is_empty() {
|
|
|
|
match self.read(buf) {
|
|
|
|
Ok(0) => return Err(ErrorKind::UnexpectedEof.into()),
|
|
|
|
Ok(len) => buf = &mut buf[len..],
|
|
|
|
Err(e) if e.kind() == ErrorKind::Interrupted => {}
|
|
|
|
Err(e) => return Err(e),
|
2023-09-11 17:57:07 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-28 00:49:26 +00:00
|
|
|
Ok(())
|
2023-09-11 17:57:07 +00:00
|
|
|
}
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn take(&mut self, limit: u64) -> Take<Self> { Take { reader: self, remaining: limit } }
|
|
|
|
}
|
2023-09-11 17:57:07 +00:00
|
|
|
|
2023-12-08 00:43:51 +00:00
|
|
|
/// A trait describing an input stream that uses an internal buffer when reading.
|
|
|
|
pub trait BufRead: Read {
|
|
|
|
/// Returns data read from this reader, filling the internal buffer if needed.
|
|
|
|
fn fill_buf(&mut self) -> Result<&[u8]>;
|
|
|
|
|
|
|
|
/// Marks the buffered data up to amount as consumed.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// May panic if `amount` is greater than amount of data read by `fill_buf`.
|
|
|
|
fn consume(&mut self, amount: usize);
|
|
|
|
}
|
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
pub struct Take<'a, R: Read + ?Sized> {
|
|
|
|
reader: &'a mut R,
|
|
|
|
remaining: u64,
|
|
|
|
}
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
impl<'a, R: Read + ?Sized> Read for Take<'a, R> {
|
|
|
|
#[inline]
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
2023-12-07 23:41:41 +00:00
|
|
|
let len = cmp::min(buf.len(), self.remaining.try_into().unwrap_or(buf.len()));
|
2023-11-28 00:49:26 +00:00
|
|
|
let read = self.reader.read(&mut buf[..len])?;
|
|
|
|
self.remaining -= read.try_into().unwrap_or(self.remaining);
|
|
|
|
Ok(read)
|
2023-09-11 17:57:07 +00:00
|
|
|
}
|
2023-11-28 00:49:26 +00:00
|
|
|
}
|
2023-09-11 17:57:07 +00:00
|
|
|
|
2023-12-08 00:43:51 +00:00
|
|
|
// Impl copied from Rust stdlib.
|
|
|
|
impl<'a, R: BufRead + ?Sized> BufRead for Take<'a, R> {
|
|
|
|
#[inline]
|
|
|
|
fn fill_buf(&mut self) -> Result<&[u8]> {
|
|
|
|
// Don't call into inner reader at all at EOF because it may still block
|
|
|
|
if self.remaining == 0 {
|
|
|
|
return Ok(&[]);
|
|
|
|
}
|
|
|
|
|
|
|
|
let buf = self.reader.fill_buf()?;
|
|
|
|
// Cast length to a u64 instead of casting `remaining` to a `usize`
|
|
|
|
// (in case `remaining > u32::MAX` and we are on a 32 bit machine).
|
|
|
|
let cap = cmp::min(buf.len() as u64, self.remaining) as usize;
|
|
|
|
Ok(&buf[..cap])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn consume(&mut self, amount: usize) {
|
|
|
|
assert!(amount as u64 <= self.remaining);
|
|
|
|
self.remaining -= amount as u64;
|
|
|
|
self.reader.consume(amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl<R: std::io::Read> Read for R {
|
|
|
|
#[inline]
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
|
|
|
Ok(<R as std::io::Read>::read(self, buf)?)
|
2023-09-11 17:57:07 +00:00
|
|
|
}
|
2023-11-28 00:49:26 +00:00
|
|
|
}
|
2023-09-11 17:57:07 +00:00
|
|
|
|
2023-12-08 00:43:51 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl<R: std::io::BufRead + Read + ?Sized> BufRead for R {
|
|
|
|
#[inline]
|
|
|
|
fn fill_buf(&mut self) -> Result<&[u8]> { Ok(std::io::BufRead::fill_buf(self)?) }
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn consume(&mut self, amount: usize) { std::io::BufRead::consume(self, amount) }
|
|
|
|
}
|
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
impl Read for &[u8] {
|
|
|
|
#[inline]
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
2023-12-07 23:41:41 +00:00
|
|
|
let cnt = cmp::min(self.len(), buf.len());
|
2023-11-28 00:49:26 +00:00
|
|
|
buf[..cnt].copy_from_slice(&self[..cnt]);
|
|
|
|
*self = &self[cnt..];
|
|
|
|
Ok(cnt)
|
2023-09-12 05:21:50 +00:00
|
|
|
}
|
2023-11-28 00:49:26 +00:00
|
|
|
}
|
2023-09-12 05:21:50 +00:00
|
|
|
|
2023-12-08 00:43:51 +00:00
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
impl BufRead for &[u8] {
|
|
|
|
#[inline]
|
|
|
|
fn fill_buf(&mut self) -> Result<&[u8]> { Ok(self) }
|
|
|
|
|
|
|
|
// This panics if amount is out of bounds, same as the std version.
|
|
|
|
#[inline]
|
|
|
|
fn consume(&mut self, amount: usize) { *self = &self[amount..] }
|
|
|
|
}
|
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
pub struct Cursor<T> {
|
|
|
|
inner: T,
|
|
|
|
pos: u64,
|
|
|
|
}
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
impl<T: AsRef<[u8]>> Cursor<T> {
|
|
|
|
#[inline]
|
|
|
|
pub fn new(inner: T) -> Self { Cursor { inner, pos: 0 } }
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn position(&self) -> u64 { self.pos }
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn into_inner(self) -> T { self.inner }
|
|
|
|
}
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
impl<T: AsRef<[u8]>> Read for Cursor<T> {
|
|
|
|
#[inline]
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
|
|
|
let inner: &[u8] = self.inner.as_ref();
|
|
|
|
let start_pos = self.pos.try_into().unwrap_or(inner.len());
|
|
|
|
let read = core::cmp::min(inner.len().saturating_sub(start_pos), buf.len());
|
|
|
|
buf[..read].copy_from_slice(&inner[start_pos..start_pos + read]);
|
2023-12-03 00:58:10 +00:00
|
|
|
self.pos =
|
|
|
|
self.pos.saturating_add(read.try_into().unwrap_or(u64::max_value() /* unreachable */));
|
2023-11-28 00:49:26 +00:00
|
|
|
Ok(read)
|
2023-09-11 17:57:07 +00:00
|
|
|
}
|
2023-11-28 00:49:26 +00:00
|
|
|
}
|
2023-09-09 23:31:48 +00:00
|
|
|
|
2023-12-08 00:43:51 +00:00
|
|
|
impl<T: AsRef<[u8]>> BufRead for Cursor<T> {
|
|
|
|
#[inline]
|
|
|
|
fn fill_buf(&mut self) -> Result<&[u8]> {
|
|
|
|
let inner: &[u8] = self.inner.as_ref();
|
|
|
|
Ok(&inner[self.pos as usize..])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn consume(&mut self, amount: usize) {
|
|
|
|
assert!(amount <= self.inner.as_ref().len());
|
|
|
|
self.pos += amount as u64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
/// A generic trait describing an output stream. See [`std::io::Write`] for more info.
|
|
|
|
pub trait Write {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize>;
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
fn flush(&mut self) -> Result<()>;
|
2023-09-09 23:31:48 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
|
|
|
|
while !buf.is_empty() {
|
|
|
|
match self.write(buf) {
|
|
|
|
Ok(0) => return Err(ErrorKind::UnexpectedEof.into()),
|
|
|
|
Ok(len) => buf = &buf[len..],
|
|
|
|
Err(e) if e.kind() == ErrorKind::Interrupted => {}
|
|
|
|
Err(e) => return Err(e),
|
2023-09-12 17:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-28 00:49:26 +00:00
|
|
|
Ok(())
|
2023-09-12 17:47:37 +00:00
|
|
|
}
|
2023-11-28 00:49:26 +00:00
|
|
|
}
|
2023-09-12 17:47:37 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl<W: std::io::Write> Write for W {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
|
|
|
Ok(<W as std::io::Write>::write(self, buf)?)
|
2023-09-12 17:47:37 +00:00
|
|
|
}
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> Result<()> { Ok(<W as std::io::Write>::flush(self)?) }
|
|
|
|
}
|
2023-09-12 17:47:37 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
|
|
|
impl Write for alloc::vec::Vec<u8> {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
|
|
|
self.extend_from_slice(buf);
|
|
|
|
Ok(buf.len())
|
2023-09-12 17:47:37 +00:00
|
|
|
}
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> Result<()> { Ok(()) }
|
|
|
|
}
|
2023-09-12 17:47:37 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
impl<'a> Write for &'a mut [u8] {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
|
|
|
let cnt = core::cmp::min(self.len(), buf.len());
|
|
|
|
self[..cnt].copy_from_slice(&buf[..cnt]);
|
|
|
|
*self = &mut core::mem::take(self)[cnt..];
|
|
|
|
Ok(cnt)
|
2023-09-12 17:47:37 +00:00
|
|
|
}
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> Result<()> { Ok(()) }
|
|
|
|
}
|
2023-09-12 17:47:47 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
/// A sink to which all writes succeed. See [`std::io::Sink`] for more info.
|
|
|
|
pub struct Sink;
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
impl Write for Sink {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize> { Ok(buf.len()) }
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn write_all(&mut self, _: &[u8]) -> Result<()> { Ok(()) }
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> Result<()> { Ok(()) }
|
2023-09-09 23:31:48 +00:00
|
|
|
}
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl std::io::Write for Sink {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { Ok(buf.len()) }
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn write_all(&mut self, _: &[u8]) -> std::io::Result<()> { Ok(()) }
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> std::io::Result<()> { Ok(()) }
|
|
|
|
}
|
2023-11-28 00:55:30 +00:00
|
|
|
|
2023-11-28 00:49:26 +00:00
|
|
|
/// Returns a sink to which all writes succeed. See [`std::io::sink`] for more info.
|
2023-11-29 00:17:59 +00:00
|
|
|
#[inline]
|
2023-11-28 00:49:26 +00:00
|
|
|
pub fn sink() -> Sink { Sink }
|
2023-12-08 00:43:51 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn buf_read_fill_and_consume_slice() {
|
|
|
|
let data = [0_u8, 1, 2];
|
|
|
|
|
|
|
|
let mut slice = &data[..];
|
|
|
|
|
|
|
|
let fill = BufRead::fill_buf(&mut slice).unwrap();
|
|
|
|
assert_eq!(fill.len(), 3);
|
|
|
|
assert_eq!(fill, &[0_u8, 1, 2]);
|
|
|
|
slice.consume(2);
|
|
|
|
|
|
|
|
let fill = BufRead::fill_buf(&mut slice).unwrap();
|
|
|
|
assert_eq!(fill.len(), 1);
|
|
|
|
assert_eq!(fill, &[2_u8]);
|
|
|
|
slice.consume(1);
|
|
|
|
|
|
|
|
// checks we can attempt to read from a now-empty reader.
|
|
|
|
let fill = BufRead::fill_buf(&mut slice).unwrap();
|
|
|
|
assert_eq!(fill.len(), 0);
|
|
|
|
assert_eq!(fill, &[]);
|
|
|
|
}
|
|
|
|
}
|