use num::traits::SaturatingAdd;
use std::ops::Add;
pub fn create_saturating_add_fn<T: SaturatingAdd>() -> Box<dyn Fn(&T, &T) -> T + Send> {
Box::new(|value1, value2| value1.saturating_add(value2))
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct SumAndCount {
pub sum: u32,
pub count: u32,
}
impl SumAndCount {
pub fn avg(&self) -> f64 {
self.sum as f64 / self.count as f64
}
}
impl Add for SumAndCount {
type Output = Self;
fn add(self, other: Self) -> Self {
Self { sum: self.sum + other.sum, count: self.count + other.count }
}
}
impl SaturatingAdd for SumAndCount {
fn saturating_add(&self, other: &Self) -> Self {
Self {
sum: self.sum.saturating_add(other.sum),
count: self.count.saturating_add(other.count),
}
}
}