diff --git a/src/noptica.rs b/src/noptica.rs index 54f6e53..c25f15f 100644 --- a/src/noptica.rs +++ b/src/noptica.rs @@ -105,27 +105,32 @@ impl PositionTracker { } } -pub struct Decimator { - accumulator: i64, +pub struct Decimator { + accumulator: T, current_count: u32, max_count: u32 } -impl Decimator { - pub fn new(max_count: u32) -> Decimator { +impl Decimator { + pub fn new(max_count: u32) -> Decimator where T: std::convert::From { Decimator { - accumulator: 0, + accumulator: T::from(0), current_count: 0, max_count: max_count } } - pub fn input(&mut self, data: i64) -> Option { + pub fn input(&mut self, data: T) -> Option + where T: Copy + + std::convert::From + + std::convert::From<::Output> + + std::ops::AddAssign + + std::ops::Div { self.accumulator += data; self.current_count += 1; if self.current_count == self.max_count { - let average = self.accumulator/(self.current_count as i64); - self.accumulator = 0; + let average = T::from(self.accumulator/T::from(self.current_count)); + self.accumulator = T::from(0); self.current_count = 0; Some(average) } else {