#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]
#![cfg_attr(nightly, allow(stable_features, unknown_lints))]
#![cfg_attr(nightly, feature(async_fn_in_trait, impl_trait_projections))]
#![allow(async_fn_in_trait)]
#[cfg(feature = "alloc")]
extern crate alloc;
mod impls;
pub use embedded_io::{
Error, ErrorKind, ErrorType, ReadExactError, ReadReady, SeekFrom, WriteReady,
};
pub trait Read: ErrorType {
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>;
async fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), ReadExactError<Self::Error>> {
while !buf.is_empty() {
match self.read(buf).await {
Ok(0) => break,
Ok(n) => buf = &mut buf[n..],
Err(e) => return Err(ReadExactError::Other(e)),
}
}
if buf.is_empty() {
Ok(())
} else {
Err(ReadExactError::UnexpectedEof)
}
}
}
pub trait BufRead: ErrorType {
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error>;
fn consume(&mut self, amt: usize);
}
pub trait Write: ErrorType {
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>;
async fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
let mut buf = buf;
while !buf.is_empty() {
match self.write(buf).await {
Ok(0) => panic!("write() returned Ok(0)"),
Ok(n) => buf = &buf[n..],
Err(e) => return Err(e),
}
}
Ok(())
}
}
pub trait Seek: ErrorType {
async fn seek(&mut self, pos: SeekFrom) -> Result<u64, Self::Error>;
async fn rewind(&mut self) -> Result<(), Self::Error> {
self.seek(SeekFrom::Start(0)).await?;
Ok(())
}
async fn stream_position(&mut self) -> Result<u64, Self::Error> {
self.seek(SeekFrom::Current(0)).await
}
}
impl<T: ?Sized + Read> Read for &mut T {
#[inline]
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
T::read(self, buf).await
}
}
impl<T: ?Sized + BufRead> BufRead for &mut T {
async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
T::fill_buf(self).await
}
fn consume(&mut self, amt: usize) {
T::consume(self, amt);
}
}
impl<T: ?Sized + Write> Write for &mut T {
#[inline]
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
T::write(self, buf).await
}
#[inline]
async fn flush(&mut self) -> Result<(), Self::Error> {
T::flush(self).await
}
}
impl<T: ?Sized + Seek> Seek for &mut T {
#[inline]
async fn seek(&mut self, pos: SeekFrom) -> Result<u64, Self::Error> {
T::seek(self, pos).await
}
}