compressed_intvec/fixed/atomic/macros.rs
1//! # Macros for [`AtomicFixedVec`]
2/// Creates an [`AtomicFixedVec`] with default parameters.
3///
4/// This macro simplifies the creation of an [`AtomicFixedVec`]. It uses a [`u64`]
5/// storage backend and [`BitWidth::Minimal`](crate::fixed::BitWidth::Minimal) for space efficiency.
6///
7/// There are two forms of this macro:
8///
9/// - Create a vector from a list of elements:
10/// ```
11/// # use compressed_intvec::atomic_fixed_vec;
12/// let vec = atomic_fixed_vec![10u32, 20, 30];
13/// # assert_eq!(vec.len(), 3);
14/// ```
15///
16/// - Create a vector from a repeated element:
17/// ```
18/// # use compressed_intvec::atomic_fixed_vec;
19/// let vec = atomic_fixed_vec![0i16; 100];
20/// # assert_eq!(vec.len(), 100);
21/// ```
22///
23/// # Examples
24///
25/// ```
26/// use compressed_intvec::prelude::*;
27/// use compressed_intvec::atomic_fixed_vec;
28/// use compressed_intvec::fixed::UAtomicFixedVec;
29///
30/// // Create a vector from a list of elements. The type is inferred.
31/// let vec = atomic_fixed_vec![10u32, 20, 30];
32/// assert_eq!(vec.len(), 3);
33/// assert_eq!(vec.bit_width(), 5); // 30 requires 5 bits
34///
35/// // Create a vector from a repeated element.
36/// let vec_rep = atomic_fixed_vec![0i16; 100];
37/// assert_eq!(vec_rep.len(), 100);
38/// assert_eq!(vec_rep.get(50), Some(0));
39///
40/// // Create an empty vector (type annotation is required).
41/// let empty: UAtomicFixedVec<u64> = atomic_fixed_vec![];
42/// assert!(empty.is_empty());
43/// ```
44#[macro_export]
45macro_rules! atomic_fixed_vec {
46 // Empty vector: `atomic_fixed_vec![]`
47 // Requires type annotation from the user.
48 () => {
49 $crate::fixed::atomic::AtomicFixedVec::builder().build(&[]).unwrap()
50 };
51
52 // From list: `atomic_fixed_vec![a, b, c]`
53 ($($elem:expr),+ $(,)?) => {
54 // Delegate to a helper function to avoid repeating complex bounds.
55 $crate::fixed::atomic::macros::from_slice(&[$($elem),+])
56 };
57
58 // From element and length: `atomic_fixed_vec![elem; len]`
59 ($elem:expr; $len:expr) => {
60 // Delegate to a helper function.
61 $crate::fixed::atomic::macros::from_repetition($elem, $len)
62 };
63}
64
65// --- Macro Helper Functions (Not part of the public API) ---
66
67use crate::fixed::atomic::{builder::AtomicFixedVecBuilder, AtomicFixedVec};
68use crate::fixed::traits::Storable;
69use num_traits::ToPrimitive;
70
71/// A hidden helper function for the `atomic_fixed_vec![...]` macro variant.
72///
73/// This function is not intended for direct use. It is called by the macro
74/// to construct an `AtomicFixedVec` from a slice of elements.
75#[doc(hidden)]
76pub fn from_slice<T>(slice: &[T]) -> AtomicFixedVec<T>
77where
78 T: Storable<u64> + ToPrimitive + Copy,
79{
80 // The builder defaults to `BitWidth::Minimal`.
81 AtomicFixedVecBuilder::new().build(slice).unwrap()
82}
83
84/// A hidden helper function for the `atomic_fixed_vec![elem; len]` macro variant.
85///
86/// This function is not intended for direct use. It is called by the macro
87/// to construct an `AtomicFixedVec` by repeating an element.
88#[doc(hidden)]
89pub fn from_repetition<T>(elem: T, len: usize) -> AtomicFixedVec<T>
90where
91 T: Storable<u64> + ToPrimitive + Copy,
92{
93 let v = vec![elem; len];
94 AtomicFixedVecBuilder::new().build(&v).unwrap()
95}