@@ -197,8 +197,8 @@ use num::ToPrimitive;
197197use iterators:: HistogramIterator ;
198198
199199/// Min value of a new histogram.
200- /// Equivalent to u64::max_value(), but const functions aren't allowed (yet).
201- /// See https://github.com/rust-lang/rust/issues/24111
200+ /// Equivalent to ` u64::max_value()` , but const functions aren't allowed (yet).
201+ /// See < https://github.com/rust-lang/rust/issues/24111>
202202const ORIGINAL_MIN : u64 = ( -1_i64 >> 63 ) as u64 ;
203203/// Max value of a new histogram.
204204const ORIGINAL_MAX : u64 = 0 ;
@@ -308,6 +308,11 @@ impl<T: Counter> Histogram<T> {
308308 self . total_count
309309 }
310310
311+ /// Returns true if this histogram has no recorded values.
312+ pub fn is_empty ( & self ) -> bool {
313+ self . total_count == 0
314+ }
315+
311316 /// Get the number of buckets used by the histogram to cover the highest trackable value.
312317 ///
313318 /// This method differs from `.len()` in that it does not count the sub buckets within each
@@ -334,7 +339,8 @@ impl<T: Counter> Histogram<T> {
334339 // Calculate the index for the first entry that will be used in the bucket (halfway through
335340 // sub_bucket_count). For bucket_index 0, all sub_bucket_count entries may be used, but
336341 // bucket_base_index is still set in the middle.
337- let bucket_base_index = ( bucket_index as i32 + 1 ) << self . sub_bucket_half_count_magnitude ;
342+ let bucket_base_index =
343+ ( i32:: from ( bucket_index) + 1 ) << self . sub_bucket_half_count_magnitude ;
338344
339345 // Calculate the offset in the bucket. This subtraction will result in a positive value in
340346 // all buckets except the 0th bucket (since a value in that bucket may be less than half
@@ -347,15 +353,15 @@ impl<T: Counter> Histogram<T> {
347353 // sub_bucket_half_count at most) for bucket 0, and bucket_base_index will be halfway into
348354 // bucket 0's sub buckets in that case.
349355 debug_assert ! ( index >= 0 ) ;
350- return index. to_usize ( ) ;
356+ index. to_usize ( )
351357 }
352358
353359 /// Find the bucket the given value should be placed in.
354360 /// If the value is bigger than what this histogram can express, the last valid bucket index
355361 /// is returned instead.
356362 fn index_for_or_last ( & self , value : u64 ) -> usize {
357- return self . index_for ( value)
358- . map_or ( self . last_index ( ) , |i| cmp:: min ( i, self . last_index ( ) ) ) ;
363+ self . index_for ( value)
364+ . map_or ( self . last_index ( ) , |i| cmp:: min ( i, self . last_index ( ) ) )
359365 }
360366
361367 /// Get a mutable reference to the count bucket for the given value, if it is in range.
@@ -606,7 +612,7 @@ impl<T: Counter> Histogram<T> {
606612
607613 /// Clear the contents of this histogram while preserving its statistics and configuration.
608614 pub fn clear ( & mut self ) {
609- for c in self . counts . iter_mut ( ) {
615+ for c in & mut self . counts {
610616 * c = T :: zero ( ) ;
611617 }
612618 self . total_count = 0 ;
@@ -702,7 +708,7 @@ impl<T: Counter> Histogram<T> {
702708 // maintain single unit resolution to 2x 10^decimal_points.
703709
704710 // largest value with single unit resolution, in [2, 200_000].
705- let largest = 2 * 10_u32 . pow ( sigfig as u32 ) ;
711+ let largest = 2 * 10_u32 . pow ( u32:: from ( sigfig ) ) ;
706712
707713 let unit_magnitude = ( low as f64 ) . log2 ( ) . floor ( ) as u8 ;
708714 let unit_magnitude_mask = ( 1 << unit_magnitude) - 1 ;
@@ -714,9 +720,9 @@ impl<T: Counter> Histogram<T> {
714720 // that.
715721 // In [1, 18]. 2^18 > 2 * 10^5 (the largest possible
716722 // largest_value_with_single_unit_resolution)
717- let sub_bucket_count_magnitude = ( largest as f64 ) . log2 ( ) . ceil ( ) as u8 ;
723+ let sub_bucket_count_magnitude = ( f64:: from ( largest ) ) . log2 ( ) . ceil ( ) as u8 ;
718724 let sub_bucket_half_count_magnitude = sub_bucket_count_magnitude - 1 ;
719- let sub_bucket_count = 1_u32 << ( sub_bucket_count_magnitude as u32 ) ;
725+ let sub_bucket_count = 1_u32 << u32 :: from ( sub_bucket_count_magnitude) ;
720726
721727 if unit_magnitude + sub_bucket_count_magnitude > 63 {
722728 // sub_bucket_count entries can't be represented, with unit_magnitude applied, in a
@@ -730,7 +736,7 @@ impl<T: Counter> Histogram<T> {
730736
731737 let sub_bucket_half_count = sub_bucket_count / 2 ;
732738 // sub_bucket_count is always at least 2, so subtraction won't underflow
733- let sub_bucket_mask = ( sub_bucket_count as u64 - 1 ) << unit_magnitude;
739+ let sub_bucket_mask = ( u64:: from ( sub_bucket_count ) - 1 ) << unit_magnitude;
734740
735741 let mut h = Histogram {
736742 auto_resize : false ,
@@ -945,10 +951,10 @@ impl<T: Counter> Histogram<T> {
945951 /// );
946952 /// // etc...
947953 /// ```
948- pub fn iter_quantiles < ' a > (
949- & ' a self ,
954+ pub fn iter_quantiles (
955+ & self ,
950956 ticks_per_half_distance : u32 ,
951- ) -> HistogramIterator < ' a , T , iterators:: quantile:: Iter < ' a , T > > {
957+ ) -> HistogramIterator < T , iterators:: quantile:: Iter < T > > {
952958 // TODO upper bound on ticks per half distance? 2^31 ticks is not useful
953959 iterators:: quantile:: Iter :: new ( self , ticks_per_half_distance)
954960 }
@@ -1008,10 +1014,7 @@ impl<T: Counter> Histogram<T> {
10081014 /// );
10091015 /// assert_eq!(perc.next(), None);
10101016 /// ```
1011- pub fn iter_linear < ' a > (
1012- & ' a self ,
1013- step : u64 ,
1014- ) -> HistogramIterator < ' a , T , iterators:: linear:: Iter < ' a , T > > {
1017+ pub fn iter_linear ( & self , step : u64 ) -> HistogramIterator < T , iterators:: linear:: Iter < T > > {
10151018 iterators:: linear:: Iter :: new ( self , step)
10161019 }
10171020
@@ -1049,11 +1052,7 @@ impl<T: Counter> Histogram<T> {
10491052 /// );
10501053 /// assert_eq!(perc.next(), None);
10511054 /// ```
1052- pub fn iter_log < ' a > (
1053- & ' a self ,
1054- start : u64 ,
1055- exp : f64 ,
1056- ) -> HistogramIterator < ' a , T , iterators:: log:: Iter < ' a , T > > {
1055+ pub fn iter_log ( & self , start : u64 , exp : f64 ) -> HistogramIterator < T , iterators:: log:: Iter < T > > {
10571056 iterators:: log:: Iter :: new ( self , start, exp)
10581057 }
10591058
@@ -1091,7 +1090,7 @@ impl<T: Counter> Histogram<T> {
10911090 /// );
10921091 /// assert_eq!(perc.next(), None);
10931092 /// ```
1094- pub fn iter_recorded < ' a > ( & ' a self ) -> HistogramIterator < ' a , T , iterators:: recorded:: Iter > {
1093+ pub fn iter_recorded ( & self ) -> HistogramIterator < T , iterators:: recorded:: Iter > {
10951094 iterators:: recorded:: Iter :: new ( self )
10961095 }
10971096
@@ -1150,7 +1149,7 @@ impl<T: Counter> Histogram<T> {
11501149 /// );
11511150 /// assert_eq!(perc.next(), Some(IterationValue::new(10, 1.0, 1.0, 0, 0)));
11521151 /// ```
1153- pub fn iter_all < ' a > ( & ' a self ) -> HistogramIterator < ' a , T , iterators:: all:: Iter > {
1152+ pub fn iter_all ( & self ) -> HistogramIterator < T , iterators:: all:: Iter > {
11541153 iterators:: all:: Iter :: new ( self )
11551154 }
11561155
@@ -1263,7 +1262,7 @@ impl<T: Counter> Histogram<T> {
12631262 for i in 0 ..self . counts . len ( ) {
12641263 // Direct indexing is safe; indexes must reside in counts array.
12651264 // TODO overflow
1266- total_to_current_index = total_to_current_index + self . counts [ i] . as_u64 ( ) ;
1265+ total_to_current_index += self . counts [ i] . as_u64 ( ) ;
12671266 if total_to_current_index >= count_at_quantile {
12681267 let value_at_index = self . value_for ( i) ;
12691268 return if quantile == 0.0 {
@@ -1402,7 +1401,7 @@ impl<T: Counter> Histogram<T> {
14021401 /// any two equivalent values are counted in a common total count.
14031402 pub fn equivalent_range ( & self , value : u64 ) -> u64 {
14041403 let bucket_index = self . bucket_for ( value) ;
1405- 1_u64 << self . unit_magnitude + bucket_index
1404+ 1_u64 << ( self . unit_magnitude + bucket_index)
14061405 }
14071406
14081407 // ********************************************************************************************
@@ -1444,7 +1443,7 @@ impl<T: Counter> Histogram<T> {
14441443
14451444 /// Returns count at index, or None if out of bounds
14461445 fn count_at_index ( & self , index : usize ) -> Option < T > {
1447- self . counts . get ( index) . map ( | & t| t )
1446+ self . counts . get ( index) . cloned ( )
14481447 }
14491448
14501449 /// Returns an error if the index doesn't exist.
@@ -1492,14 +1491,15 @@ impl<T: Counter> Histogram<T> {
14921491 // However, the resulting shift may overflow given bogus input, e.g. if unit magnitude is
14931492 // large and the input sub_bucket_index is for an entry in the counts index that shouldn't
14941493 // be used (because this calculation will overflow).
1495- ( sub_bucket_index as u64 ) << ( bucket_index + self . unit_magnitude )
1494+ u64 :: from ( sub_bucket_index) << ( bucket_index + self . unit_magnitude )
14961495 }
14971496
14981497 /// Find the number of buckets needed such that `value` is representable.
14991498 fn buckets_to_cover ( & self , value : u64 ) -> u8 {
15001499 // Shift won't overflow because sub_bucket_magnitude + unit_magnitude <= 63.
15011500 // the k'th bucket can express from 0 * 2^k to sub_bucket_count * 2^k in units of 2^k
1502- let mut smallest_untrackable_value = ( self . sub_bucket_count as u64 ) << self . unit_magnitude ;
1501+ let mut smallest_untrackable_value =
1502+ u64:: from ( self . sub_bucket_count ) << self . unit_magnitude ;
15031503
15041504 // always have at least 1 bucket
15051505 let mut buckets_needed = 1 ;
@@ -1524,7 +1524,7 @@ impl<T: Counter> Histogram<T> {
15241524 /// Or, equivalently, we need 1 more bucket to capture the max value if we consider the
15251525 /// sub-bucket length to be halved.
15261526 fn num_bins ( & self , number_of_buckets : u8 ) -> u32 {
1527- ( number_of_buckets as u32 + 1 ) * ( self . sub_bucket_half_count )
1527+ ( u32:: from ( number_of_buckets ) + 1 ) * ( self . sub_bucket_half_count )
15281528 }
15291529
15301530 /// Resize the underlying counts array such that it can cover the given `high` value.
0 commit comments