diff --git a/io/src/lib.rs b/io/src/lib.rs index 55ee4a56..790b88fc 100644 --- a/io/src/lib.rs +++ b/io/src/lib.rs @@ -151,24 +151,6 @@ impl<'a, R: BufRead + ?Sized> BufRead for Take<'a, R> { } } -#[cfg(feature = "std")] -impl Read for R { - #[inline] - fn read(&mut self, buf: &mut [u8]) -> Result { - Ok(::read(self, buf)?) - } -} - -#[cfg(feature = "std")] -impl 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 { @@ -179,7 +161,12 @@ impl Read for &[u8] { } } -#[cfg(not(feature = "std"))] // Conflicts with blanket impl when "std" is enabled. +#[cfg(feature = "std")] +impl Read for std::io::BufReader { + #[inline] + fn read(&mut self, buf: &mut [u8]) -> Result { 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 BufRead for std::io::BufReader { + #[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 { inner: T, @@ -262,18 +258,7 @@ pub trait Write { } } -#[cfg(feature = "std")] -impl Write for W { - #[inline] - fn write(&mut self, buf: &[u8]) -> Result { - Ok(::write(self, buf)?) - } - - #[inline] - fn flush(&mut self) -> Result<()> { Ok(::flush(self)?) } -} - -#[cfg(all(feature = "alloc", not(feature = "std")))] +#[cfg(feature = "alloc")] impl Write for alloc::vec::Vec { #[inline] fn write(&mut self, buf: &[u8]) -> Result { @@ -285,7 +270,6 @@ impl Write for alloc::vec::Vec { 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 { @@ -299,12 +283,20 @@ impl<'a> Write for &'a mut [u8] { fn flush(&mut self) -> Result<()> { Ok(()) } } +#[cfg(feature = "std")] +impl Write for std::io::BufWriter { + #[inline] + fn write(&mut self, buf: &[u8]) -> Result { 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 { Ok(buf.len()) } diff --git a/io/src/macros.rs b/io/src/macros.rs index 7ea91836..5fae2a75 100644 --- a/io/src/macros.rs +++ b/io/src/macros.rs @@ -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 { + $write_fn(self, buf) + } + #[inline] + fn flush(&mut self) -> $crate::Result<()> { + $flush_fn(self) + } + } } }