use core::fmt::Debug;
use std::io::ErrorKind;
pub trait ReadSimple {
type Error: Debug;
fn read_simple(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>;
}
#[cfg(feature = "std")]
impl<T> ReadSimple for T
where
T: std::io::Read,
{
type Error = std::io::Error;
#[inline]
fn read_simple(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let result = self.read(buf);
if let Err(e) = &result {
if e.kind() == ErrorKind::WouldBlock {
return Ok(0);
}
}
if let Ok(size) = &result {
tracing::trace!("read {:#?}", &buf[0..*size]);
}
result
}
}
pub trait WriteSimple {
type Error: Debug;
fn write_all_simple(&mut self, buf: &[u8]) -> Result<(), Self::Error>;
}
#[cfg(feature = "std")]
impl<T> WriteSimple for T
where
T: std::io::Write,
{
type Error = std::io::Error;
#[inline]
fn write_all_simple(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
tracing::trace!("writing {:#?}", buf);
self.write_all(buf)
}
}