Inline io module in io crate root

Its not immediately obvious why we nest the whole `io` code in an `io`
submodule within `lib.rs`. As far as I can tell we can inline it and
re-export from `rust-bitcoin` same as we do for our other dependencies.

This change would effect other users of the crate but since the `io`
crate is unreleased this effects no-one except us.
This commit is contained in:
Tobin C. Harding 2023-11-28 11:49:26 +11:00
parent 80fe9b99b2
commit 5c0759a390
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
6 changed files with 333 additions and 333 deletions

View File

@ -27,8 +27,8 @@ bitcoinconsensus-std = ["bitcoinconsensus/std", "std"]
# The no-std feature doesn't disable std - you need to turn off the std feature for that by disabling default. # The no-std feature doesn't disable std - you need to turn off the std feature for that by disabling default.
# Instead no-std enables additional features required for this crate to be usable without std. # Instead no-std enables additional features required for this crate to be usable without std.
# As a result, both can be enabled without conflict. # As a result, both can be enabled without conflict.
std = ["secp256k1/std", "bitcoin-io/std", "hashes/std", "bech32/std", "internals/std", "hex/std"] std = ["secp256k1/std", "io/std", "hashes/std", "bech32/std", "internals/std", "hex/std"]
no-std = ["hashes/alloc", "hashes/io", "bitcoin-io/alloc", "bech32/alloc", "secp256k1/alloc", "hex/alloc"] no-std = ["hashes/alloc", "hashes/io", "io/alloc", "bech32/alloc", "secp256k1/alloc", "hex/alloc"]
[package.metadata.docs.rs] [package.metadata.docs.rs]
all-features = true all-features = true
@ -40,10 +40,9 @@ hex = { package = "hex-conservative", version = "0.1.1", default-features = fals
bech32 = { version = "0.10.0-beta", default-features = false } bech32 = { version = "0.10.0-beta", default-features = false }
hashes = { package = "bitcoin_hashes", version = "0.13.0", default-features = false } hashes = { package = "bitcoin_hashes", version = "0.13.0", default-features = false }
secp256k1 = { version = "0.28.0", default-features = false, features = ["hashes"] } secp256k1 = { version = "0.28.0", default-features = false, features = ["hashes"] }
io = { package = "bitcoin-io", version = "0.1", default-features = false }
hex_lit = "0.1.1" hex_lit = "0.1.1"
bitcoin-io = { version = "0.1", default-features = false }
base64 = { version = "0.21.3", optional = true } base64 = { version = "0.21.3", optional = true }
# Only use this feature for no-std builds, otherwise use bitcoinconsensus-std. # Only use this feature for no-std builds, otherwise use bitcoinconsensus-std.
bitcoinconsensus = { version = "0.20.2-0.5.0", default-features = false, optional = true } bitcoinconsensus = { version = "0.20.2-0.5.0", default-features = false, optional = true }

View File

@ -74,6 +74,9 @@ pub extern crate hashes;
/// Re-export the `hex-conservative` crate. /// Re-export the `hex-conservative` crate.
pub extern crate hex; pub extern crate hex;
/// Re-export the `bitcoin-io` crate.
pub extern crate io;
/// Rust wrapper library for Pieter Wuille's libsecp256k1. Implements ECDSA and BIP 340 signatures /// Rust wrapper library for Pieter Wuille's libsecp256k1. Implements ECDSA and BIP 340 signatures
/// for the SECG elliptic curve group secp256k1 and related utilities. /// for the SECG elliptic curve group secp256k1 and related utilities.
pub extern crate secp256k1; pub extern crate secp256k1;
@ -113,7 +116,6 @@ pub mod sign_message;
pub mod string; pub mod string;
pub mod taproot; pub mod taproot;
use bitcoin_io::io;
#[rustfmt::skip] // Keep public re-exports separate. #[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)] #[doc(inline)]

View File

@ -10,7 +10,7 @@ extern crate bitcoin_hashes;
#[cfg(feature = "alloc")] use alloc::string::ToString; #[cfg(feature = "alloc")] use alloc::string::ToString;
use bitcoin_hashes::{sha256, Hash, HashEngine}; use bitcoin_hashes::{sha256, Hash, HashEngine};
use bitcoin_io::io::Write; use bitcoin_io::Write;
use core::str::FromStr; use core::str::FromStr;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln}; use cortex_m_semihosting::{debug, hprintln};

View File

