commonlibsse_ng\macros/array.rs
1/// A macro to construct an `hkArrayBase` similar to the `vec![]` macro.
2///
3/// # Examples
4///
5/// Create an array with values:
6/// ```no_run
7/// # use commonlibsse_ng::hk_array;
8/// let arr = hk_array![1, 2, 3];
9/// ```
10///
11/// Create an empty array:
12/// ```no_run
13/// # use commonlibsse_ng::hk_array;
14/// use commonlibsse_ng::re::hkArray::hkArray;
15/// let empty: hkArray<i8> = hk_array![];
16/// ```
17///
18/// Create an array with a custom allocator:
19/// ```
20/// # use commonlibsse_ng::hk_array;
21/// use stdx::alloc::Global;
22/// let arr = hk_array![with_allocator: Global => "a", "b", "c"];
23/// ```
24#[macro_export]
25macro_rules! hk_array {
26 // Empty hkArray with default allocator
27 () => {
28 $crate::re::hkArray::hkArray::new()
29 };
30
31 ($value:expr; $count:expr) => {{
32 let mut arr: $crate::re::hkArray::hkArray<_> = $crate::re::hkArray::hkArray::with_capacity($count);
33 for _ in 0..$count {
34 arr.push($value);
35 }
36 arr
37 }};
38
39 // hkArray with elements, using default allocator
40 ($($elem:expr),+ $(,)?) => {{
41 let mut arr: $crate::re::hkArray::hkArray<_> = $crate::re::hkArray::hkArray::with_capacity(0);
42 $(
43 arr.push($elem);
44 )+
45 arr
46 }};
47
48 (with_allocator: $alloc:ty => $value:expr; $count:expr) => {{
49 let mut arr = $crate::re::hkArray::hkArray::<_, $alloc>::with_capacity($count);
50 for _ in 0..$count {
51 arr.push($value);
52 }
53 arr
54 }};
55
56 // hkArray with elements and custom allocator
57 (with_allocator: $alloc:ty => $($elem:expr),+ $(,)?) => {{
58 let mut arr = $crate::re::hkArray::hkArray::<_, $alloc>::with_capacity(0);
59 $(
60 arr.push($elem);
61 )+
62 arr
63 }};
64}
65
66/// Creates a `BSTArray` with syntax similar to the standard `vec!` macro.
67///
68/// # Examples
69///
70/// Create an array from a list of values:
71/// ```no_run
72/// # use commonlibsse_ng::bst_array;
73/// let arr = bst_array![1_u32, 2, 3];
74/// assert_eq!(arr.len(), 3);
75/// ```
76///
77/// Create an array with repeated values:
78/// ```no_run
79/// # use commonlibsse_ng::bst_array;
80/// let arr = bst_array![42_u32; 5];
81/// assert_eq!(arr.len(), 5);
82/// ```
83///
84/// Create an empty array:
85/// ```no_run
86/// # use commonlibsse_ng::bst_array;
87/// use commonlibsse_ng::re::BSTArray::BSTArray;
88/// let arr: BSTArray<i32> = bst_array![];
89/// assert!(arr.is_empty());
90/// ```
91///
92/// Create an array with a custom allocator:
93/// ```
94/// # use commonlibsse_ng::bst_array;
95/// use stdx::alloc::Global;
96/// let arr = bst_array![with_allocator: Global => 10, 20];
97/// ```
98///
99/// Create a repeated array with a custom allocator:
100/// ```
101/// # use commonlibsse_ng::bst_array;
102/// use stdx::alloc::Global;
103/// let arr = bst_array![with_allocator: Global => 7; 3];
104/// ```
105///
106/// # Note
107///
108/// This macro uses `.with_capacity()` and `.push()` under the hood.
109/// It does **not** call `resize()` or pre-initialize memory.
110///
111/// The repeated variant (`[value; count]`) requires that `value` is `Clone`,
112/// but cloning is done by the caller expression (i.e., `push(value)`).
113#[macro_export]
114macro_rules! bst_array {
115 () => {
116 $crate::re::BSTArray::BSTArray::new()
117 };
118
119 ($value:expr; $count:expr) => {{
120 let mut arr: $crate::re::BSTArray::BSTArray<_> = $crate::re::BSTArray::BSTArray::with_capacity($count);
121 for _ in 0..$count {
122 arr.push($value);
123 }
124 arr
125 }};
126
127 ($($elem:expr),+ $(,)?) => {{
128 let _count = <[()]>::len(&[$($crate::bst_array![@count $elem]),+]);
129 let mut arr: $crate::re::BSTArray::BSTArray<_> = $crate::re::BSTArray::BSTArray::with_capacity(_count);
130 $(
131 arr.push($elem);
132 )+
133 arr
134 }};
135
136 (with_allocator: $alloc:ty => $value:expr; $count:expr) => {{
137 let mut arr = $crate::re::BSTArray::BSTArray::<_, $alloc>::with_capacity($count);
138 for _ in 0..$count {
139 arr.push($value);
140 }
141 arr
142 }};
143
144 (with_allocator: $alloc:ty => $($elem:expr),+ $(,)?) => {{
145 let _count = <[()]>::len(&[$($crate::bst_array![@count $elem]),+]);
146 let mut arr = $crate::re::BSTArray::BSTArray::<_, $alloc>::with_capacity(_count);
147 $(
148 arr.push($elem);
149 )+
150 arr
151 }};
152
153 (@count $x:expr) => { () };
154}