From 48d83552bd76edd8feb03c18f1f08f4f87ee68e9 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 4 Nov 2019 16:13:59 +0800 Subject: [PATCH] make decimator generic --- src/noptica.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) 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 {