Skip to main content

vortex_buffer/
macros.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4/// Dispatches a byte width to a compile-time `const W: usize`.
5///
6/// The default specialized widths are 1, 2, 4, 8, 16, and 32 bytes. Pass `widths = [...]` to
7/// specialize a different set of nonzero powers of two. The input is a `usize` expression evaluated
8/// once. Any other width, including zero, reaches the required fallback without rounding.
9///
10/// The body and fallback are ordinary match arms: they can move values, return, and propagate
11/// errors from the enclosing function. Both must produce the same result type.
12///
13/// # Examples
14///
15/// ```
16/// use vortex_buffer::match_each_power_of_two_width;
17///
18/// let width = match_each_power_of_two_width!(
19///     8,
20///     |W| { Ok::<_, usize>(size_of::<[u8; W]>()) },
21///     other => { Err(other) },
22/// );
23/// assert_eq!(width, Ok(8));
24///
25/// let width = match_each_power_of_two_width!(
26///     64,
27///     widths = [16, 64],
28///     |W| { W },
29///     other => { other },
30/// );
31/// assert_eq!(width, 64);
32/// ```
33///
34/// Specializations must be nonzero powers of two:
35///
36/// ```compile_fail
37/// use vortex_buffer::match_each_power_of_two_width;
38///
39/// match_each_power_of_two_width!(3, widths = [3], |W| { W }, other => { other });
40/// ```
41#[macro_export]
42macro_rules! match_each_power_of_two_width {
43    (
44        $byte_width:expr,
45        widths = [$($width:literal),+ $(,)?],
46        |$W:ident| $body:block,
47        $other:pat => $fallback:block $(,)?
48    ) => {{
49        let byte_width: usize = $byte_width;
50        match byte_width {
51            $(
52                $width => {
53                    const $W: usize = $width;
54                    const { assert!($W.is_power_of_two(), "width must be a nonzero power of two") };
55                    $body
56                },
57            )+
58            $other => $fallback,
59        }
60    }};
61    (
62        $byte_width:expr,
63        |$W:ident| $body:block,
64        $other:pat => $fallback:block $(,)?
65    ) => {
66        $crate::match_each_power_of_two_width!(
67            $byte_width,
68            widths = [1, 2, 4, 8, 16, 32],
69            |$W| $body,
70            $other => $fallback,
71        )
72    };
73}
74
75/// A macro for constructing buffers akin to `vec![..]`.
76#[macro_export]
77macro_rules! buffer {
78    () => (
79        $crate::Buffer::empty()
80    );
81    ($start:tt .. $end:tt) => (
82        $crate::Buffer::from_iter($start..$end)
83    );
84    ($start:tt ..= $end:tt) => (
85        $crate::Buffer::from_iter($start..=$end)
86    );
87    ($elem:expr; $n:expr) => (
88        $crate::Buffer::full($elem, $n)
89    );
90    ($($x:expr),+ $(,)?) => (
91        $crate::Buffer::from_iter([$($x),+])
92    );
93}
94
95/// A macro for constructing buffers akin to `vec![..]`.
96#[macro_export]
97macro_rules! buffer_mut {
98    () => (
99        $crate::BufferMut::empty()
100    );
101    ($start:tt .. $end:tt) => (
102        $crate::BufferMut::from_iter($start..$end)
103    );
104    ($start:tt ..= $end:tt) => (
105        $crate::BufferMut::from_iter($start..=$end)
106    );
107    ($elem:expr; $n:expr) => (
108        $crate::BufferMut::full($elem, $n)
109    );
110    ($($x:expr),+ $(,)?) => (
111        $crate::BufferMut::from_iter([$($x),+])
112    );
113}