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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::future::Future;

use super::AsyncReadRent;
use crate::{
    buf::{IoBufMut, IoVecBufMut, SliceMut},
    BufResult,
};

macro_rules! reader_trait {
    ($future: ident, $n_ty: ty, $f: ident) => {
        /// Read number in async way
        fn $f(&mut self) -> impl Future<Output = std::io::Result<$n_ty>>;
    };
}

macro_rules! reader_be_impl {
    ($future: ident, $n_ty: ty, $f: ident) => {
        async fn $f(&mut self) -> std::io::Result<$n_ty> {
            let (res, buf) = self
                .read_exact(std::boxed::Box::new([0; std::mem::size_of::<$n_ty>()]))
                .await;
            res?;
            use crate::utils::box_into_inner::IntoInner;
            Ok(<$n_ty>::from_be_bytes(Box::consume(buf)))
        }
    };
}

macro_rules! reader_le_impl {
    ($future: ident, $n_ty: ty, $f: ident) => {
        async fn $f(&mut self) -> std::io::Result<$n_ty> {
            let (res, buf) = self
                .read_exact(std::boxed::Box::new([0; std::mem::size_of::<$n_ty>()]))
                .await;
            res?;
            use crate::utils::box_into_inner::IntoInner;
            Ok(<$n_ty>::from_le_bytes(Box::consume(buf)))
        }
    };
}

/// AsyncReadRentExt
pub trait AsyncReadRentExt {
    /// Read until buf capacity is fulfilled
    fn read_exact<T: IoBufMut + 'static>(
        &mut self,
        buf: T,
    ) -> impl Future<Output = BufResult<usize, T>>;

    /// Readv until buf capacity is fulfilled
    fn read_vectored_exact<T: IoVecBufMut + 'static>(
        &mut self,
        buf: T,
    ) -> impl Future<Output = BufResult<usize, T>>;

    reader_trait!(ReadU8Future, u8, read_u8);
    reader_trait!(ReadU16Future, u16, read_u16);
    reader_trait!(ReadU32Future, u32, read_u32);
    reader_trait!(ReadU64Future, u64, read_u64);
    reader_trait!(ReadU128Future, u16, read_u128);
    reader_trait!(ReadI8Future, i8, read_i8);
    reader_trait!(ReadI16Future, i16, read_i16);
    reader_trait!(ReadI32Future, i32, read_i32);
    reader_trait!(ReadI64Future, i64, read_i64);
    reader_trait!(ReadI128Future, i128, read_i128);
    reader_trait!(ReadF32Future, f32, read_f32);
    reader_trait!(ReadF64Future, f64, read_f64);

    reader_trait!(ReadU8LEFuture, u8, read_u8_le);
    reader_trait!(ReadU16LEFuture, u16, read_u16_le);
    reader_trait!(ReadU32LEFuture, u32, read_u32_le);
    reader_trait!(ReadU64LEFuture, u64, read_u64_le);
    reader_trait!(ReadU128LEFuture, u16, read_u128_le);
    reader_trait!(ReadI8LEFuture, i8, read_i8_le);
    reader_trait!(ReadI16LEFuture, i16, read_i16_le);
    reader_trait!(ReadI32LEFuture, i32, read_i32_le);
    reader_trait!(ReadI64LEFuture, i64, read_i64_le);
    reader_trait!(ReadI128LEFuture, i128, read_i128_le);
    reader_trait!(ReadF32LEFuture, f32, read_f32_le);
    reader_trait!(ReadF64LEFuture, f64, read_f64_le);
}

impl<A> AsyncReadRentExt for A
where
    A: AsyncReadRent + ?Sized,
{
    async fn read_exact<T: IoBufMut + 'static>(&mut self, mut buf: T) -> BufResult<usize, T> {
        let len = buf.bytes_total();
        let mut read = 0;
        while read < len {
            let buf_slice = unsafe { SliceMut::new_unchecked(buf, read, len) };
            let (result, buf_slice) = self.read(buf_slice).await;
            buf = buf_slice.into_inner();
            match result {
                Ok(0) => {
                    return (
                        Err(std::io::Error::new(
                            std::io::ErrorKind::UnexpectedEof,
                            "failed to fill whole buffer",
                        )),
                        buf,
                    )
                }
                Ok(n) => {
                    read += n;
                    unsafe { buf.set_init(read) };
                }
                Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => {}
                Err(e) => return (Err(e), buf),
            }
        }
        (Ok(read), buf)
    }

    async fn read_vectored_exact<T: IoVecBufMut + 'static>(
        &mut self,
        mut buf: T,
    ) -> BufResult<usize, T> {
        let mut meta = crate::buf::write_vec_meta(&mut buf);
        let len = meta.len();
        let mut read = 0;

        while read < len {
            let (res, meta_) = self.readv(meta).await;
            meta = meta_;
            match res {
                Ok(0) => {
                    return (
                        Err(std::io::Error::new(
                            std::io::ErrorKind::UnexpectedEof,
                            "failed to fill whole buffer",
                        )),
                        buf,
                    )
                }
                Ok(n) => read += n,
                Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => {}
                Err(e) => return (Err(e), buf),
            }
        }
        (Ok(read), buf)
    }

    reader_be_impl!(ReadU8Future, u8, read_u8);
    reader_be_impl!(ReadU16Future, u16, read_u16);
    reader_be_impl!(ReadU32Future, u32, read_u32);
    reader_be_impl!(ReadU64Future, u64, read_u64);
    reader_be_impl!(ReadU128Future, u16, read_u128);
    reader_be_impl!(ReadI8Future, i8, read_i8);
    reader_be_impl!(ReadI16Future, i16, read_i16);
    reader_be_impl!(ReadI32Future, i32, read_i32);
    reader_be_impl!(ReadI64Future, i64, read_i64);
    reader_be_impl!(ReadI128Future, i128, read_i128);
    reader_be_impl!(ReadF32Future, f32, read_f32);
    reader_be_impl!(ReadF64Future, f64, read_f64);

    reader_le_impl!(ReadU8LEFuture, u8, read_u8_le);
    reader_le_impl!(ReadU16LEFuture, u16, read_u16_le);
    reader_le_impl!(ReadU32LEFuture, u32, read_u32_le);
    reader_le_impl!(ReadU64LEFuture, u64, read_u64_le);
    reader_le_impl!(ReadU128LEFuture, u16, read_u128_le);
    reader_le_impl!(ReadI8LEFuture, i8, read_i8_le);
    reader_le_impl!(ReadI16LEFuture, i16, read_i16_le);
    reader_le_impl!(ReadI32LEFuture, i32, read_i32_le);
    reader_le_impl!(ReadI64LEFuture, i64, read_i64_le);
    reader_le_impl!(ReadI128LEFuture, i128, read_i128_le);
    reader_be_impl!(ReadF32LEFuture, f32, read_f32_le);
    reader_be_impl!(ReadF64LEFuture, f64, read_f64_le);
}