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
macro_rules! read_bools_future {
    ($future: ident, $poll_func: ident, $struct_buf: ident, $n: literal) => {
        #[cfg(feature = "async_bools")]
        #[cfg_attr(docsrs, doc(cfg(feature = "async_bools")))]
        #[derive(Debug)]
        struct $struct_buf {
            byte: Option<u8>,
        }
        #[cfg(feature = "async_bools")]
        impl $struct_buf {
            fn new() -> Self {
                Self { byte: None }
            }
            fn reset(&mut self) {
                self.byte = None;
            }
        }
        #[cfg(feature = "async_bools")]
        $crate::pin_project_lite::pin_project! {
            #[cfg_attr(docsrs, doc(cfg(feature = "async_bools")))]
            #[derive(Debug)]
            #[project(!Unpin)]
            #[must_use = "futures do nothing unless you `.await` or poll them"]
            pub struct $future<'a, R: ?Sized> {
                #[pin]
                reader: &'a mut R,
                inner: $struct_buf,
            }
        }
        #[cfg(feature = "async_bools")]
        impl<'a, R: $crate::AsyncVariableReadable + Unpin+ ?Sized> std::future::Future for $future<'a, R> {
            type Output = std::io::Result<[bool; $n]>;

            fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
                let mut me = self.project();
                R::$poll_func(std::pin::Pin::new(&mut *me.reader), cx, me.inner)
            }
        }
    };
}
macro_rules! read_bools_poll {
    ($poll_func: ident, $struct_buf: ident, $n: literal) => {
        #[cfg(feature = "async_bools")]
        #[cfg_attr(docsrs, doc(cfg(feature = "async_bools")))]
        fn $poll_func(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>, inner: &mut $struct_buf) -> std::task::Poll<std::io::Result<[bool; $n]>> {
            const MAX: u8 = ((1 << ($n - 1)) - 1 << 1) + 1; // (1 << $n) - 1
            let b = match inner.byte {
                Some(b) => b, None => {
                    let b = ready!(self.poll_read_single(cx))?;
                    inner.byte.replace(b);
                    b
                },
            };
            if b > MAX {
                return std::task::Poll::Ready(Err(std::io::Error::new(std::io::ErrorKind::InvalidData, format!("Invalid bools at {}.", stringify!($func)))));
            }
            let mut bools = [false; $n];
            for i in 0..$n {
                bools[i] = b & (1 << i) != 0;
            }
            std::task::Poll::Ready(Ok(bools))
        }
    };
}
macro_rules! read_bools_func {
    ($func: ident, $future: ident, $struct_buf: ident) => {
        #[cfg(feature = "async_bools")]
        #[cfg_attr(docsrs, doc(cfg(feature = "async_bools")))]
        #[inline]
        fn $func(&mut self) -> $future<Self> where Self: Unpin {
            $future { reader: self, inner: $struct_buf::new() }
        }
    };
}
macro_rules! define_read_bools_futures {
    () => {
        read_bools_future!(ReadBools2, poll_read_bools_2, InternalReadBools2, 2);
        read_bools_future!(ReadBools3, poll_read_bools_3, InternalReadBools3, 3);
        read_bools_future!(ReadBools4, poll_read_bools_4, InternalReadBools4, 4);
        read_bools_future!(ReadBools5, poll_read_bools_5, InternalReadBools5, 5);
        read_bools_future!(ReadBools6, poll_read_bools_6, InternalReadBools6, 6);
        read_bools_future!(ReadBools7, poll_read_bools_7, InternalReadBools7, 7);
        read_bools_future!(ReadBools8, poll_read_bools_8, InternalReadBools8, 8);
    };
}
macro_rules! define_read_bools_poll {
    () => {
        read_bools_poll!(poll_read_bools_2, InternalReadBools2, 2);
        read_bools_poll!(poll_read_bools_3, InternalReadBools3, 3);
        read_bools_poll!(poll_read_bools_4, InternalReadBools4, 4);
        read_bools_poll!(poll_read_bools_5, InternalReadBools5, 5);
        read_bools_poll!(poll_read_bools_6, InternalReadBools6, 6);
        read_bools_poll!(poll_read_bools_7, InternalReadBools7, 7);
        read_bools_poll!(poll_read_bools_8, InternalReadBools8, 8);
    };
}
macro_rules! define_read_bools_func {
    () => {
        read_bools_func!(read_bools_2, ReadBools2, InternalReadBools2);
        read_bools_func!(read_bools_3, ReadBools3, InternalReadBools3);
        read_bools_func!(read_bools_4, ReadBools4, InternalReadBools4);
        read_bools_func!(read_bools_5, ReadBools5, InternalReadBools5);
        read_bools_func!(read_bools_6, ReadBools6, InternalReadBools6);
        read_bools_func!(read_bools_7, ReadBools7, InternalReadBools7);
        read_bools_func!(read_bools_8, ReadBools8, InternalReadBools8);
    };
}
define_read_bools_futures!();