11// Copyright 2019-2022 ChainSafe Systems
22// SPDX-License-Identifier: Apache-2.0, MIT
33
4- use anyhow:: anyhow;
54use cid:: multihash:: Code ;
65use cid:: Cid ;
76use fvm_ipld_blockstore:: Blockstore ;
@@ -11,6 +10,7 @@ use fvm_ipld_encoding::CborStore;
1110use itertools:: sorted;
1211
1312use super :: ValueMut ;
13+ use crate :: error:: EitherError ;
1414use crate :: node:: { CollapsedNode , Link } ;
1515use crate :: {
1616 init_sized_vec, nodes_for_height, Error , Node , Root , DEFAULT_BIT_WIDTH , MAX_HEIGHT , MAX_INDEX ,
7272 }
7373
7474 /// Constructs an AMT with a blockstore and a Cid of the root of the AMT
75- pub fn load ( cid : & Cid , block_store : BS ) -> Result < Self , Error > {
75+ pub fn load ( cid : & Cid , block_store : BS ) -> Result < Self , Error < BS > > {
7676 // Load root bytes from database
7777 let root: Root < V > = block_store
7878 . get_cbor ( cid) ?
9797 }
9898
9999 /// Generates an AMT with block store and array of cbor marshallable objects and returns Cid
100- pub fn new_from_iter ( block_store : BS , vals : impl IntoIterator < Item = V > ) -> Result < Cid , Error > {
100+ pub fn new_from_iter (
101+ block_store : BS ,
102+ vals : impl IntoIterator < Item = V > ,
103+ ) -> Result < Cid , Error < BS > > {
101104 let mut t = Self :: new ( block_store) ;
102105
103106 t. batch_set ( vals) ?;
@@ -106,7 +109,7 @@ where
106109 }
107110
108111 /// Get value at index of AMT
109- pub fn get ( & self , i : u64 ) -> Result < Option < & V > , Error > {
112+ pub fn get ( & self , i : u64 ) -> Result < Option < & V > , Error < BS > > {
110113 if i > MAX_INDEX {
111114 return Err ( Error :: OutOfRange ( i) ) ;
112115 }
@@ -121,7 +124,7 @@ where
121124 }
122125
123126 /// Set value at index
124- pub fn set ( & mut self , i : u64 , val : V ) -> Result < ( ) , Error > {
127+ pub fn set ( & mut self , i : u64 , val : V ) -> Result < ( ) , Error < BS > > {
125128 if i > MAX_INDEX {
126129 return Err ( Error :: OutOfRange ( i) ) ;
127130 }
@@ -163,7 +166,7 @@ where
163166
164167 /// Batch set (naive for now)
165168 // TODO Implement more efficient batch set to not have to traverse tree and keep cache for each
166- pub fn batch_set ( & mut self , vals : impl IntoIterator < Item = V > ) -> Result < ( ) , Error > {
169+ pub fn batch_set ( & mut self , vals : impl IntoIterator < Item = V > ) -> Result < ( ) , Error < BS > > {
167170 for ( i, val) in ( 0u64 ..) . zip ( vals) {
168171 self . set ( i, val) ?;
169172 }
@@ -172,7 +175,7 @@ where
172175 }
173176
174177 /// Delete item from AMT at index
175- pub fn delete ( & mut self , i : u64 ) -> Result < Option < V > , Error > {
178+ pub fn delete ( & mut self , i : u64 ) -> Result < Option < V > , Error < BS > > {
176179 if i > MAX_INDEX {
177180 return Err ( Error :: OutOfRange ( i) ) ;
178181 }
@@ -243,23 +246,23 @@ where
243246 & mut self ,
244247 iter : impl IntoIterator < Item = u64 > ,
245248 strict : bool ,
246- ) -> Result < bool , Error > {
249+ ) -> Result < bool , Error < BS > > {
247250 // TODO: optimize this
248251 let mut modified = false ;
249252
250253 // Iterate sorted indices. Sorted to safely optimize later.
251254 for i in sorted ( iter) {
252255 let found = self . delete ( i) ?. is_none ( ) ;
253256 if strict && found {
254- return Err ( anyhow ! ( "no such index {} in Amt for batch delete" , i ) . into ( ) ) ;
257+ return Err ( Error :: BatchDelteNotFound ( i ) ) ;
255258 }
256259 modified |= found;
257260 }
258261 Ok ( modified)
259262 }
260263
261264 /// flush root and return Cid used as key in block store
262- pub fn flush ( & mut self ) -> Result < Cid , Error > {
265+ pub fn flush ( & mut self ) -> Result < Cid , Error < BS > > {
263266 self . root . node . flush ( & self . block_store ) ?;
264267 Ok ( self . block_store . put_cbor ( & self . root , Code :: Blake2b256 ) ?)
265268 }
@@ -283,14 +286,14 @@ where
283286 /// let mut values: Vec<(u64, String)> = Vec::new();
284287 /// map.for_each(|i, v| {
285288 /// values.push((i, v.clone()));
286- /// Ok(())
289+ /// Ok::<_, ()> (())
287290 /// }).unwrap();
288291 /// assert_eq!(&values, &[(1, "One".to_owned()), (4, "Four".to_owned())]);
289292 /// ```
290293 #[ inline]
291- pub fn for_each < F > ( & self , mut f : F ) -> Result < ( ) , Error >
294+ pub fn for_each < F , U > ( & self , mut f : F ) -> Result < ( ) , EitherError < U , BS > >
292295 where
293- F : FnMut ( u64 , & V ) -> anyhow :: Result < ( ) > ,
296+ F : FnMut ( u64 , & V ) -> Result < ( ) , U > ,
294297 {
295298 self . for_each_while ( |i, x| {
296299 f ( i, x) ?;
@@ -300,9 +303,9 @@ where
300303
301304 /// Iterates over each value in the Amt and runs a function on the values, for as long as that
302305 /// function keeps returning `true`.
303- pub fn for_each_while < F > ( & self , mut f : F ) -> Result < ( ) , Error >
306+ pub fn for_each_while < F , U > ( & self , mut f : F ) -> Result < ( ) , EitherError < U , BS > >
304307 where
305- F : FnMut ( u64 , & V ) -> anyhow :: Result < bool > ,
308+ F : FnMut ( u64 , & V ) -> Result < bool , U > ,
306309 {
307310 self . root
308311 . node
@@ -318,10 +321,10 @@ where
318321
319322 /// Iterates over each value in the Amt and runs a function on the values that allows modifying
320323 /// each value.
321- pub fn for_each_mut < F > ( & mut self , mut f : F ) -> Result < ( ) , Error >
324+ pub fn for_each_mut < F , U > ( & mut self , mut f : F ) -> Result < ( ) , EitherError < U , BS > >
322325 where
323326 V : Clone ,
324- F : FnMut ( u64 , & mut ValueMut < ' _ , V > ) -> anyhow :: Result < ( ) > ,
327+ F : FnMut ( u64 , & mut ValueMut < ' _ , V > ) -> Result < ( ) , U > ,
325328 {
326329 self . for_each_while_mut ( |i, x| {
327330 f ( i, x) ?;
@@ -331,12 +334,12 @@ where
331334
332335 /// Iterates over each value in the Amt and runs a function on the values that allows modifying
333336 /// each value, for as long as that function keeps returning `true`.
334- pub fn for_each_while_mut < F > ( & mut self , mut f : F ) -> Result < ( ) , Error >
337+ pub fn for_each_while_mut < F , U > ( & mut self , mut f : F ) -> Result < ( ) , EitherError < U , BS > >
335338 where
336339 // TODO remove clone bound when go-interop doesn't require it.
337340 // (If needed without, this bound can be removed by duplicating function signatures)
338341 V : Clone ,
339- F : FnMut ( u64 , & mut ValueMut < ' _ , V > ) -> anyhow :: Result < bool > ,
342+ F : FnMut ( u64 , & mut ValueMut < ' _ , V > ) -> Result < bool , U > ,
340343 {
341344 #[ cfg( not( feature = "go-interop" ) ) ]
342345 {
0 commit comments