variable_len_reader/asynchronous/
read_bools.rs

1macro_rules! read_bools_future {
2    ($future: ident, $n: literal) => {
3        read_bools_future!(f cfg(feature = "async_bools"), $future, $n);
4    };
5    (f $feature: meta, $future: ident, $n: literal) => {
6        read_wrap_future!(f $feature, $future, ReadSingle);
7        #[$feature]
8        impl<'a, R: AsyncVariableReader + Unpin + ?Sized> Future for $future<'a, R> {
9            type Output = ::core::result::Result<[bool; $n], R::Error>;
10
11            fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
12                self.project().inner.poll(cx).map(|r| r.and_then(|b| {
13                    const MAX: u8 = ((1 << ($n - 1)) - 1 << 1) + 1; // (1 << $n) - 1
14                    if b > MAX {
15                        return Err(R::read_bools_error(stringify!($future), b));
16                    }
17                    let mut bools = [false; $n];
18                    for i in 0..$n {
19                        bools[i] = b & (1 << i) != 0;
20                    }
21                    Ok(bools)
22                }))
23            }
24        }
25    };
26}
27macro_rules! read_bools_func {
28    ($future: ident, $func: ident) => {
29        read_bools_func!(f cfg(feature = "async_bools"), $future, $func);
30    };
31    (f $feature: meta, $future: ident, $func: ident) => {
32        read_wrap_func!(f $feature, $future, $func, read_single);
33    };
34}
35
36macro_rules! define_read_bools_future {
37    () => {
38        read_bools_future!(ReadBools2, 2);
39        read_bools_future!(ReadBools3, 3);
40        read_bools_future!(ReadBools4, 4);
41        read_bools_future!(ReadBools5, 5);
42        read_bools_future!(ReadBools6, 6);
43        read_bools_future!(ReadBools7, 7);
44        read_bools_future!(ReadBools8, 8);
45    };
46}
47macro_rules! define_read_bools_func {
48    () => {
49        #[cfg(feature = "async_bools")]
50        #[cfg_attr(docsrs, doc(cfg(feature = "async_bools")))]
51        fn read_bools_error(future_name: &'static str, byte: u8) -> Self::Error;
52
53        read_bools_func!(ReadBools2, read_bools_2);
54        read_bools_func!(ReadBools3, read_bools_3);
55        read_bools_func!(ReadBools4, read_bools_4);
56        read_bools_func!(ReadBools5, read_bools_5);
57        read_bools_func!(ReadBools6, read_bools_6);
58        read_bools_func!(ReadBools7, read_bools_7);
59        read_bools_func!(ReadBools8, read_bools_8);
60    };
61}
62
63define_read_bools_future!();