bc_envelope/extension/expressions/
parameters.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::sync::{Once, Mutex};
use paste::paste;

use super::{Parameter, ParametersStore};

/// A macro that declares a parameter at compile time.
#[macro_export]
macro_rules! parameter_constant {
    ($const_name:ident, $value:expr, $name:expr) => {
        paste! {
            pub const [<$const_name _VALUE>]: u64 = $value;
        }
        pub const $const_name: Parameter = Parameter::new_with_static_name($value, $name);
    };
}

parameter_constant!(BLANK, 1, "_");
parameter_constant!(LHS, 2, "lhs");
parameter_constant!(RHS, 3, "rhs");

#[doc(hidden)]
#[derive(Debug)]
pub struct LazyParameters {
    init: Once,
    data: Mutex<Option<ParametersStore>>,
}

impl LazyParameters {
    pub fn get(&self) -> std::sync::MutexGuard<'_, Option<ParametersStore>> {
        self.init.call_once(|| {
            let m = ParametersStore::new([
                BLANK,
                LHS,
                RHS,
            ]);
            *self.data.lock().unwrap() = Some(m);
        });
        self.data.lock().unwrap()
    }
}

/// The global shared store of known parameters.
pub static GLOBAL_PARAMETERS: LazyParameters = LazyParameters {
    init: Once::new(),
    data: Mutex::new(None),
};