1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
use core::fmt::Debug;
/// A simple read trait suitable for embedded devices.
pub trait ReadSimple {
/// The error type used by this reader.
type Error: Debug;
/// Read up to `buf.len()` bytes into the given buffer,
/// returning the exact number of bytes read.
///
/// The caller makes no guarantees about the contents of `buf`.
///
/// In the case of an error, the contents of `buf` are undefined.
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> {
self.read(buf)
}
}
/// A simple write trait suitable for embedded devices.
pub trait WriteSimple {
/// The error type used by this reader.
type Error: Debug;
/// Write all the bytes in `buf`.
///
/// Upon returning an error, the number of bytes written is undefined.
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> {
self.write_all(buf)
}
}