diff --git a/src/iterators/mod.rs b/src/iterators/mod.rs index 973d304..db20240 100644 --- a/src/iterators/mod.rs +++ b/src/iterators/mod.rs @@ -74,7 +74,7 @@ pub struct HistogramIterator<'a, T: 'a + Counter, P: PickyIterator> { count_since_last_iteration: u64, count_at_index: T, current_index: usize, - last_picked_index: usize, + last_picked_index: Option, max_value_index: usize, fresh: bool, ended: bool, @@ -152,7 +152,7 @@ impl<'a, T: Counter, P: PickyIterator> HistogramIterator<'a, T, P> { count_since_last_iteration: 0, count_at_index: T::zero(), current_index: 0, - last_picked_index: 0, + last_picked_index: None, max_value_index: h.index_for(h.max()).expect("Either 0 or an existing index"), picker, fresh: true, @@ -186,7 +186,7 @@ where } // Have we already picked the index with the last non-zero count in the histogram? - if self.last_picked_index >= self.max_value_index { + if self.last_picked_index >= Some(self.max_value_index) { // is the picker done? if !self.picker.more(self.current_index) { self.ended = true; @@ -243,7 +243,7 @@ where // step multiple times without advancing the index. self.count_since_last_iteration = 0; - self.last_picked_index = self.current_index; + self.last_picked_index = Some(self.current_index); return Some(val); } diff --git a/tests/histogram.rs b/tests/histogram.rs index 9235eed..d4e3f38 100644 --- a/tests/histogram.rs +++ b/tests/histogram.rs @@ -560,3 +560,10 @@ fn subtract_underflow_guarded_by_per_value_count_check() { h.subtract(h2).unwrap_err() ); } + +#[test] +fn recorded_only_zeros() { + let mut h = Histogram::::new(1).unwrap(); + h += 0; + assert_eq!(h.iter_recorded().count(), 1); +}