LCOV - code coverage report
Current view: top level - libs/utils/src - accum.rs (source / functions) Coverage Total Hit
Test: fcf55189004bd3119eed75e2873a97da8078700c.info Lines: 0.0 % 8 0
Test Date: 2024-06-25 12:07:31 Functions: 0.0 % 1 0

            Line data    Source code
       1              : /// A helper to "accumulate" a value similar to `Iterator::reduce`, but lets you
       2              : /// feed the accumulated values by calling the 'accum' function, instead of having an
       3              : /// iterator.
       4              : ///
       5              : /// For example, to calculate the smallest value among some integers:
       6              : ///
       7              : /// ```
       8              : /// use utils::accum::Accum;
       9              : ///
      10              : /// let values = [1, 2, 3];
      11              : ///
      12              : /// let mut min_value: Accum<u32> = Accum(None);
      13              : /// for new_value in &values {
      14              : ///     min_value.accum(std::cmp::min, *new_value);
      15              : /// }
      16              : ///
      17              : /// assert_eq!(min_value.0.unwrap(), 1);
      18              : /// ```
      19              : pub struct Accum<T>(pub Option<T>);
      20              : impl<T: Copy> Accum<T> {
      21            0 :     pub fn accum<F>(&mut self, func: F, new_value: T)
      22            0 :     where
      23            0 :         F: FnOnce(T, T) -> T,
      24            0 :     {
      25              :         // If there is no previous value, just store the new value.
      26              :         // Otherwise call the function to decide which one to keep.
      27            0 :         self.0 = Some(if let Some(accum) = self.0 {
      28            0 :             func(accum, new_value)
      29              :         } else {
      30            0 :             new_value
      31              :         });
      32            0 :     }
      33              : }
        

Generated by: LCOV version 2.1-beta