Skip to content

Commit f22e8dd

Browse files
committed
clippy clean
1 parent 157beed commit f22e8dd

File tree

13 files changed

+121
-104
lines changed

13 files changed

+121
-104
lines changed

clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
doc-valid-idents = ["HdrHistogram", "HdrSample"]

examples/cli.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,39 +230,39 @@ fn quantiles<R: BufRead, W: Write>(
230230
// subcommands, the convenience seems worth it.
231231
#[derive(Debug)]
232232
enum CliError {
233-
IoError(io::Error),
234-
HistogramSerializeError(V2SerializeError),
235-
HistogramSerializeCompressedError(V2DeflateSerializeError),
236-
HistogramDeserializeError(DeserializeError),
237-
HistogramRecordError(RecordError),
233+
Io(io::Error),
234+
HistogramSerialize(V2SerializeError),
235+
HistogramSerializeCompressed(V2DeflateSerializeError),
236+
HistogramDeserialize(DeserializeError),
237+
HistogramRecord(RecordError),
238238
}
239239

240240
impl From<io::Error> for CliError {
241241
fn from(e: io::Error) -> Self {
242-
CliError::IoError(e)
242+
CliError::Io(e)
243243
}
244244
}
245245

246246
impl From<V2SerializeError> for CliError {
247247
fn from(e: V2SerializeError) -> Self {
248-
CliError::HistogramSerializeError(e)
248+
CliError::HistogramSerialize(e)
249249
}
250250
}
251251

252252
impl From<V2DeflateSerializeError> for CliError {
253253
fn from(e: V2DeflateSerializeError) -> Self {
254-
CliError::HistogramSerializeCompressedError(e)
254+
CliError::HistogramSerializeCompressed(e)
255255
}
256256
}
257257

258258
impl From<RecordError> for CliError {
259259
fn from(e: RecordError) -> Self {
260-
CliError::HistogramRecordError(e)
260+
CliError::HistogramRecord(e)
261261
}
262262
}
263263

264264
impl From<DeserializeError> for CliError {
265265
fn from(e: DeserializeError) -> Self {
266-
CliError::HistogramDeserializeError(e)
266+
CliError::HistogramDeserialize(e)
267267
}
268268
}

src/core/counter.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,33 @@ pub trait Counter
2424
impl Counter for u8 {
2525
#[inline]
2626
fn as_f64(&self) -> f64 {
27-
*self as f64
27+
f64::from(*self)
2828
}
2929
#[inline]
3030
fn as_u64(&self) -> u64 {
31-
*self as u64
31+
u64::from(*self)
3232
}
3333
}
3434

3535
impl Counter for u16 {
3636
#[inline]
3737
fn as_f64(&self) -> f64 {
38-
*self as f64
38+
f64::from(*self)
3939
}
4040
#[inline]
4141
fn as_u64(&self) -> u64 {
42-
*self as u64
42+
u64::from(*self)
4343
}
4444
}
4545

4646
impl Counter for u32 {
4747
#[inline]
4848
fn as_f64(&self) -> f64 {
49-
*self as f64
49+
f64::from(*self)
5050
}
5151
#[inline]
5252
fn as_u64(&self) -> u64 {
53-
*self as u64
53+
u64::from(*self)
5454
}
5555
}
5656

src/iterators/all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct Iter {
99

1010
impl Iter {
1111
/// Construct a new full iterator. See `Histogram::iter_all` for details.
12-
pub fn new<'a, T: Counter>(hist: &'a Histogram<T>) -> HistogramIterator<'a, T, Iter> {
12+
pub fn new<T: Counter>(hist: &Histogram<T>) -> HistogramIterator<T, Iter> {
1313
HistogramIterator::new(hist, Iter { visited: None })
1414
}
1515
}

src/iterators/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,10 @@ where
220220
) {
221221
let quantile = self.total_count_to_index as f64 / self.hist.count() as f64;
222222
let val = IterationValue {
223-
value_iterated_to: metadata.value_iterated_to.unwrap_or(
223+
value_iterated_to: metadata.value_iterated_to.unwrap_or_else(|| {
224224
self.hist
225-
.highest_equivalent(self.hist.value_for(self.current_index)),
226-
),
225+
.highest_equivalent(self.hist.value_for(self.current_index))
226+
}),
227227
quantile,
228228
quantile_iterated_to: metadata.quantile_iterated_to.unwrap_or(quantile),
229229
count_at_value: self.hist

src/iterators/quantile.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl<'a, T: 'a + Counter> Iter<'a, T> {
3434
}
3535

3636
impl<'a, T: 'a + Counter> PickyIterator<T> for Iter<'a, T> {
37+
#[cfg_attr(feature = "cargo-clippy", allow(float_cmp))]
3738
fn pick(&mut self, _: usize, running_total: u64, count_at_index: T) -> Option<PickMetadata> {
3839
if count_at_index == T::zero() {
3940
return None;
@@ -132,7 +133,7 @@ impl<'a, T: 'a + Counter> PickyIterator<T> for Iter<'a, T> {
132133
// tick, so we add an extra power of two to get ticks per whole distance.
133134
// Use u64 math so that there's less risk of overflow with large numbers of ticks and data
134135
// that ends up needing large numbers of halvings.
135-
let total_ticks = (self.ticks_per_half_distance as u64)
136+
let total_ticks = u64::from(self.ticks_per_half_distance)
136137
.checked_mul(
137138
1_u64
138139
.checked_shl(num_halvings + 1)

src/iterators/recorded.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ impl Iter {
1616

1717
impl<T: Counter> PickyIterator<T> for Iter {
1818
fn pick(&mut self, index: usize, _: u64, count_at_index: T) -> Option<PickMetadata> {
19-
if count_at_index != T::zero() {
20-
// have we visited before?
21-
if self.visited.map(|i| i != index).unwrap_or(true) {
22-
self.visited = Some(index);
23-
return Some(PickMetadata::new(None, None));
24-
}
19+
if count_at_index != T::zero() && self.visited.map(|i| i != index).unwrap_or(true) {
20+
self.visited = Some(index);
21+
return Some(PickMetadata::new(None, None));
2522
}
2623
None
2724
}

src/lib.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ use num::ToPrimitive;
197197
use 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>
202202
const ORIGINAL_MIN: u64 = (-1_i64 >> 63) as u64;
203203
/// Max value of a new histogram.
204204
const 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

Comments
 (0)