Skip to main content

miden_standards/
standards_lib.rs

1use alloc::sync::Arc;
2
3use miden_protocol::assembly::Package;
4use miden_protocol::assembly::mast::MastForest;
5use miden_protocol::utils::sync::LazyLock;
6
7// CONSTANTS
8// ================================================================================================
9
10const STANDARDS_PACKAGE_BYTES: &[u8] =
11    include_bytes!(concat!(env!("OUT_DIR"), "/assets/miden-standards.masp"));
12
13static STANDARDS_PACKAGE: LazyLock<Arc<Package>> = LazyLock::new(|| {
14    Arc::new(
15        // These bytes are produced by this crate's build script and embedded in the binary.
16        Package::read_from_bytes_trusted(STANDARDS_PACKAGE_BYTES)
17            .expect("standards lib masp should be well-formed"),
18    )
19});
20
21// MIDEN STANDARDS LIBRARY
22// ================================================================================================
23
24#[derive(Clone)]
25pub struct StandardsLib(Arc<Package>);
26
27impl StandardsLib {
28    /// Returns the underlying [`Arc<Package>`]
29    pub fn package(&self) -> Arc<Package> {
30        self.0.clone()
31    }
32
33    /// Returns a reference to the [`MastForest`] of the inner [`Package`].
34    pub fn mast_forest(&self) -> &Arc<MastForest> {
35        self.0.mast_forest()
36    }
37}
38
39impl AsRef<Package> for StandardsLib {
40    fn as_ref(&self) -> &Package {
41        self.0.as_ref()
42    }
43}
44
45impl From<StandardsLib> for Package {
46    fn from(value: StandardsLib) -> Self {
47        Arc::unwrap_or_clone(value.0)
48    }
49}
50
51impl Default for StandardsLib {
52    fn default() -> Self {
53        StandardsLib(STANDARDS_PACKAGE.clone())
54    }
55}
56
57// TESTS
58// ================================================================================================
59
60// NOTE: Most standards-related tests can be found in miden-testing.
61#[cfg(all(test, feature = "std"))]
62mod tests {
63    use miden_protocol::assembly::Path;
64
65    use super::StandardsLib;
66
67    #[test]
68    fn test_compile() {
69        let path = Path::new("::miden::standards::faucets::fungible::mint_and_send");
70        let miden = StandardsLib::default();
71        let exists = miden.0.module_descriptors().any(|module| {
72            module
73                .procedures()
74                .any(|(_, proc)| module.path().join(&proc.name).as_path() == path)
75        });
76
77        assert!(exists);
78    }
79}