bc_envelope/extension/expressions/
functions.rs1use std::sync::{Mutex, Once};
2
3use paste::paste;
4
5use super::{Function, FunctionsStore};
6
7#[macro_export]
9macro_rules! function_constant {
10 ($const_name:ident, $value:expr, $name:expr) => {
11 paste! {
12 pub const [<$const_name _VALUE>]: u64 = $value;
13 }
14 pub const $const_name: Function =
15 Function::new_with_static_name($value, $name);
16 };
17}
18
19function_constant!(ADD, 1, "add"); function_constant!(SUB, 2, "sub"); function_constant!(MUL, 3, "mul"); function_constant!(DIV, 4, "div"); function_constant!(NEG, 5, "neg"); function_constant!(LT, 6, "lt"); function_constant!(LE, 7, "le"); function_constant!(GT, 8, "gt"); function_constant!(GE, 9, "ge"); function_constant!(EQ, 10, "eq"); function_constant!(NE, 11, "ne"); function_constant!(AND, 12, "and"); function_constant!(OR, 13, "or"); function_constant!(XOR, 14, "xor"); function_constant!(NOT, 15, "not"); #[doc(hidden)]
36#[derive(Debug)]
37pub struct LazyFunctions {
38 init: Once,
39 data: Mutex<Option<FunctionsStore>>,
40}
41
42impl LazyFunctions {
43 pub fn get(&self) -> std::sync::MutexGuard<'_, Option<FunctionsStore>> {
44 self.init.call_once(|| {
45 let m = FunctionsStore::new([ADD, SUB, MUL, DIV]);
46 *self.data.lock().unwrap() = Some(m);
47 });
48 self.data.lock().unwrap()
49 }
50}
51
52pub static GLOBAL_FUNCTIONS: LazyFunctions =
54 LazyFunctions { init: Once::new(), data: Mutex::new(None) };