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
|
|
|
|
//!
|
|
|
|
//! Because the core `std::io` module is not yet exposed in `no-std` Rust, building `no-std`
|
|
|
|
//! applications which require reading and writing objects via standard traits is not generally
|
|
|
|
//! possible. While there is ongoing work to improve this situation, this module is not likely to
|
|
|
|
//! be available for applications with broad rustc version support for some time.
|
|
|
|
//!
|
|
|
|
//! Thus, this library exists to export a minmal version of `std::io`'s traits which `no-std`
|
|
|
|
//! applications may need. With the `std` feature, these traits are also implemented for the
|
|
|
|
//! `std::io` traits, allowing standard objects to be used wherever the traits from this crate are
|
|
|
|
//! required.
|
|
|
|
//!
|
|
|
|
//! This traits are not one-for-one drop-ins, but are as close as possible while still implementing
|
|
|
|
//! `std::io`'s traits without unnecessary complexity.
|
|
|
|
|
|
|
|
// Experimental features we need.
|
|
|
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
|
|
|
|
|
|
|
#![cfg_attr(not(feature = "std"), no_std)]
|
|
|
|
|
|
|
|
#[cfg(all(not(feature = "std"), not(feature = "core2")))]
|
|
|
|
compile_error!("At least one of std or core2 must be enabled");
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
pub use std::error;
|
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
pub use core2::error;
|
2023-09-09 23:31:48 +00:00
|
|
|
|
|
|
|
#[cfg(any(feature = "alloc", feature = "std"))]
|
|
|
|
extern crate alloc;
|
|
|
|
|
|
|
|
/// Standard I/O stream definitions which are API-equivalent to `std`'s `io` module. See
|
|
|
|
/// [`std::io`] for more info.
|
|
|
|
pub mod io {
|
|
|
|
#[cfg(all(not(feature = "std"), not(feature = "core2")))]
|
|
|
|
compile_error!("At least one of std or core2 must be enabled");
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
2023-09-12 17:47:37 +00:00
|
|
|
pub use std::io::{Read, sink, Cursor, Take, Error, ErrorKind, Result};
|
2023-09-09 23:31:48 +00:00
|
|
|
|
|
|
|
#[cfg(not(feature = "std"))]
|
2023-09-12 17:47:37 +00:00
|
|
|
pub use core2::io::{Read, Cursor, Take, Error, ErrorKind, Result};
|
2023-09-09 23:31:48 +00:00
|
|
|
|
2023-09-12 17:47:37 +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>;
|
|
|
|
fn flush(&mut self) -> Result<()>;
|
2023-09-09 23:31:48 +00:00
|
|
|
|
2023-09-12 17:47:37 +00:00
|
|
|
#[inline]
|
|
|
|
fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
|
|
|
|
while !buf.is_empty() {
|
|
|
|
match self.write(buf) {
|
|
|
|
Ok(0) => return Err(Error::new(ErrorKind::UnexpectedEof, "")),
|
|
|
|
Ok(len) => buf = &buf[len..],
|
|
|
|
Err(e) if e.kind() == ErrorKind::Interrupted => {}
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl<W: std::io::Write> Write for W {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
|
|
|
<W as std::io::Write>::write(self, buf)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> Result<()> {
|
|
|
|
<W as std::io::Write>::flush(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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())
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> Result<()> { Ok(()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> Result<()> { Ok(()) }
|
|
|
|
}
|
2023-09-09 23:31:48 +00:00
|
|
|
}
|
2023-09-12 19:32:16 +00:00
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
/// Re-export std for the below macro
|
|
|
|
pub use std as _std;
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
/// Because we cannot provide a blanket implementation of [`std::io::Write`] for all implementers
|
|
|
|
/// of this crate's `io::Write` trait, we provide this macro instead.
|
|
|
|
///
|
|
|
|
/// This macro will implement `Write` given a `write` and `flush` fn, either by implementing the
|
|
|
|
/// crate's native `io::Write` trait directly, or a more generic trait from `std` for users using
|
|
|
|
/// that feature. In any case, this crate's `io::Write` feature will be implemented for the given
|
|
|
|
/// type, even if indirectly.
|
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
macro_rules! impl_write {
|
|
|
|
($ty: ty, $write_fn: expr, $flush_fn: expr $(, $bounded_ty: ident : $bounds: path),*) => {
|
|
|
|
impl<$($bounded_ty: $bounds),*> $crate::io::Write for $ty {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> $crate::io::Result<usize> {
|
|
|
|
$write_fn(self, buf)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> $crate::io::Result<()> {
|
|
|
|
$flush_fn(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
/// Because we cannot provide a blanket implementation of [`std::io::Write`] for all implementers
|
|
|
|
/// of this crate's `io::Write` trait, we provide this macro instead.
|
|
|
|
///
|
|
|
|
/// This macro will implement `Write` given a `write` and `flush` fn, either by implementing the
|
|
|
|
/// crate's native `io::Write` trait directly, or a more generic trait from `std` for users using
|
|
|
|
/// that feature. In any case, this crate's `io::Write` feature will be implemented for the given
|
|
|
|
/// type, even if indirectly.
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
macro_rules! impl_write {
|
|
|
|
($ty: ty, $write_fn: expr, $flush_fn: expr $(, $bounded_ty: ident : $bounds: path),*) => {
|
|
|
|
impl<$($bounded_ty: $bounds),*> $crate::_std::io::Write for $ty {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> $crate::_std::io::Result<usize> {
|
|
|
|
$write_fn(self, buf)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> $crate::_std::io::Result<()> {
|
|
|
|
$flush_fn(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|