monoio/io/
async_read_rent_ext.rs

1use std::future::Future;
2
3use super::AsyncReadRent;
4use crate::{
5    buf::{IoBufMut, IoVecBufMut, SliceMut},
6    BufResult,
7};
8
9macro_rules! reader_trait {
10    ($future: ident, $n_ty: ty, $f: ident) => {
11        /// Read number in async way
12        fn $f(&mut self) -> impl Future<Output = std::io::Result<$n_ty>>;
13    };
14}
15
16macro_rules! reader_be_impl {
17    ($future: ident, $n_ty: ty, $f: ident) => {
18        async fn $f(&mut self) -> std::io::Result<$n_ty> {
19            let (res, buf) = self
20                .read_exact(std::boxed::Box::new([0; std::mem::size_of::<$n_ty>()]))
21                .await;
22            res?;
23            use crate::utils::box_into_inner::IntoInner;
24            Ok(<$n_ty>::from_be_bytes(Box::consume(buf)))
25        }
26    };
27}
28
29macro_rules! reader_le_impl {
30    ($future: ident, $n_ty: ty, $f: ident) => {
31        async fn $f(&mut self) -> std::io::Result<$n_ty> {
32            let (res, buf) = self
33                .read_exact(std::boxed::Box::new([0; std::mem::size_of::<$n_ty>()]))
34                .await;
35            res?;
36            use crate::utils::box_into_inner::IntoInner;
37            Ok(<$n_ty>::from_le_bytes(Box::consume(buf)))
38        }
39    };
40}
41
42/// AsyncReadRentExt
43pub trait AsyncReadRentExt {
44    /// Read until buf capacity is fulfilled
45    fn read_exact<T: IoBufMut + 'static>(
46        &mut self,
47        buf: T,
48    ) -> impl Future<Output = BufResult<usize, T>>;
49
50    /// Readv until buf capacity is fulfilled
51    fn read_vectored_exact<T: IoVecBufMut + 'static>(
52        &mut self,
53        buf: T,
54    ) -> impl Future<Output = BufResult<usize, T>>;
55
56    reader_trait!(ReadU8Future, u8, read_u8);
57    reader_trait!(ReadU16Future, u16, read_u16);
58    reader_trait!(ReadU32Future, u32, read_u32);
59    reader_trait!(ReadU64Future, u64, read_u64);
60    reader_trait!(ReadU128Future, u16, read_u128);
61    reader_trait!(ReadI8Future, i8, read_i8);
62    reader_trait!(ReadI16Future, i16, read_i16);
63    reader_trait!(ReadI32Future, i32, read_i32);
64    reader_trait!(ReadI64Future, i64, read_i64);
65    reader_trait!(ReadI128Future, i128, read_i128);
66    reader_trait!(ReadF32Future, f32, read_f32);
67    reader_trait!(ReadF64Future, f64, read_f64);
68
69    reader_trait!(ReadU8LEFuture, u8, read_u8_le);
70    reader_trait!(ReadU16LEFuture, u16, read_u16_le);
71    reader_trait!(ReadU32LEFuture, u32, read_u32_le);
72    reader_trait!(ReadU64LEFuture, u64, read_u64_le);
73    reader_trait!(ReadU128LEFuture, u128, read_u128_le);
74    reader_trait!(ReadI8LEFuture, i8, read_i8_le);
75    reader_trait!(ReadI16LEFuture, i16, read_i16_le);
76    reader_trait!(ReadI32LEFuture, i32, read_i32_le);
77    reader_trait!(ReadI64LEFuture, i64, read_i64_le);
78    reader_trait!(ReadI128LEFuture, i128, read_i128_le);
79    reader_trait!(ReadF32LEFuture, f32, read_f32_le);
80    reader_trait!(ReadF64LEFuture, f64, read_f64_le);
81}
82
83impl<A> AsyncReadRentExt for A
84where
85    A: AsyncReadRent + ?Sized,
86{
87    async fn read_exact<T: IoBufMut + 'static>(&mut self, mut buf: T) -> BufResult<usize, T> {
88        let len = buf.bytes_total();
89        let mut read = 0;
90        while read < len {
91            let buf_slice = unsafe { SliceMut::new_unchecked(buf, read, len) };
92            let (result, buf_slice) = self.read(buf_slice).await;
93            buf = buf_slice.into_inner();
94            match result {
95                Ok(0) => {
96                    return (
97                        Err(std::io::Error::new(
98                            std::io::ErrorKind::UnexpectedEof,
99                            "failed to fill whole buffer",
100                        )),
101                        buf,
102                    )
103                }
104                Ok(n) => {
105                    read += n;
106                    unsafe { buf.set_init(read) };
107                }
108                Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => {}
109                Err(e) => return (Err(e), buf),
110            }
111        }
112        (Ok(read), buf)
113    }
114
115    async fn read_vectored_exact<T: IoVecBufMut + 'static>(
116        &mut self,
117        mut buf: T,
118    ) -> BufResult<usize, T> {
119        let mut meta = crate::buf::write_vec_meta(&mut buf);
120        let len = meta.len();
121        let mut read = 0;
122
123        while read < len {
124            let (res, meta_) = self.readv(meta).await;
125            meta = meta_;
126            match res {
127                Ok(0) => {
128                    return (
129                        Err(std::io::Error::new(
130                            std::io::ErrorKind::UnexpectedEof,
131                            "failed to fill whole buffer",
132                        )),
133                        buf,
134                    )
135                }
136                Ok(n) => read += n,
137                Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => {}
138                Err(e) => return (Err(e), buf),
139            }
140        }
141        (Ok(read), buf)
142    }
143
144    reader_be_impl!(ReadU8Future, u8, read_u8);
145    reader_be_impl!(ReadU16Future, u16, read_u16);
146    reader_be_impl!(ReadU32Future, u32, read_u32);
147    reader_be_impl!(ReadU64Future, u64, read_u64);
148    reader_be_impl!(ReadU128Future, u16, read_u128);
149    reader_be_impl!(ReadI8Future, i8, read_i8);
150    reader_be_impl!(ReadI16Future, i16, read_i16);
151    reader_be_impl!(ReadI32Future, i32, read_i32);
152    reader_be_impl!(ReadI64Future, i64, read_i64);
153    reader_be_impl!(ReadI128Future, i128, read_i128);
154    reader_be_impl!(ReadF32Future, f32, read_f32);
155    reader_be_impl!(ReadF64Future, f64, read_f64);
156
157    reader_le_impl!(ReadU8LEFuture, u8, read_u8_le);
158    reader_le_impl!(ReadU16LEFuture, u16, read_u16_le);
159    reader_le_impl!(ReadU32LEFuture, u32, read_u32_le);
160    reader_le_impl!(ReadU64LEFuture, u64, read_u64_le);
161    reader_le_impl!(ReadU128LEFuture, u128, read_u128_le);
162    reader_le_impl!(ReadI8LEFuture, i8, read_i8_le);
163    reader_le_impl!(ReadI16LEFuture, i16, read_i16_le);
164    reader_le_impl!(ReadI32LEFuture, i32, read_i32_le);
165    reader_le_impl!(ReadI64LEFuture, i64, read_i64_le);
166    reader_le_impl!(ReadI128LEFuture, i128, read_i128_le);
167    reader_be_impl!(ReadF32LEFuture, f32, read_f32_le);
168    reader_be_impl!(ReadF64LEFuture, f64, read_f64_le);
169}