Skip to content
This repository was archived by the owner on May 4, 2024. It is now read-only.

Commit 2ee1733

Browse files
authored
[chore] implement GetModule for all ModuleResolvers (#935)
This allows us to use GetModule with less boilerplate in some other places where it's needed
1 parent 030fe38 commit 2ee1733

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

language/tools/move-bytecode-utils/src/module_cache.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,39 @@ impl<R: ModuleResolver> GetModule for ModuleCache<R> {
6767
}
6868
}
6969

70+
impl<R: ModuleResolver> GetModule for &R {
71+
type Error = R::Error;
72+
type Item = CompiledModule;
73+
74+
fn get_module_by_id(&self, id: &ModuleId) -> Result<Option<CompiledModule>, Self::Error> {
75+
Ok(self
76+
.get_module(id)
77+
.unwrap()
78+
.map(|bytes| CompiledModule::deserialize(&bytes).unwrap()))
79+
}
80+
}
81+
82+
impl<R: ModuleResolver> GetModule for &mut R {
83+
type Error = R::Error;
84+
type Item = CompiledModule;
85+
86+
fn get_module_by_id(&self, id: &ModuleId) -> Result<Option<CompiledModule>, Self::Error> {
87+
Ok(self
88+
.get_module(id)
89+
.unwrap()
90+
.map(|bytes| CompiledModule::deserialize(&bytes).unwrap()))
91+
}
92+
}
93+
94+
impl<T: GetModule> GetModule for Arc<T> {
95+
type Error = T::Error;
96+
type Item = T::Item;
97+
98+
fn get_module_by_id(&self, id: &ModuleId) -> Result<Option<T::Item>, Self::Error> {
99+
self.as_ref().get_module_by_id(id)
100+
}
101+
}
102+
70103
/// Simple in-memory module cache that implements Sync
71104
pub struct SyncModuleCache<R: ModuleResolver> {
72105
cache: RwLock<BTreeMap<ModuleId, Arc<CompiledModule>>>,

language/tools/move-cli/src/sandbox/utils/on_disk_state_view.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -419,21 +419,6 @@ impl ResourceResolver for OnDiskStateView {
419419
}
420420
}
421421

422-
impl GetModule for &OnDiskStateView {
423-
type Error = anyhow::Error;
424-
type Item = CompiledModule;
425-
426-
fn get_module_by_id(&self, id: &ModuleId) -> Result<Option<CompiledModule>, Self::Error> {
427-
if let Some(bytes) = self.get_module_bytes(id)? {
428-
let module = CompiledModule::deserialize(&bytes)
429-
.map_err(|e| anyhow!("Failure deserializing module {:?}: {:?}", id, e))?;
430-
Ok(Some(module))
431-
} else {
432-
Ok(None)
433-
}
434-
}
435-
}
436-
437422
impl Default for OnDiskStateView {
438423
fn default() -> Self {
439424
OnDiskStateView::create(Path::new(DEFAULT_BUILD_DIR), Path::new(DEFAULT_STORAGE_DIR))

0 commit comments

Comments
 (0)