Skip to content

Commit c9be650

Browse files
authored
refactor: avoid tricky error downcasting in the Map (filecoin-project#1590)
Now that we have external iterators, we don't need this error smuggling.
1 parent 7308289 commit c9be650

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

runtime/src/util/map.rs

+10-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::builtin::HAMT_BIT_WIDTH;
22
use crate::{ActorError, AsActorError, Hasher};
3-
use anyhow::anyhow;
43
use cid::Cid;
54
use fvm_ipld_blockstore::Blockstore;
65
use fvm_ipld_hamt as hamt;
@@ -153,29 +152,18 @@ where
153152
/// Iterates over all key-value pairs in the map.
154153
pub fn for_each<F>(&self, mut f: F) -> Result<(), ActorError>
155154
where
156-
// Note the result type of F uses ActorError.
157-
// The implementation will extract and propagate any ActorError
158-
// wrapped in a hamt::Error::Dynamic.
159155
F: FnMut(K, &V) -> Result<(), ActorError>,
160156
{
161-
self.hamt
162-
.for_each(|k, v| {
163-
let key =
164-
K::from_bytes(k).context_code(ExitCode::USR_ILLEGAL_STATE, "invalid key")?;
165-
f(key, v).map_err(|e| anyhow!(e))
166-
})
167-
.map_err(|hamt_err| match hamt_err {
168-
hamt::Error::Dynamic(e) => match e.downcast::<ActorError>() {
169-
Ok(ae) => ae,
170-
Err(e) => ActorError::illegal_state(format!(
171-
"error in callback traversing HAMT {}: {}",
172-
self.name, e
173-
)),
174-
},
175-
e => {
176-
ActorError::illegal_state(format!("error traversing HAMT {}: {}", self.name, e))
177-
}
178-
})
157+
for kv in &self.hamt {
158+
let (k, v) = kv.with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
159+
format!("error traversing HAMT {}", self.name)
160+
})?;
161+
let k = K::from_bytes(k).with_context_code(ExitCode::USR_ILLEGAL_STATE, || {
162+
format!("invalid key in HAMT {}", self.name)
163+
})?;
164+
f(k, v)?;
165+
}
166+
Ok(())
179167
}
180168
}
181169

0 commit comments

Comments
 (0)