Skip to content

Commit

Permalink
additions based on integration into builtin-actors
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Apr 22, 2022
1 parent bafcdc5 commit 8a68e6a
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 21 deletions.
2 changes: 1 addition & 1 deletion fvm/src/state_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ where
where
F: FnMut(Address, &ActorState) -> anyhow::Result<()>,
{
self.hamt.for_each(|k, v| {
self.hamt.try_for_each(|k, v| {
let addr = Address::from_bytes(&k.0)?;
f(addr, v)
})?;
Expand Down
2 changes: 1 addition & 1 deletion ipld/amt/benches/amt_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn for_each(c: &mut Criterion) {
b.iter(|| {
let a = Amt::load(&cid, &db).unwrap();
black_box(a)
.for_each(|_, _v: &u64| Ok::<_, ()>(()))
.try_for_each(|_, _v: &u64| Ok::<_, ()>(()))
.unwrap();
})
});
Expand Down
75 changes: 68 additions & 7 deletions ipld/amt/src/amt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,26 +284,42 @@ where
/// map.set(4, "Four".to_owned()).unwrap();
///
/// let mut values: Vec<(u64, String)> = Vec::new();
/// map.for_each(|i, v| {
/// map.try_for_each(|i, v| {
/// values.push((i, v.clone()));
/// Ok::<_, ()>(())
/// }).unwrap();
/// assert_eq!(&values, &[(1, "One".to_owned()), (4, "Four".to_owned())]);
/// ```
#[inline]
pub fn for_each<F, U>(&self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
pub fn try_for_each<F, U>(&self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
where
F: FnMut(u64, &V) -> Result<(), U>,
{
self.for_each_while(|i, x| {
self.try_for_each_while(|i, x| {
f(i, x)?;
Ok(true)
})
}

#[inline]
pub fn for_each<F>(&self, mut f: F) -> Result<(), Error<BS::Error>>
where
V: DeserializeOwned,
F: FnMut(u64, &V),
{
self.try_for_each(|k, v| {
f(k, v);
Ok(())
})
.map_err(|err| match err {
EitherError::User(()) => unreachable!(),
EitherError::Amt(e) => e,
})
}

/// Iterates over each value in the Amt and runs a function on the values, for as long as that
/// function keeps returning `true`.
pub fn for_each_while<F, U>(&self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
pub fn try_for_each_while<F, U>(&self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
where
F: FnMut(u64, &V) -> Result<bool, U>,
{
Expand All @@ -319,22 +335,51 @@ where
.map(|_| ())
}

/// Iterates over each value in the Amt and runs a function on the values, for as long as that
/// function keeps returning `true`.
pub fn for_each_while<F>(&self, mut f: F) -> Result<(), Error<BS::Error>>
where
F: FnMut(u64, &V) -> bool,
{
self.try_for_each_while::<_, ()>(|key, value| Ok(f(key, value)))
.map_err(|err| match err {
EitherError::User(()) => unreachable!(),
EitherError::Amt(e) => e,
})
}

/// Iterates over each value in the Amt and runs a function on the values that allows modifying
/// each value.
pub fn for_each_mut<F, U>(&mut self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
pub fn try_for_each_mut<F, U>(&mut self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
where
V: Clone,
F: FnMut(u64, &mut ValueMut<'_, V>) -> Result<(), U>,
{
self.for_each_while_mut(|i, x| {
self.try_for_each_while_mut(|i, x| {
f(i, x)?;
Ok(true)
})
}

/// Iterates over each value in the Amt and runs a function on the values that allows modifying
/// each value.
pub fn for_each_mut<F>(&mut self, mut f: F) -> Result<(), Error<BS::Error>>
where
V: Clone,
F: FnMut(u64, &mut ValueMut<'_, V>),
{
self.for_each_while_mut(|i, x| {
f(i, x);
true
})
}

/// Iterates over each value in the Amt and runs a function on the values that allows modifying
/// each value, for as long as that function keeps returning `true`.
pub fn for_each_while_mut<F, U>(&mut self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
pub fn try_for_each_while_mut<F, U>(
&mut self,
mut f: F,
) -> Result<(), EitherError<U, BS::Error>>
where
// TODO remove clone bound when go-interop doesn't require it.
// (If needed without, this bound can be removed by duplicating function signatures)
Expand Down Expand Up @@ -392,4 +437,20 @@ where
Ok(())
}
}

/// Iterates over each value in the Amt and runs a function on the values that allows modifying
/// each value, for as long as that function keeps returning `true`.
pub fn for_each_while_mut<F>(&mut self, mut f: F) -> Result<(), Error<BS::Error>>
where
// TODO remove clone bound when go-interop doesn't require it.
// (If needed without, this bound can be removed by duplicating function signatures)
V: Clone,
F: FnMut(u64, &mut ValueMut<'_, V>) -> bool,
{
self.try_for_each_while_mut::<_, ()>(|key, value| Ok(f(key, value)))
.map_err(|err| match err {
EitherError::User(()) => unreachable!(),
EitherError::Amt(e) => e,
})
}
}
2 changes: 1 addition & 1 deletion ipld/amt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod root;
mod value_mut;

pub use self::amt::Amt;
pub use self::error::Error;
pub use self::error::{EitherError, Error};
pub(crate) use self::node::Node;
pub(crate) use self::root::Root;
pub use self::value_mut::ValueMut;
Expand Down
5 changes: 1 addition & 4 deletions ipld/amt/tests/amt_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ fn for_each() {
let mut x = 0;
a.for_each(|_, _: &BytesDe| {
x += 1;
Ok::<(), ()>(())
})
.unwrap();

Expand All @@ -343,13 +342,12 @@ fn for_each() {
);
}
x += 1;
Ok::<(), ()>(())
})
.unwrap();
assert_eq!(x, indexes.len());

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

assert_eq!(
c.to_string().as_str(),
Expand Down Expand Up @@ -385,7 +383,6 @@ fn for_each_mutate() {
// Value it's set to doesn't matter, just cloning for expedience
**v = v.clone();
}
Ok::<(), ()>(())
})
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion ipld/blockstore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use block::*;
///
/// The cgo blockstore adapter implements this trait.
pub trait Blockstore {
type Error: std::error::Error + std::fmt::Debug + Send + Sync + 'static;
type Error: std::error::Error + Send + Sync + 'static;

/// Gets the block from the blockstore.
fn get(&self, k: &Cid) -> Result<Option<Vec<u8>>, Self::Error>;
Expand Down
2 changes: 1 addition & 1 deletion ipld/hamt/benches/hamt_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn for_each(c: &mut Criterion) {
b.iter(|| {
let a = Hamt::<_, _>::load(&cid, &db).unwrap();
black_box(a)
.for_each(|_k, _v: &BenchData| Ok::<_, ()>(()))
.try_for_each(|_k, _v: &BenchData| Ok::<_, ()>(()))
.unwrap();
})
});
Expand Down
20 changes: 18 additions & 2 deletions ipld/hamt/src/hamt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,21 +304,37 @@ where
/// map.set(4, 2).unwrap();
///
/// let mut total = 0;
/// map.for_each(|_, v: &u64| {
/// map.try_for_each(|_, v: &u64| {
/// total += v;
/// Ok::<(), ()>(())
/// }).unwrap();
/// assert_eq!(total, 3);
/// ```
#[inline]
pub fn for_each<F, U>(&self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
pub fn try_for_each<F, U>(&self, mut f: F) -> Result<(), EitherError<U, BS::Error>>
where
V: DeserializeOwned,
F: FnMut(&K, &V) -> Result<(), U>,
{
self.root.for_each(self.store.borrow(), &mut f)
}

#[inline]
pub fn for_each<F>(&self, mut f: F) -> Result<(), Error<BS::Error>>
where
V: DeserializeOwned,
F: FnMut(&K, &V),
{
self.try_for_each(|k, v| {
f(k, v);
Ok(())
})
.map_err(|err| match err {
EitherError::User(()) => unreachable!(),
EitherError::Hamt(e) => e,
})
}

/// Consumes this HAMT and returns the Blockstore it owns.
pub fn into_store(self) -> BS {
self.store
Expand Down
3 changes: 0 additions & 3 deletions ipld/hamt/tests/hamt_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ fn for_each() {
hamt.for_each(|k, v| {
assert_eq!(k, v);
count += 1;
Ok::<(), ()>(())
})
.unwrap();
assert_eq!(count, 200);
Expand All @@ -291,7 +290,6 @@ fn for_each() {
hamt.for_each(|k, v| {
assert_eq!(k, v);
count += 1;
Ok::<(), ()>(())
})
.unwrap();
assert_eq!(count, 200);
Expand All @@ -301,7 +299,6 @@ fn for_each() {
hamt.for_each(|k, v| {
assert_eq!(k, v);
count += 1;
Ok::<(), ()>(())
})
.unwrap();
assert_eq!(count, 200);
Expand Down

0 comments on commit 8a68e6a

Please sign in to comment.