@@ -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}
0 commit comments