@@ -284,26 +284,42 @@ where
284
284
/// map.set(4, "Four".to_owned()).unwrap();
285
285
///
286
286
/// let mut values: Vec<(u64, String)> = Vec::new();
287
- /// map.for_each (|i, v| {
287
+ /// map.try_for_each (|i, v| {
288
288
/// values.push((i, v.clone()));
289
289
/// Ok::<_, ()>(())
290
290
/// }).unwrap();
291
291
/// assert_eq!(&values, &[(1, "One".to_owned()), (4, "Four".to_owned())]);
292
292
/// ```
293
293
#[ 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 > >
295
295
where
296
296
F : FnMut ( u64 , & V ) -> Result < ( ) , U > ,
297
297
{
298
- self . for_each_while ( |i, x| {
298
+ self . try_for_each_while ( |i, x| {
299
299
f ( i, x) ?;
300
300
Ok ( true )
301
301
} )
302
302
}
303
303
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
+
304
320
/// Iterates over each value in the Amt and runs a function on the values, for as long as that
305
321
/// 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 > >
307
323
where
308
324
F : FnMut ( u64 , & V ) -> Result < bool , U > ,
309
325
{
@@ -319,22 +335,51 @@ where
319
335
. map ( |_| ( ) )
320
336
}
321
337
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
+
322
351
/// Iterates over each value in the Amt and runs a function on the values that allows modifying
323
352
/// 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 > >
325
354
where
326
355
V : Clone ,
327
356
F : FnMut ( u64 , & mut ValueMut < ' _ , V > ) -> Result < ( ) , U > ,
328
357
{
329
- self . for_each_while_mut ( |i, x| {
358
+ self . try_for_each_while_mut ( |i, x| {
330
359
f ( i, x) ?;
331
360
Ok ( true )
332
361
} )
333
362
}
334
363
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
+
335
377
/// Iterates over each value in the Amt and runs a function on the values that allows modifying
336
378
/// 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 > >
338
383
where
339
384
// TODO remove clone bound when go-interop doesn't require it.
340
385
// (If needed without, this bound can be removed by duplicating function signatures)
@@ -392,4 +437,20 @@ where
392
437
Ok ( ( ) )
393
438
}
394
439
}
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
+ }
395
456
}
0 commit comments