vortex_buffer/bit/macros.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4/// A macro for constructing bit-buffers akin to `vec![..]`.
5///
6/// Supports multiple syntaxes:
7/// - `bitbuffer![]` - empty buffer
8/// - `bitbuffer![value; count]` - fill with value
9/// - `bitbuffer![expr, expr, ...]` - comma-separated boolean expressions
10/// - `bitbuffer![0 1 0 1]` - space-separated bit literals (0s and 1s)
11#[macro_export]
12macro_rules! bitbuffer {
13 // Internal rule to convert a single bit (0 or 1) to bool
14 (@bit 0) => { false };
15 (@bit 1) => { true };
16
17 () => (
18 $crate::BitBuffer::empty()
19 );
20
21 // We capture single-element 0/1 cases to avoid ambiguity with the
22 // comma-separated expression case.
23 (0) => {
24 $crate::BitBuffer::from_iter([false])
25 };
26 (1) => {
27 $crate::BitBuffer::from_iter([true])
28 };
29
30 ($elem:expr; $n:expr) => (
31 $crate::BitBuffer::full($elem, $n)
32 );
33 ($($x:expr),+ $(,)?) => (
34 $crate::BitBuffer::from_iter([$($x),+])
35 );
36 ($($bit:tt)+) => {
37 $crate::BitBuffer::from_iter([$( $crate::bitbuffer!(@bit $bit) ),+])
38 };
39}
40
41/// A macro for constructing bit-buffers akin to `vec![..]`.
42///
43/// Supports multiple syntaxes:
44/// - `bitbuffer_mut![]` - empty buffer
45/// - `bitbuffer_mut![value; count]` - fill with value
46/// - `bitbuffer_mut![expr, expr, ...]` - comma-separated boolean expressions
47/// - `bitbuffer_mut![0 1 0 1]` - space-separated bit literals (0s and 1s)
48#[macro_export]
49macro_rules! bitbuffer_mut {
50 // Internal rule to convert a single bit (0 or 1) to bool
51 (@bit 0) => { false };
52 (@bit 1) => { true };
53
54 () => (
55 $crate::BitBufferMut::empty()
56 );
57
58 // We capture single-element 0/1 cases to avoid ambiguity with the
59 // comma-separated expression case.
60 (0) => {
61 $crate::BitBuffer::from_iter([false])
62 };
63 (1) => {
64 $crate::BitBuffer::from_iter([true])
65 };
66
67 ($elem:expr; $n:expr) => (
68 $crate::BitBufferMut::full($elem, $n)
69 );
70 ($($x:expr),+ $(,)?) => (
71 $crate::BitBufferMut::from_iter([$($x),+])
72 );
73 ($($bit:tt)+) => {
74 $crate::BitBufferMut::from_iter([$( $crate::bitbuffer_mut!(@bit $bit) ),+])
75 };
76}