Skip to content

Commit 4b20a04

Browse files
additions based on integration into builtin-actors
1 parent fd9f106 commit 4b20a04

File tree

9 files changed

+92
-21
lines changed

9 files changed

+92
-21
lines changed

Diff for: fvm/src/state_tree.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ where
504504
where
505505
F: FnMut(Address, &ActorState) -> anyhow::Result<()>,
506506
{
507-
self.hamt.for_each(|k, v| {
507+
self.hamt.try_for_each(|k, v| {
508508
let addr = Address::from_bytes(&k.0)?;
509509
f(addr, v)
510510
})?;

Diff for: ipld/amt/benches/amt_benchmark.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fn for_each(c: &mut Criterion) {
118118
b.iter(|| {
119119
let a = Amt::load(&cid, &db).unwrap();
120120
black_box(a)
121-
.for_each(|_, _v: &u64| Ok::<_, ()>(()))
121+
.try_for_each(|_, _v: &u64| Ok::<_, ()>(()))
122122
.unwrap();
123123
})
124124
});

Diff for: ipld/amt/src/amt.rs

+68-7
Original file line numberDiff line numberDiff line change
@@ -284,26 +284,42 @@ where
284284
/// map.set(4, "Four".to_owned()).unwrap();
285285
///
286286
/// let mut values: Vec<(u64, String)> = Vec::new();
287-
/// map.for_each(|i, v| {
287+
/// map.try_for_each(|i, v| {
288288
/// values.push((i, v.clone()));
289289
/// Ok::<_, ()>(())
290290
/// }).unwrap();
291291
/// assert_eq!(&values, &[(1, "One".to_owned()), (4, "Four".to_owned())]);
292292
/// ```
293293
#[inline]
294-
pub fn for_each<F, U>(&self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
294+
pub fn try_for_each<F, U>(&self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
295295
where
296296
F: FnMut(u64, &V) -> Result<(), U>,
297297
{
298-
self.for_each_while(|i, x| {
298+
self.try_for_each_while(|i, x| {
299299
f(i, x)?;
300300
Ok(true)
301301
})
302302
}
303303

304+
#[inline]
305+
pub fn for_each<F>(&self, mut f: F) -> Result<(), Error<BS::Error>>
306+
where
307+
V: DeserializeOwned,
308+
F: FnMut(u64, &V),
309+
{
310+
self.try_for_each(|k, v| {
311+
f(k, v);
312+
Ok(())
313+
})
314+
.map_err(|err| match err {
315+
EitherError::User(()) => unreachable!(),
316+
EitherError::Amt(e) => e,
317+
})
318+
}
319+
304320
/// Iterates over each value in the Amt and runs a function on the values, for as long as that
305321
/// function keeps returning `true`.
306-
pub fn for_each_while<F, U>(&self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
322+
pub fn try_for_each_while<F, U>(&self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
307323
where
308324
F: FnMut(u64, &V) -> Result<bool, U>,
309325
{
@@ -319,22 +335,51 @@ where
319335
.map(|_| ())
320336
}
321337

338+
/// Iterates over each value in the Amt and runs a function on the values, for as long as that
339+
/// function keeps returning `true`.
340+
pub fn for_each_while<F>(&self, mut f: F) -> Result<(), Error<BS::Error>>
341+
where
342+
F: FnMut(u64, &V) -> bool,
343+
{
344+
self.try_for_each_while::<_, ()>(|key, value| Ok(f(key, value)))
345+
.map_err(|err| match err {
346+
EitherError::User(()) => unreachable!(),
347+
EitherError::Amt(e) => e,
348+
})
349+
}
350+
322351
/// Iterates over each value in the Amt and runs a function on the values that allows modifying
323352
/// each value.
324-
pub fn for_each_mut<F, U>(&mut self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
353+
pub fn try_for_each_mut<F, U>(&mut self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
325354
where
326355
V: Clone,
327356
F: FnMut(u64, &mut ValueMut<'_, V>) -> Result<(), U>,
328357
{
329-
self.for_each_while_mut(|i, x| {
358+
self.try_for_each_while_mut(|i, x| {
330359
f(i, x)?;
331360
Ok(true)
332361
})
333362
}
334363

364+
/// Iterates over each value in the Amt and runs a function on the values that allows modifying
365+
/// each value.
366+
pub fn for_each_mut<F>(&mut self, mut f: F) -> Result<(), Error<BS::Error>>
367+
where
368+
V: Clone,
369+
F: FnMut(u64, &mut ValueMut<'_, V>),
370+
{
371+
self.for_each_while_mut(|i, x| {
372+
f(i, x);
373+
true
374+
})
375+
}
376+
335377
/// Iterates over each value in the Amt and runs a function on the values that allows modifying
336378
/// each value, for as long as that function keeps returning `true`.
337-
pub fn for_each_while_mut<F, U>(&mut self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
379+
pub fn try_for_each_while_mut<F, U>(
380+
&mut self,
381+
mut f: F,
382+
) -> Result<(), EitherError<U, BS::Error>>
338383
where
339384
// TODO remove clone bound when go-interop doesn't require it.
340385
// (If needed without, this bound can be removed by duplicating function signatures)
@@ -392,4 +437,20 @@ where
392437
Ok(())
393438
}
394439
}
440+
441+
/// Iterates over each value in the Amt and runs a function on the values that allows modifying
442+
/// each value, for as long as that function keeps returning `true`.
443+
pub fn for_each_while_mut<F>(&mut self, mut f: F) -> Result<(), Error<BS::Error>>
444+
where
445+
// TODO remove clone bound when go-interop doesn't require it.
446+
// (If needed without, this bound can be removed by duplicating function signatures)
447+
V: Clone,
448+
F: FnMut(u64, &mut ValueMut<'_, V>) -> bool,
449+
{
450+
self.try_for_each_while_mut::<_, ()>(|key, value| Ok(f(key, value)))
451+
.map_err(|err| match err {
452+
EitherError::User(()) => unreachable!(),
453+
EitherError::Amt(e) => e,
454+
})
455+
}
395456
}

Diff for: ipld/amt/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod root;
1313
mod value_mut;
1414

1515
pub use self::amt::Amt;
16-
pub use self::error::Error;
16+
pub use self::error::{EitherError, Error};
1717
pub(crate) use self::node::Node;
1818
pub(crate) use self::root::Root;
1919
pub use self::value_mut::ValueMut;

Diff for: ipld/amt/tests/amt_tests.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ fn for_each() {
322322
let mut x = 0;
323323
a.for_each(|_, _: &BytesDe| {
324324
x += 1;
325-
Ok::<(), ()>(())
326325
})
327326
.unwrap();
328327

@@ -343,13 +342,12 @@ fn for_each() {
343342
);
344343
}
345344
x += 1;
346-
Ok::<(), ()>(())
347345
})
348346
.unwrap();
349347
assert_eq!(x, indexes.len());
350348

351349
// Iteration again will be read diff with go-interop, since they do not cache
352-
new_amt.for_each(|_, _: &BytesDe| Ok::<(), ()>(())).unwrap();
350+
new_amt.for_each(|_, _: &BytesDe| {}).unwrap();
353351

354352
assert_eq!(
355353
c.to_string().as_str(),
@@ -385,7 +383,6 @@ fn for_each_mutate() {
385383
// Value it's set to doesn't matter, just cloning for expedience
386384
**v = v.clone();
387385
}
388-
Ok::<(), ()>(())
389386
})
390387
.unwrap();
391388

Diff for: ipld/blockstore/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use block::*;
1414
///
1515
/// The cgo blockstore adapter implements this trait.
1616
pub trait Blockstore {
17-
type Error: std::error::Error + std::fmt::Debug + Send + Sync + 'static;
17+
type Error: std::error::Error + Send + Sync + 'static;
1818

1919
/// Gets the block from the blockstore.
2020
fn get(&self, k: &Cid) -> Result<Option<Vec<u8>>, Self::Error>;

Diff for: ipld/hamt/benches/hamt_benchmark.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn for_each(c: &mut Criterion) {
9595
b.iter(|| {
9696
let a = Hamt::<_, _>::load(&cid, &db).unwrap();
9797
black_box(a)
98-
.for_each(|_k, _v: &BenchData| Ok::<_, ()>(()))
98+
.try_for_each(|_k, _v: &BenchData| Ok::<_, ()>(()))
9999
.unwrap();
100100
})
101101
});

Diff for: ipld/hamt/src/hamt.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -304,21 +304,37 @@ where
304304
/// map.set(4, 2).unwrap();
305305
///
306306
/// let mut total = 0;
307-
/// map.for_each(|_, v: &u64| {
307+
/// map.try_for_each(|_, v: &u64| {
308308
/// total += v;
309309
/// Ok::<(), ()>(())
310310
/// }).unwrap();
311311
/// assert_eq!(total, 3);
312312
/// ```
313313
#[inline]
314-
pub fn for_each<F, U>(&self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
314+
pub fn try_for_each<F, U>(&self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
315315
where
316316
V: DeserializeOwned,
317317
F: FnMut(&K, &V) -> Result<(), U>,
318318
{
319319
self.root.for_each(self.store.borrow(), &mut f)
320320
}
321321

322+
#[inline]
323+
pub fn for_each<F>(&self, mut f: F) -> Result<(), Error<BS::Error>>
324+
where
325+
V: DeserializeOwned,
326+
F: FnMut(&K, &V),
327+
{
328+
self.try_for_each(|k, v| {
329+
f(k, v);
330+
Ok(())
331+
})
332+
.map_err(|err| match err {
333+
EitherError::User(()) => unreachable!(),
334+
EitherError::Hamt(e) => e,
335+
})
336+
}
337+
322338
/// Consumes this HAMT and returns the Blockstore it owns.
323339
pub fn into_store(self) -> BS {
324340
self.store

Diff for: ipld/hamt/tests/hamt_tests.rs

-3
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ fn for_each() {
273273
hamt.for_each(|k, v| {
274274
assert_eq!(k, v);
275275
count += 1;
276-
Ok::<(), ()>(())
277276
})
278277
.unwrap();
279278
assert_eq!(count, 200);
@@ -291,7 +290,6 @@ fn for_each() {
291290
hamt.for_each(|k, v| {
292291
assert_eq!(k, v);
293292
count += 1;
294-
Ok::<(), ()>(())
295293
})
296294
.unwrap();
297295
assert_eq!(count, 200);
@@ -301,7 +299,6 @@ fn for_each() {
301299
hamt.for_each(|k, v| {
302300
assert_eq!(k, v);
303301
count += 1;
304-
Ok::<(), ()>(())
305302
})
306303
.unwrap();
307304
assert_eq!(count, 200);

0 commit comments

Comments
 (0)