Skip to main content

miden_standards/
standards_lib.rs

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