miden_processor/host/
mast_forest_store.rs1use alloc::{collections::BTreeMap, sync::Arc};
2
3use miden_core::{Word, mast::MastForest};
4use miden_mast_package::{PackageDebugInfoError, debug_info::PackageDebugInfo};
5
6#[derive(Debug, Clone)]
9pub struct LoadedMastForest {
10 mast_forest: Arc<MastForest>,
11 debug_info: Result<Option<Arc<PackageDebugInfo>>, Arc<PackageDebugInfoError>>,
12}
13
14impl LoadedMastForest {
15 pub fn new(mast_forest: Arc<MastForest>) -> Self {
17 Self { mast_forest, debug_info: Ok(None) }
18 }
19
20 pub fn with_package_debug_info(
23 mast_forest: Arc<MastForest>,
24 debug_info: Result<Option<PackageDebugInfo>, PackageDebugInfoError>,
25 ) -> Self {
26 Self {
27 mast_forest,
28 debug_info: debug_info.map(|debug_info| debug_info.map(Arc::new)).map_err(Arc::new),
29 }
30 }
31
32 pub fn mast_forest(&self) -> &Arc<MastForest> {
34 &self.mast_forest
35 }
36
37 pub fn package_debug_info(
39 &self,
40 ) -> Result<Option<Arc<PackageDebugInfo>>, Arc<PackageDebugInfoError>> {
41 self.debug_info.clone()
42 }
43}
44
45pub trait MastForestStore {
53 fn get(&self, procedure_hash: &Word) -> Option<LoadedMastForest>;
56}
57
58#[derive(Debug, Default, Clone)]
60pub struct MemMastForestStore {
61 mast_forests: BTreeMap<Word, LoadedMastForest>,
62}
63
64impl MemMastForestStore {
65 pub fn insert(&mut self, mast_forest: Arc<MastForest>) {
67 self.insert_loaded(LoadedMastForest::new(mast_forest));
68 }
69
70 pub fn insert_loaded(&mut self, loaded_mast_forest: LoadedMastForest) {
72 for proc_digest in loaded_mast_forest.mast_forest.local_procedure_digests() {
74 self.mast_forests.insert(proc_digest, loaded_mast_forest.clone());
75 }
76 }
77}
78
79impl MastForestStore for MemMastForestStore {
80 fn get(&self, procedure_hash: &Word) -> Option<LoadedMastForest> {
81 self.mast_forests.get(procedure_hash).cloned()
82 }
83}