LCOV - differential code coverage report
Current view: top level - libs/utils/src - accum.rs (source / functions) Coverage Total Hit UBC
Current: f6946e90941b557c917ac98cd5a7e9506d180f3e.info Lines: 0.0 % 8 0 8
Current Date: 2023-10-19 02:04:12 Functions: 0.0 % 1 0 1
Baseline: c8637f37369098875162f194f92736355783b050.info
Baseline Date: 2023-10-18 20:25:20

           TLA  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 UBC           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