Merge rust-bitcoin/rust-bitcoin#2453: io: Remove blanket trait impls

db888fa4cc io: Remove blanket trait impls (Tobin C. Harding)

Pull request description:

  Remove the blanket impls of `Read`, `BufRead`, and `Write`. Replace them with just the impls required to get `rust-bitcoin` building.

  Note, the `TcpStream` stuff is used in `examples/handshake.rs`.

  Fix: #2432

ACKs for top commit:
  Kixunil:
    ACK db888fa4cc
  apoelstra:
    ACK db888fa4cc

Tree-SHA512: 24a3196e740f7e16a493c2f54fb9bf875fceab66c8973ffe28c7cfc9e1a440e14a36d919cc1d8055ac9da8cd4ffb0fc26fff058b8dbb9da4768d7cf4c07ec48a
This commit is contained in:
Andrew Poelstra 2024-02-12 19:59:51 +00:00
commit 4544f5d3c2
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 36 additions and 33 deletions

View File

@ -151,24 +151,6 @@ impl<'a, R: BufRead + ?Sized> BufRead for Take<'a, R> {
}
}
#[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)?)
}
}
#[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) }
}
#[cfg(not(feature = "std"))] // Conflicts with blanket impl when "std" is enabled.
impl Read for &[u8] {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
@ -179,7 +161,12 @@ impl Read for &[u8] {
}
}
#[cfg(not(feature = "std"))] // Conflicts with blanket impl when "std" is enabled.
#[cfg(feature = "std")]
impl<R: std::io::Read> Read for std::io::BufReader<R> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { Ok(std::io::Read::read(self, buf)?) }
}
impl BufRead for &[u8] {
#[inline]
fn fill_buf(&mut self) -> Result<&[u8]> { Ok(self) }
@ -189,6 +176,15 @@ impl BufRead for &[u8] {
fn consume(&mut self, amount: usize) { *self = &self[amount..] }
}
#[cfg(feature = "std")]
impl<R: std::io::Read> BufRead for std::io::BufReader<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) }
}
/// Wraps an in memory reader providing the `position` function.
pub struct Cursor<T> {
inner: T,
@ -262,18 +258,7 @@ pub trait Write {
}
}
#[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)?)
}
#[inline]
fn flush(&mut self) -> Result<()> { Ok(<W as std::io::Write>::flush(self)?) }
}
#[cfg(all(feature = "alloc", not(feature = "std")))]
#[cfg(feature = "alloc")]
impl Write for alloc::vec::Vec<u8> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> {
@ -285,7 +270,6 @@ impl Write for alloc::vec::Vec<u8> {
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> {
@ -299,12 +283,20 @@ impl<'a> Write for &'a mut [u8] {
fn flush(&mut self) -> Result<()> { Ok(()) }
}
#[cfg(feature = "std")]
impl<W: std::io::Write> Write for std::io::BufWriter<W> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> { Ok(std::io::Write::write(self, buf)?) }
#[inline]
fn flush(&mut self) -> Result<()> { Ok(std::io::Write::flush(self)?) }
}
/// A sink to which all writes succeed. See [`std::io::Sink`] for more info.
///
/// Created using `io::sink()`.
pub struct Sink;
#[cfg(not(feature = "std"))]
impl Write for Sink {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> { Ok(buf.len()) }

View File

@ -45,5 +45,16 @@ macro_rules! impl_write {
$flush_fn(self)
}
}
impl<$($bounded_ty: $bounds),*> $crate::Write for $ty {
#[inline]
fn write(&mut self, buf: &[u8]) -> $crate::Result<usize> {
$write_fn(self, buf)
}
#[inline]
fn flush(&mut self) -> $crate::Result<()> {
$flush_fn(self)
}
}
}
}