1
1
// Copyright 2019-2022 ChainSafe Systems
2
2
// SPDX-License-Identifier: Apache-2.0, MIT
3
3
4
- use anyhow:: anyhow;
5
4
use cid:: multihash:: Code ;
6
5
use cid:: Cid ;
7
6
use fvm_ipld_blockstore:: Blockstore ;
@@ -11,6 +10,7 @@ use fvm_ipld_encoding::CborStore;
11
10
use itertools:: sorted;
12
11
13
12
use super :: ValueMut ;
13
+ use crate :: error:: EitherError ;
14
14
use crate :: node:: { CollapsedNode , Link } ;
15
15
use crate :: {
16
16
init_sized_vec, nodes_for_height, Error , Node , Root , DEFAULT_BIT_WIDTH , MAX_HEIGHT , MAX_INDEX ,
72
72
}
73
73
74
74
/// 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 > > {
76
76
// Load root bytes from database
77
77
let root: Root < V > = block_store
78
78
. get_cbor ( cid) ?
97
97
}
98
98
99
99
/// 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 > > {
101
104
let mut t = Self :: new ( block_store) ;
102
105
103
106
t. batch_set ( vals) ?;
@@ -106,7 +109,7 @@ where
106
109
}
107
110
108
111
/// 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 > > {
110
113
if i > MAX_INDEX {
111
114
return Err ( Error :: OutOfRange ( i) ) ;
112
115
}
@@ -121,7 +124,7 @@ where
121
124
}
122
125
123
126
/// 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 > > {
125
128
if i > MAX_INDEX {
126
129
return Err ( Error :: OutOfRange ( i) ) ;
127
130
}
@@ -163,7 +166,7 @@ where
163
166
164
167
/// Batch set (naive for now)
165
168
// 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 > > {
167
170
for ( i, val) in ( 0u64 ..) . zip ( vals) {
168
171
self . set ( i, val) ?;
169
172
}
@@ -172,7 +175,7 @@ where
172
175
}
173
176
174
177
/// 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 > > {
176
179
if i > MAX_INDEX {
177
180
return Err ( Error :: OutOfRange ( i) ) ;
178
181
}
@@ -243,23 +246,23 @@ where
243
246
& mut self ,
244
247
iter : impl IntoIterator < Item = u64 > ,
245
248
strict : bool ,
246
- ) -> Result < bool , Error > {
249
+ ) -> Result < bool , Error < BS > > {
247
250
// TODO: optimize this
248
251
let mut modified = false ;
249
252
250
253
// Iterate sorted indices. Sorted to safely optimize later.
251
254
for i in sorted ( iter) {
252
255
let found = self . delete ( i) ?. is_none ( ) ;
253
256
if strict && found {
254
- return Err ( anyhow ! ( "no such index {} in Amt for batch delete" , i ) . into ( ) ) ;
257
+ return Err ( Error :: BatchDelteNotFound ( i ) ) ;
255
258
}
256
259
modified |= found;
257
260
}
258
261
Ok ( modified)
259
262
}
260
263
261
264
/// 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 > > {
263
266
self . root . node . flush ( & self . block_store ) ?;
264
267
Ok ( self . block_store . put_cbor ( & self . root , Code :: Blake2b256 ) ?)
265
268
}
@@ -283,14 +286,14 @@ where
283
286
/// let mut values: Vec<(u64, String)> = Vec::new();
284
287
/// map.for_each(|i, v| {
285
288
/// values.push((i, v.clone()));
286
- /// Ok(())
289
+ /// Ok::<_, ()> (())
287
290
/// }).unwrap();
288
291
/// assert_eq!(&values, &[(1, "One".to_owned()), (4, "Four".to_owned())]);
289
292
/// ```
290
293
#[ 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 > >
292
295
where
293
- F : FnMut ( u64 , & V ) -> anyhow :: Result < ( ) > ,
296
+ F : FnMut ( u64 , & V ) -> Result < ( ) , U > ,
294
297
{
295
298
self . for_each_while ( |i, x| {
296
299
f ( i, x) ?;
@@ -300,9 +303,9 @@ where
300
303
301
304
/// Iterates over each value in the Amt and runs a function on the values, for as long as that
302
305
/// 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 > >
304
307
where
305
- F : FnMut ( u64 , & V ) -> anyhow :: Result < bool > ,
308
+ F : FnMut ( u64 , & V ) -> Result < bool , U > ,
306
309
{
307
310
self . root
308
311
. node
@@ -318,10 +321,10 @@ where
318
321
319
322
/// Iterates over each value in the Amt and runs a function on the values that allows modifying
320
323
/// 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 > >
322
325
where
323
326
V : Clone ,
324
- F : FnMut ( u64 , & mut ValueMut < ' _ , V > ) -> anyhow :: Result < ( ) > ,
327
+ F : FnMut ( u64 , & mut ValueMut < ' _ , V > ) -> Result < ( ) , U > ,
325
328
{
326
329
self . for_each_while_mut ( |i, x| {
327
330
f ( i, x) ?;
@@ -331,12 +334,12 @@ where
331
334
332
335
/// Iterates over each value in the Amt and runs a function on the values that allows modifying
333
336
/// 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 > >
335
338
where
336
339
// TODO remove clone bound when go-interop doesn't require it.
337
340
// (If needed without, this bound can be removed by duplicating function signatures)
338
341
V : Clone ,
339
- F : FnMut ( u64 , & mut ValueMut < ' _ , V > ) -> anyhow :: Result < bool > ,
342
+ F : FnMut ( u64 , & mut ValueMut < ' _ , V > ) -> Result < bool , U > ,
340
343
{
341
344
#[ cfg( not( feature = "go-interop" ) ) ]
342
345
{
0 commit comments