aral_runtime_async_std/io/
mod.rs

1use futures_lite::io::{AsyncBufReadExt, AsyncReadExt};
2use std::{
3    future::Future,
4    io::{Result, SeekFrom},
5};
6
7pub trait Read {
8    fn read(&mut self, buf: &mut [u8]) -> impl Future<Output = Result<usize>> + Send;
9}
10
11impl<T: ?Sized + Read + Unpin + Send> Read for &mut T {
12    #[inline]
13    async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
14        Read::read(&mut **self, buf).await
15    }
16}
17
18impl<T: ?Sized + Read + Unpin + Send> Read for Box<T> {
19    #[inline]
20    async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
21        Read::read(&mut **self, buf).await
22    }
23}
24
25pub trait BufRead: Read {
26    fn fill_buf(&mut self) -> impl Future<Output = Result<&[u8]>> + Send;
27
28    fn consume(&mut self, amt: usize);
29}
30
31pub trait Write {
32    fn write(&mut self, buf: &[u8]) -> impl Future<Output = Result<usize>> + Send;
33
34    fn flush(&mut self) -> impl Future<Output = Result<()>> + Send;
35}
36
37pub trait Seek {
38    fn seek(&mut self, pos: SeekFrom) -> impl Future<Output = Result<u64>> + Send;
39}
40
41#[repr(transparent)]
42pub struct Empty(async_std::io::Empty);
43
44impl Read for Empty {
45    #[inline]
46    async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
47        self.0.read(buf).await
48    }
49}
50
51impl BufRead for Empty {
52    #[inline]
53    async fn fill_buf(&mut self) -> Result<&[u8]> {
54        self.0.fill_buf().await
55    }
56
57    #[inline]
58    fn consume(&mut self, amt: usize) {
59        self.0.consume(amt)
60    }
61}
62
63#[inline]
64pub fn empty() -> Empty {
65    Empty(async_std::io::empty())
66}