bc_envelope/extension/expressions/
functions.rs1use std::sync::{Once, Mutex};
2use paste::paste;
3
4use super::{Function, FunctionsStore};
5
6#[macro_export]
8macro_rules! function_constant {
9 ($const_name:ident, $value:expr, $name:expr) => {
10 paste! {
11 pub const [<$const_name _VALUE>]: u64 = $value;
12 }
13 pub const $const_name: Function = Function::new_with_static_name($value, $name);
14 };
15}
16
17function_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)]
34#[derive(Debug)]
35pub struct LazyFunctions {
36 init: Once,
37 data: Mutex<Option<FunctionsStore>>,
38}
39
40impl LazyFunctions {
41 pub fn get(&self) -> std::sync::MutexGuard<'_, Option<FunctionsStore>> {
42 self.init.call_once(|| {
43 let m = FunctionsStore::new([
44 ADD,
45 SUB,
46 MUL,
47 DIV,
48 ]);
49 *self.data.lock().unwrap() = Some(m);
50 });
51 self.data.lock().unwrap()
52 }
53}
54
55pub static GLOBAL_FUNCTIONS: LazyFunctions = LazyFunctions {
57 init: Once::new(),
58 data: Mutex::new(None),
59};