io: Remove blanket trait impls
Remove the blanket impls of `Read`, `BufRead`, and `Write`. Replace them with a set of sane impls. Note, we add code to the `impl_write` macro to implement both `crate::Write` and `std::io::Write` when "std" feature is enabled. Fix: #2432
This commit is contained in:
parent
814b72779f
commit
db888fa4cc
|
@ -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] {
|
impl Read for &[u8] {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
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] {
|
impl BufRead for &[u8] {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn fill_buf(&mut self) -> Result<&[u8]> { Ok(self) }
|
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..] }
|
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.
|
/// Wraps an in memory reader providing the `position` function.
|
||||||
pub struct Cursor<T> {
|
pub struct Cursor<T> {
|
||||||
inner: T,
|
inner: T,
|
||||||
|
@ -262,18 +258,7 @@ pub trait Write {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "alloc")]
|
||||||
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")))]
|
|
||||||
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> {
|
||||||
|
@ -285,7 +270,6 @@ impl Write for alloc::vec::Vec<u8> {
|
||||||
fn flush(&mut self) -> Result<()> { Ok(()) }
|
fn flush(&mut self) -> Result<()> { Ok(()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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> {
|
||||||
|
@ -299,12 +283,20 @@ impl<'a> Write for &'a mut [u8] {
|
||||||
fn flush(&mut self) -> Result<()> { Ok(()) }
|
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.
|
/// A sink to which all writes succeed. See [`std::io::Sink`] for more info.
|
||||||
///
|
///
|
||||||
/// Created using `io::sink()`.
|
/// Created using `io::sink()`.
|
||||||
pub struct Sink;
|
pub struct Sink;
|
||||||
|
|
||||||
#[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()) }
|
||||||
|
|
|
@ -45,5 +45,16 @@ macro_rules! impl_write {
|
||||||
$flush_fn(self)
|
$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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue