Skip to content

Commit 1499c9b

Browse files
authored
fix: propagate errors in Amt::for_each_cacheless (#2218)
1 parent fe4d5c1 commit 1499c9b

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

ipld/amt/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [Unreleased]
44

5+
- Fixed a bug in `for_each_cacheless` where it would swallow errors and continue iterating.
6+
57
## 0.7.5 [2025-08-05]
68

79
- Added `for_each_cacheless` method to iterate over the AMT without caching the values. This is lowers memory requirements usage and is useful for single-pass, read-only operations over large AMTs.

ipld/amt/src/amt.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,8 @@ where
593593

594594
/// Iterates over each value in the Amt and runs a function on the values. This is a
595595
/// non-caching version of [`Self::for_each`]. It can potentially be more efficient, especially memory-wise,
596-
/// for large AMTs or when the iteration occurs only once.
596+
/// for large AMTs or when the iteration occurs only once. An error during iteration stops the
597+
/// iteration and is returned.
597598
///
598599
/// # Examples
599600
///

ipld/amt/src/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ where
437437
Node::Leaf { vals } => {
438438
for (i, v) in (0..).zip(vals.iter()) {
439439
if let Some(v) = v {
440-
let _ = f(offset + i, v);
440+
f(offset + i, v)?;
441441
}
442442
}
443443
}

ipld/amt/tests/amt_tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ fn for_each_cacheless() {
459459
// 2nd pass, no caching so block reads roughly doubled.
460460
#[rustfmt::skip]
461461
assert_eq!(*db.stats.borrow(), BSStats {r: 2861, w: 1431, br: 177158, bw: 88649});
462+
463+
// errorr during iteration is propagated
464+
let res = new_amt.for_each_cacheless(|_, _: &BytesDe| anyhow::bail!("cthulhu fhtagn"));
465+
let err = res.unwrap_err();
466+
assert_eq!(err.to_string(), "cthulhu fhtagn");
462467
}
463468

464469
#[test]

0 commit comments

Comments
 (0)