@ -66,7 +66,7 @@ impl_write!(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use bitcoin_io::io::Write; use bitcoin_io::Write;
use crate::{hash160, hmac, ripemd160, sha1, sha256, sha256d, sha512, siphash24, Hash}; use crate::{hash160, hmac, ripemd160, sha1, sha256, sha256d, sha512, siphash24, Hash};

View File

@ -24,14 +24,13 @@ mod macros;
/// Standard I/O stream definitions which are API-equivalent to `std`'s `io` module. See /// Standard I/O stream definitions which are API-equivalent to `std`'s `io` module. See
/// [`std::io`] for more info. /// [`std::io`] for more info.
pub mod io { #[cfg(any(feature = "alloc", feature = "std"))]
#[cfg(any(feature = "alloc", feature = "std"))] use alloc::boxed::Box;
use alloc::boxed::Box; use core::convert::TryInto;
use core::convert::TryInto; use core::fmt::{Debug, Display, Formatter};
use core::fmt::{Debug, Display, Formatter};
#[cfg(all(feature = "alloc", not(feature = "std")))] #[cfg(all(feature = "alloc", not(feature = "std")))]
mod sealed { mod sealed {
use alloc::boxed::Box; use alloc::boxed::Box;
use alloc::string::String; use alloc::string::String;
use core::fmt::Debug; use core::fmt::Debug;
@ -44,18 +43,18 @@ pub mod io {
impl IntoBoxDynDebug for String { impl IntoBoxDynDebug for String {
fn into(self) -> Box<dyn Debug + Send + Sync + 'static> { Box::new(self) } fn into(self) -> Box<dyn Debug + Send + Sync + 'static> { Box::new(self) }
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Error { pub struct Error {
kind: ErrorKind, kind: ErrorKind,
#[cfg(feature = "std")] #[cfg(feature = "std")]
error: Option<Box<dyn std::error::Error + Send + Sync + 'static>>, error: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
#[cfg(all(feature = "alloc", not(feature = "std")))] #[cfg(all(feature = "alloc", not(feature = "std")))]
error: Option<Box<dyn Debug + Send + Sync + 'static>>, error: Option<Box<dyn Debug + Send + Sync + 'static>>,
} }
impl Error { impl Error {
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn new<E>(kind: ErrorKind, error: E) -> Error pub fn new<E>(kind: ErrorKind, error: E) -> Error
where where
@ -69,9 +68,9 @@ pub mod io {
} }
pub fn kind(&self) -> ErrorKind { self.kind } pub fn kind(&self) -> ErrorKind { self.kind }
} }
impl From<ErrorKind> for Error { impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error { fn from(kind: ErrorKind) -> Error {
Self { Self {
kind, kind,
@ -79,9 +78,9 @@ pub mod io {
error: None, error: None,
} }
} }
} }
impl Display for Error { impl Display for Error {
fn fmt(&self, fmt: &mut Formatter) -> core::result::Result<(), core::fmt::Error> { fn fmt(&self, fmt: &mut Formatter) -> core::result::Result<(), core::fmt::Error> {
fmt.write_fmt(format_args!("I/O Error: {}", self.kind.description()))?; fmt.write_fmt(format_args!("I/O Error: {}", self.kind.description()))?;
#[cfg(any(feature = "alloc", feature = "std"))] #[cfg(any(feature = "alloc", feature = "std"))]
@ -90,10 +89,10 @@ pub mod io {
} }
Ok(()) Ok(())
} }
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl std::error::Error for Error { impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.error.as_ref().and_then(|e| e.as_ref().source()) self.error.as_ref().and_then(|e| e.as_ref().source())
} }
@ -108,9 +107,9 @@ pub mod io {
fn cause(&self) -> Option<&dyn std::error::Error> { fn cause(&self) -> Option<&dyn std::error::Error> {
self.error.as_ref().and_then(|e| e.as_ref().cause()) self.error.as_ref().and_then(|e| e.as_ref().cause())
} }
} }
impl Error { impl Error {
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn get_ref(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)> { pub fn get_ref(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)> {
self.error.as_deref() self.error.as_deref()
@ -119,17 +118,17 @@ pub mod io {
pub fn get_ref(&self) -> Option<&(dyn Debug + Send + Sync + 'static)> { pub fn get_ref(&self) -> Option<&(dyn Debug + Send + Sync + 'static)> {
self.error.as_deref() self.error.as_deref()
} }
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl From<std::io::Error> for Error { impl From<std::io::Error> for Error {
fn from(o: std::io::Error) -> Error { fn from(o: std::io::Error) -> Error {
Self { kind: ErrorKind::from_std(o.kind()), error: o.into_inner() } Self { kind: ErrorKind::from_std(o.kind()), error: o.into_inner() }
} }
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl From<Error> for std::io::Error { impl From<Error> for std::io::Error {
fn from(o: Error) -> std::io::Error { fn from(o: Error) -> std::io::Error {
if let Some(err) = o.error { if let Some(err) = o.error {
std::io::Error::new(o.kind.to_std(), err) std::io::Error::new(o.kind.to_std(), err)
@ -137,9 +136,9 @@ pub mod io {
o.kind.to_std().into() o.kind.to_std().into()
} }
} }
} }
macro_rules! define_errorkind { macro_rules! define_errorkind {
($($kind: ident),*) => { ($($kind: ident),*) => {
#[non_exhaustive] #[non_exhaustive]
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
@ -172,9 +171,9 @@ pub mod io {
} }
} }
} }
} }
define_errorkind!( define_errorkind!(
NotFound, NotFound,
PermissionDenied, PermissionDenied,
ConnectionRefused, ConnectionRefused,
@ -194,12 +193,12 @@ pub mod io {
UnexpectedEof, UnexpectedEof,
// Note: Any time we bump the MSRV any new error kinds should be added here! // Note: Any time we bump the MSRV any new error kinds should be added here!
Other Other
); );
pub type Result<T> = core::result::Result<T, Error>; pub type Result<T> = core::result::Result<T, Error>;
/// A generic trait describing an input stream. See [`std::io::Read`] for more info. /// A generic trait describing an input stream. See [`std::io::Read`] for more info.
pub trait Read { pub trait Read {
fn read(&mut self, buf: &mut [u8]) -> Result<usize>; fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
#[inline] #[inline]
fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> { fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
@ -215,13 +214,13 @@ pub mod io {
} }
#[inline] #[inline]
fn take(&mut self, limit: u64) -> Take<Self> { Take { reader: self, remaining: limit } } fn take(&mut self, limit: u64) -> Take<Self> { Take { reader: self, remaining: limit } }
} }
pub struct Take<'a, R: Read + ?Sized> { pub struct Take<'a, R: Read + ?Sized> {
reader: &'a mut R, reader: &'a mut R,
remaining: u64, remaining: u64,
} }
impl<'a, R: Read + ?Sized> Read for Take<'a, R> { impl<'a, R: Read + ?Sized> Read for Take<'a, R> {
#[inline] #[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let len = core::cmp::min(buf.len(), self.remaining.try_into().unwrap_or(buf.len())); let len = core::cmp::min(buf.len(), self.remaining.try_into().unwrap_or(buf.len()));
@ -229,18 +228,18 @@ pub mod io {
self.remaining -= read.try_into().unwrap_or(self.remaining); self.remaining -= read.try_into().unwrap_or(self.remaining);
Ok(read) Ok(read)
} }
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl<R: std::io::Read> Read for R { impl<R: std::io::Read> Read for R {
#[inline] #[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
Ok(<R as std::io::Read>::read(self, buf)?) Ok(<R as std::io::Read>::read(self, buf)?)
} }
} }
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
impl Read for &[u8] { impl Read for &[u8] {
#[inline] #[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let cnt = core::cmp::min(self.len(), buf.len()); let cnt = core::cmp::min(self.len(), buf.len());
@ -248,21 +247,21 @@ pub mod io {
*self = &self[cnt..]; *self = &self[cnt..];
Ok(cnt) Ok(cnt)
} }
} }
pub struct Cursor<T> { pub struct Cursor<T> {
inner: T, inner: T,
pos: u64, pos: u64,
} }
impl<T: AsRef<[u8]>> Cursor<T> { impl<T: AsRef<[u8]>> Cursor<T> {
#[inline] #[inline]
pub fn new(inner: T) -> Self { Cursor { inner, pos: 0 } } pub fn new(inner: T) -> Self { Cursor { inner, pos: 0 } }
#[inline] #[inline]
pub fn position(&self) -> u64 { self.pos } pub fn position(&self) -> u64 { self.pos }
#[inline] #[inline]
pub fn into_inner(self) -> T { self.inner } pub fn into_inner(self) -> T { self.inner }
} }
impl<T: AsRef<[u8]>> Read for Cursor<T> { impl<T: AsRef<[u8]>> Read for Cursor<T> {
#[inline] #[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let inner: &[u8] = self.inner.as_ref(); let inner: &[u8] = self.inner.as_ref();
@ -274,10 +273,10 @@ pub mod io {
.saturating_add(read.try_into().unwrap_or(u64::max_value() /* unreachable */)); .saturating_add(read.try_into().unwrap_or(u64::max_value() /* unreachable */));
Ok(read) Ok(read)
} }
} }
/// A generic trait describing an output stream. See [`std::io::Write`] for more info. /// A generic trait describing an output stream. See [`std::io::Write`] for more info.
pub trait Write { pub trait Write {
fn write(&mut self, buf: &[u8]) -> Result<usize>; fn write(&mut self, buf: &[u8]) -> Result<usize>;
fn flush(&mut self) -> Result<()>; fn flush(&mut self) -> Result<()>;
@ -293,20 +292,20 @@ pub mod io {
} }
Ok(()) Ok(())
} }
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl<W: std::io::Write> Write for W { impl<W: std::io::Write> Write for W {
#[inline] #[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> { fn write(&mut self, buf: &[u8]) -> Result<usize> {
Ok(<W as std::io::Write>::write(self, buf)?) Ok(<W as std::io::Write>::write(self, buf)?)
} }
#[inline] #[inline]
fn flush(&mut self) -> Result<()> { Ok(<W as std::io::Write>::flush(self)?) } fn flush(&mut self) -> Result<()> { Ok(<W as std::io::Write>::flush(self)?) }
} }
#[cfg(all(feature = "alloc", not(feature = "std")))] #[cfg(all(feature = "alloc", not(feature = "std")))]
impl Write for alloc::vec::Vec<u8> { impl Write for alloc::vec::Vec<u8> {
#[inline] #[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> { fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.extend_from_slice(buf); self.extend_from_slice(buf);
@ -314,10 +313,10 @@ pub mod io {
} }
#[inline] #[inline]
fn flush(&mut self) -> Result<()> { Ok(()) } fn flush(&mut self) -> Result<()> { Ok(()) }
} }
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
impl<'a> Write for &'a mut [u8] { impl<'a> Write for &'a mut [u8] {
#[inline] #[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> { fn write(&mut self, buf: &[u8]) -> Result<usize> {
let cnt = core::cmp::min(self.len(), buf.len()); let cnt = core::cmp::min(self.len(), buf.len());
@ -327,28 +326,28 @@ pub mod io {
} }
#[inline] #[inline]
fn flush(&mut self) -> Result<()> { Ok(()) } fn flush(&mut self) -> Result<()> { Ok(()) }
} }
/// A sink to which all writes succeed. See [`std::io::Sink`] for more info. /// A sink to which all writes succeed. See [`std::io::Sink`] for more info.
pub struct Sink; pub struct Sink;
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
impl Write for Sink { impl Write for Sink {
#[inline] #[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> { Ok(buf.len()) } fn write(&mut self, buf: &[u8]) -> Result<usize> { Ok(buf.len()) }
#[inline] #[inline]
fn write_all(&mut self, _: &[u8]) -> Result<()> { Ok(()) } fn write_all(&mut self, _: &[u8]) -> Result<()> { Ok(()) }
#[inline] #[inline]
fn flush(&mut self) -> Result<()> { Ok(()) } fn flush(&mut self) -> Result<()> { Ok(()) }
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl std::io::Write for Sink { impl std::io::Write for Sink {
#[inline] #[inline]
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { Ok(buf.len()) } fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { Ok(buf.len()) }
#[inline] #[inline]
fn write_all(&mut self, _: &[u8]) -> std::io::Result<()> { Ok(()) } fn write_all(&mut self, _: &[u8]) -> std::io::Result<()> { Ok(()) }
#[inline] #[inline]
fn flush(&mut self) -> std::io::Result<()> { Ok(()) } fn flush(&mut self) -> std::io::Result<()> { Ok(()) }
}
/// Returns a sink to which all writes succeed. See [`std::io::sink`] for more info.
pub fn sink() -> Sink { Sink }
} }
/// Returns a sink to which all writes succeed. See [`std::io::sink`] for more info.
pub fn sink() -> Sink { Sink }

View File

@ -11,13 +11,13 @@
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
macro_rules! impl_write { macro_rules! impl_write {
($ty: ty, $write_fn: expr, $flush_fn: expr $(, $bounded_ty: ident : $bounds: path),*) => { ($ty: ty, $write_fn: expr, $flush_fn: expr $(, $bounded_ty: ident : $bounds: path),*) => {
impl<$($bounded_ty: $bounds),*> $crate::io::Write for $ty { impl<$($bounded_ty: $bounds),*> $crate::Write for $ty {
#[inline] #[inline]
fn write(&mut self, buf: &[u8]) -> $crate::io::Result<usize> { fn write(&mut self, buf: &[u8]) -> $crate::Result<usize> {
$write_fn(self, buf) $write_fn(self, buf)
} }
#[inline] #[inline]
fn flush(&mut self) -> $crate::io::Result<()> { fn flush(&mut self) -> $crate::Result<()> {
$flush_fn(self) $flush_fn(self)
} }
} }