From c9be650ff7b93a9a1598e58ca2f948c28eae1c08 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Mon, 25 Nov 2024 01:47:09 +0000 Subject: [PATCH] refactor: avoid tricky error downcasting in the Map (#1590) Now that we have external iterators, we don't need this error smuggling. --- runtime/src/util/map.rs | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/runtime/src/util/map.rs b/runtime/src/util/map.rs index b3c3e3ee2..0052f3546 100644 --- a/runtime/src/util/map.rs +++ b/runtime/src/util/map.rs @@ -1,6 +1,5 @@ use crate::builtin::HAMT_BIT_WIDTH; use crate::{ActorError, AsActorError, Hasher}; -use anyhow::anyhow; use cid::Cid; use fvm_ipld_blockstore::Blockstore; use fvm_ipld_hamt as hamt; @@ -153,29 +152,18 @@ where /// Iterates over all key-value pairs in the map. pub fn for_each(&self, mut f: F) -> Result<(), ActorError> where - // Note the result type of F uses ActorError. - // The implementation will extract and propagate any ActorError - // wrapped in a hamt::Error::Dynamic. F: FnMut(K, &V) -> Result<(), ActorError>, { - self.hamt - .for_each(|k, v| { - let key = - K::from_bytes(k).context_code(ExitCode::USR_ILLEGAL_STATE, "invalid key")?; - f(key, v).map_err(|e| anyhow!(e)) - }) - .map_err(|hamt_err| match hamt_err { - hamt::Error::Dynamic(e) => match e.downcast::() { - Ok(ae) => ae, - Err(e) => ActorError::illegal_state(format!( - "error in callback traversing HAMT {}: {}", - self.name, e - )), - }, - e => { - ActorError::illegal_state(format!("error traversing HAMT {}: {}", self.name, e)) - } - }) + for kv in &self.hamt { + let (k, v) = kv.with_context_code(ExitCode::USR_ILLEGAL_STATE, || { + format!("error traversing HAMT {}", self.name) + })?; + let k = K::from_bytes(k).with_context_code(ExitCode::USR_ILLEGAL_STATE, || { + format!("invalid key in HAMT {}", self.name) + })?; + f(k, v)?; + } + Ok(()) } }