use core::future::Future;
pub use crate::blocking::ReadExactError;
type ReadExactFuture<'a, T>
where
T: Read + ?Sized + 'a,
= impl Future<Output = Result<(), ReadExactError<T::Error>>> + 'a;
pub trait Read: crate::Io {
type ReadFuture<'a>: Future<Output = Result<usize, Self::Error>>
where
Self: 'a;
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a>;
fn read_exact<'a>(&'a mut self, mut buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> {
async move {
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() {
Err(ReadExactError::UnexpectedEof)
} else {
Ok(())
}
}
}
}
pub trait BufRead: crate::Io {
type FillBufFuture<'a>: Future<Output = Result<&'a [u8], Self::Error>>
where
Self: 'a;
fn fill_buf<'a>(&'a mut self) -> Self::FillBufFuture<'a>;
fn consume(&mut self, amt: usize);
}
type WriteAllFuture<'a, T>
where
T: Write + ?Sized + 'a,
= impl Future<Output = Result<(), T::Error>> + 'a;
pub trait Write: crate::Io {
type WriteFuture<'a>: Future<Output = Result<usize, Self::Error>>
where
Self: 'a;
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a>;
type FlushFuture<'a>: Future<Output = Result<(), Self::Error>>
where
Self: 'a;
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a>;
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self> {
async move {
let mut buf = buf;
while !buf.is_empty() {
match self.write(buf).await {
Ok(0) => panic!("zero-length write."),
Ok(n) => buf = &buf[n..],
Err(e) => return Err(e),
}
}
Ok(())
}
}
}
type RewindFuture<'a, T>
where
T: Seek + ?Sized + 'a,
= impl Future<Output = Result<(), T::Error>> + 'a;
pub trait Seek: crate::Io {
type SeekFuture<'a>: Future<Output = Result<u64, Self::Error>>
where
Self: 'a;
fn seek<'a>(&'a mut self, pos: crate::SeekFrom) -> Self::SeekFuture<'a>;
fn rewind<'a>(&'a mut self) -> RewindFuture<'a, Self> {
async move {
self.seek(crate::SeekFrom::Start(0)).await?;
Ok(())
}
}
fn stream_position<'a>(&'a mut self) -> Self::SeekFuture<'a> {
self.seek(crate::SeekFrom::Current(0))
}
}
impl<T: ?Sized + Read> Read for &mut T {
type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a
where
Self: 'a;
#[inline]
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
T::read(self, buf)
}
}
impl<T: ?Sized + BufRead> BufRead for &mut T {
type FillBufFuture<'a> = impl Future<Output = Result<&'a [u8], Self::Error>>
where
Self: 'a;
fn fill_buf<'a>(&'a mut self) -> Self::FillBufFuture<'a> {
T::fill_buf(self)
}
fn consume(&mut self, amt: usize) {
T::consume(self, amt)
}
}
impl<T: ?Sized + Write> Write for &mut T {
type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a
where
Self: 'a;
#[inline]
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
T::write(self, buf)
}
type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a
where
Self: 'a;
#[inline]
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
T::flush(self)
}
}
impl<T: ?Sized + Seek> Seek for &mut T {
type SeekFuture<'a> = impl Future<Output = Result<u64, Self::Error>> + 'a
where
Self: 'a;
#[inline]
fn seek<'a>(&'a mut self, pos: crate::SeekFrom) -> Self::SeekFuture<'a> {
T::seek(self, pos)
}
}
impl Read for &[u8] {
type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a
where
Self: 'a;
#[inline]
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
async move {
let amt = core::cmp::min(buf.len(), self.len());
let (a, b) = self.split_at(amt);
if amt == 1 {
buf[0] = a[0];
} else {
buf[..amt].copy_from_slice(a);
}
*self = b;
Ok(amt)
}
}
}
impl BufRead for &[u8] {
type FillBufFuture<'a> = impl Future<Output = Result<&'a [u8], Self::Error>>
where
Self: 'a;
#[inline]
fn fill_buf<'a>(&'a mut self) -> Self::FillBufFuture<'a> {
async move { Ok(*self) }
}
#[inline]
fn consume(&mut self, amt: usize) {
*self = &self[amt..];
}
}
impl Write for &mut [u8] {
type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a
where
Self: 'a;
#[inline]
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
async move {
let amt = core::cmp::min(buf.len(), self.len());
let (a, b) = core::mem::replace(self, &mut []).split_at_mut(amt);
a.copy_from_slice(&buf[..amt]);
*self = b;
Ok(amt)
}
}
type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>>
where
Self: 'a;
#[inline]
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
async move { Ok(()) }
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + Read> Read for alloc::boxed::Box<T> {
type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a
where
Self: 'a;
#[inline]
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
T::read(self, buf)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + BufRead> BufRead for alloc::boxed::Box<T> {
type FillBufFuture<'a> = impl Future<Output = Result<&'a [u8], Self::Error>>
where
Self: 'a;
fn fill_buf<'a>(&'a mut self) -> Self::FillBufFuture<'a> {
T::fill_buf(self)
}
fn consume(&mut self, amt: usize) {
T::consume(self, amt)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + Write> Write for alloc::boxed::Box<T> {
type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a
where
Self: 'a;
#[inline]
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
T::write(self, buf)
}
type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a
where
Self: 'a;
#[inline]
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
T::flush(self)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + Seek> Seek for alloc::boxed::Box<T> {
type SeekFuture<'a> = impl Future<Output = Result<u64, Self::Error>> + 'a
where
Self: 'a;
#[inline]
fn seek<'a>(&'a mut self, pos: crate::SeekFrom) -> Self::SeekFuture<'a> {
T::seek(self, pos)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl Write for alloc::vec::Vec<u8> {
type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> + 'a
where
Self: 'a;
#[inline]
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
async move {
self.extend_from_slice(buf);
Ok(buf.len())
}
}
type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>>
where
Self: 'a;
#[inline]
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> {
async move { Ok(()) }
}
}