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
#[cfg(feature = "async_bools")]
#[cfg_attr(docsrs, doc(cfg(feature = "async_bools")))]
macro_rules! read_bools_future {
    ($future: ident, $n: literal) => {
        $crate::pin_project_lite::pin_project! {
            #[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,
            }
        }
        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> {
                const MAX: u8 = ((1 << ($n - 1)) - 1 << 1) + 1; // (1 << $n) - 1
                let mut me = self.project();
                let b = ready!(R::poll_read_single(Pin::new(&mut *me.reader), cx))?;
                if b > MAX {
                    return Poll::Ready(Err(Error::new(ErrorKind::InvalidData, format!("Invalid bools at {}.", stringify!($func)))));
                }
                let mut bools = [false; $n];
                for i in 0..$n {
                    bools[i] = b & (1 << i) != 0;
                }
                Poll::Ready(Ok(bools))
            }
        }
    };
}
#[cfg(feature = "async_bools")]
#[cfg_attr(docsrs, doc(cfg(feature = "async_bools")))]
macro_rules! read_bools_func {
    ($func: ident, $future: ident) => {
        #[inline]
        fn $func(&mut self) -> $future<Self> where Self: Unpin {
            $future { reader: self }
        }
    };
}
#[cfg(feature = "async_bools")]
#[cfg_attr(docsrs, doc(cfg(feature = "async_bools")))]
macro_rules! define_read_bools_futures {
    () => {
        read_bools_future!(ReadBools2, 2);
        read_bools_future!(ReadBools3, 3);
        read_bools_future!(ReadBools4, 4);
        read_bools_future!(ReadBools5, 5);
        read_bools_future!(ReadBools6, 6);
        read_bools_future!(ReadBools7, 7);
        read_bools_future!(ReadBools8, 8);
    };
}
#[cfg(feature = "async_bools")]
#[cfg_attr(docsrs, doc(cfg(feature = "async_bools")))]
macro_rules! define_read_bools_func {
    () => {
        read_bools_func!(read_bools_2, ReadBools2);
        read_bools_func!(read_bools_3, ReadBools3);
        read_bools_func!(read_bools_4, ReadBools4);
        read_bools_func!(read_bools_5, ReadBools5);
        read_bools_func!(read_bools_6, ReadBools6);
        read_bools_func!(read_bools_7, ReadBools7);
        read_bools_func!(read_bools_8, ReadBools8);
    };
}
#[cfg(feature = "async_bools")]
#[cfg_attr(docsrs, doc(cfg(feature = "async_bools")))]
define_read_bools_futures!();