1#![no_std]
2
3#[macro_export]
103#[rustfmt::skip]
104macro_rules! const_arr {
105 ([$TYPE:ty; $SIZE:literal], $func_name:ident) => {
106 {
107 const TEMP_ITEM: $TYPE = $func_name(0);
114 let mut arr: [$TYPE; $SIZE] = [TEMP_ITEM; $SIZE];
115
116 let mut ind = 0;
118 while ind < $SIZE {
119 arr[ind] = $func_name(ind);
120 ind += 1;
121 }
122 arr
123 }
124 };
125 ([$TYPE:ty; $SIZE:literal], |$name:ident| $body:expr) => {
126 {
127 #[allow(non_upper_case_globals)]
134 let mut arr: [$TYPE; $SIZE] = {
135 const $name: usize = 0;
136 const TEMP_ITEM: $TYPE = $body;
137 [TEMP_ITEM; $SIZE]
138 };
139
140 let mut $name = 0;
142 while $name < $SIZE {
143 arr[$name] = $body;
144 $name += 1;
145 }
146 arr
147 }
148 };
149 ([$TYPE:ty; $SIZE:literal], |_| $body:expr ) => {
150 {
151 const TEMP_ITEM: $TYPE = $body;
152 [TEMP_ITEM; $SIZE]
153 }
154 };
155 () => {compile_error!("Please specify array type TYPE: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
156 ([$type:ty; $size:literal]) => {compile_error!("Please specify init function INIT_FN: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
157 ([$type:ty; $size:literal], ) => {compile_error!("Please specify init function INIT_FN: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
158 ([$type:ty; $size:literal], ||) => {compile_error!("Init function has wrong format. It should be |i| i: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
159 ([$type:ty; $size:literal], || $_wha:tt) => {compile_error!("Init function has wrong format. It should be |i| i: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
160 ([$type:ty; $size:literal], $num:literal) => {compile_error!("Please add |_| to last argument to turn it to closure: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
161 ($type:ty) => {compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
162 ($type:ty, ) => {compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
163 ($type:ty,$size:literal) => {compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
164 ($type:ty,$size:literal, $_:tt) => {compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
165 ($type:ty,$size:literal, |$_:tt| $_n2:tt) => {compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n const ARR: [TYPE; SIZE] = const_arr!([TYPE; SIZE], INIT_FN);\n e.g. const ARR: [i32; 10 ] = const_arr!([i32; 10 ], |i| i as i32);"); };
166}
167
168#[macro_export]
199#[rustfmt::skip]
200macro_rules! make_const_arr {
201 ($NAME:ident, [$TYPE:ty; $SIZE:literal], $func_name:ident ) => {
202 const $NAME: [$TYPE; $SIZE] = {
203 const TEMP_ITEM: $TYPE = $func_name(0);
210 let mut arr: [$TYPE; $SIZE] = [TEMP_ITEM; $SIZE];
211
212 let mut ind = 0;
214 while ind < $SIZE {
215 arr[ind] = $func_name(ind);
216 ind += 1;
217 }
218 arr
219 }
220 ;
221 };
222 ($NAME:ident, [$TYPE:ty; $SIZE:literal], |$name:ident| $body:expr ) => {
223 const $NAME: [$TYPE; $SIZE] = {
224 #[allow(non_upper_case_globals)]
231 let mut arr: [$TYPE; $SIZE] = {
232 const $name: usize = 0;
233 const TEMP_ITEM: $TYPE = $body;
234 [TEMP_ITEM; $SIZE]
235 };
236
237 let mut $name = 0;
239 while $name < $SIZE {
240 arr[$name] = $body;
241 $name += 1;
242 }
243 arr
244 };
245 };
246 ($NAME:ident, [$TYPE:ty; $SIZE:literal], |_| $body:expr ) => {
247 const $NAME: [$TYPE; $SIZE] = {
248 const TEMP_ITEM: $TYPE = $body;
249 [TEMP_ITEM; $SIZE]
250 };
251 };
252 () => { compile_error!("Please specify array name ARR_NAME: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
253 ($_:literal) => { compile_error!("Please specify array name ARR_NAME: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
254 ($NAME:ident) => { compile_error!("Please specify array type TYPE: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
255 ($NAME:ident, ) => { make_const_arr!($NAME); };
256 ($NAME:ident, [$type:ty]) => { compile_error!("Please add SIZE to array type: It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
257 ($NAME:ident, [$type:ty;]) => { compile_error!("Please add SIZE to array type: It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
258 ($NAME:ident, [$type:ty;$size:literal]) => { compile_error!("Please specify init function INIT_FN: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
259 ($NAME:ident, [$type:ty;$size:literal], $num:literal) => { compile_error!("Please add |_| to last argument to turn it to closure: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
260 ($NAME:ident, $_:tt) => { compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
261 ($NAME:ident, $_n1:tt, $_n2:tt) => { compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };($NAME:ident, $_n1:tt, $_n2:tt, $_n3:tt) => { compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
262 ($NAME:ident, $_n1:tt, $_n2:tt, $_fn_name:ident) => { compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
263 ($NAME:ident, $_n1:tt, $_n2:tt, |$_cl:tt| $_b:tt) => { compile_error!("Array type has wrong format. It should be [TYPE; SIZE]: \n make_const_arr!(ARR_NAME, [TYPE; SIZE], INIT_FN);\n e.g. make_const_arr!(MY_ARR , [i32; 1024], |i| i as i32);"); };
264}