1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
extern crate libc;

use self::libc::{int64_t, int32_t, c_double, c_int, c_void, c_char, size_t};
use std::str;
use std::ffi::CStr;
use std::marker::PhantomData;
use std::fmt;
use std::error::Error;
use std::mem;
use std::ptr;
use std::result;

pub use super::Result;

#[cfg(test)]
pub mod test;

#[repr(C)]
#[derive(Debug)]
struct hdr_histogram {
    lowest_trackable_value: int64_t,
    highest_trackable_value: int64_t,
    unit_magnitude: int32_t,
    significant_figures: int32_t,
    sub_bucket_half_count_magnitude: int32_t,
    sub_bucket_half_count: int32_t,
    sub_bucket_mask: int64_t,
    sub_bucket_count: int32_t,
    bucket_count: int32_t,
    min_value: int64_t,
    max_value: int64_t,
    normalizing_index_offset: int32_t,
    conversion_ratio: c_double,
    counts_len: int32_t,
    total_count: int64_t,
    counts: [int64_t; 0],
}

#[repr(C)]
#[derive(Default,Debug,Copy,Clone)]
struct hdr_histogram_bucket_config {
    lowest_trackable_value: int64_t,
    highest_trackable_value: int64_t,
    unit_magnitude: int64_t,
    significant_figures: int64_t,
    sub_bucket_half_count_magnitude: int32_t,
    sub_bucket_half_count: int32_t,
    sub_bucket_mask: int64_t,
    sub_bucket_count: int32_t,
    bucket_count: int32_t,
    counts_len: int32_t,
}
        

#[repr(C)]
struct hdr_iter_percentiles {
    seen_last_value: bool,
    ticks_per_half_histance: int32_t,
    percentile_to_iterate_to: c_double,
    percentile: c_double,
}

#[repr(C)]
struct hdr_iter_recorded {
    count_added_in_this_iteration_step: int64_t,
}

#[repr(C)]
struct hdr_iter_linear {
    value_units_per_bucket: int64_t,
    count_added_in_this_iteration_step: int64_t,
    next_value_reporting_level: int64_t,
    next_value_reporting_level_lowest_equivalent: int64_t,
}

#[repr(C)]
struct hdr_iter_log {
    log_base: c_double,
    count_added_in_this_iteration_step: int64_t,
    next_value_reporting_level: int64_t,
    next_value_reporting_level_lowest_equivalent: int64_t,
}

#[repr(C)]
#[derive(Debug)]
struct hdr_iter {
    h: *const hdr_histogram,

    /** raw index into the counts array */
    counts_index: int32_t,
    /** value directly from array for the current counts_index */
    count: int64_t,
    /** sum of all of the counts up to and including the count at this index */
    cumulative_count: int64_t,
    /** The current value based on counts_index */
    value: int64_t,
    highest_equivalent_value: int64_t,
    lowest_equivalent_value: int64_t,
    median_equivalent_value: int64_t,
    value_iterated_from: int64_t,
    value_iterated_to: int64_t,

    union: [int64_t; 4],        // hdr_iter_log/linear

    _next_fp: *const c_void,    // function pointer
}

impl Default for hdr_iter {
    fn default() -> Self { unsafe { mem::zeroed() } }
}

#[allow(dead_code)]
extern {
    fn hdr_init(lowest_trackable_value: int64_t, highest_trackable_value: int64_t,
                significant_figures: c_int, res: *mut *mut hdr_histogram) -> c_int;
    fn hdr_reset(h: *mut hdr_histogram);
    fn hdr_get_memory_size(h: *const hdr_histogram) -> size_t;

    fn hdr_record_value(h: *mut hdr_histogram, value: int64_t) -> bool;
    fn hdr_record_values(h: *mut hdr_histogram, value: int64_t, count: int64_t) -> bool;
    fn hdr_record_corrected_value(h: *mut hdr_histogram, value: int64_t, expected_interval: int64_t) -> bool;
    fn hdr_record_corrected_values(h: *mut hdr_histogram, value: int64_t, count: int64_t,
                                   expected_interval: int64_t) -> bool;

    fn hdr_add(h: *mut hdr_histogram, from: *const hdr_histogram) -> int64_t;
    fn hdr_add_while_correcting_for_coordinated_omission(h: *mut hdr_histogram, from: *const hdr_histogram,
                                                         expected_interval: int64_t) -> int64_t;

    fn hdr_min(h: *const hdr_histogram) -> int64_t;
    fn hdr_max(h: *const hdr_histogram) -> int64_t;

    fn hdr_value_at_percentile(h: *const hdr_histogram, percentile: c_double) -> int64_t;

    fn hdr_stddev(h: *const hdr_histogram) -> c_double;
    fn hdr_mean(h: *const hdr_histogram) -> c_double;

    fn hdr_values_are_equivalent(h: *const hdr_histogram, a: int64_t, b: int64_t) -> bool;
    fn hdr_lowest_equivalent_value(h: *const hdr_histogram, value: int64_t) -> int64_t;
    fn hdr_count_at_value(h: *const hdr_histogram, value: int64_t) -> int64_t;
    fn hdr_count_at_index(h: *const hdr_histogram, index: int32_t) -> int64_t;
    fn hdr_value_at_index(h: *const hdr_histogram, index: int32_t) -> int64_t;
    
    fn hdr_calculate_bucket_config(lowest_trackable_value: int64_t, highest_trackable_value: int64_t,
                                   significant_figures: c_int, cfg: *mut hdr_histogram_bucket_config) -> c_int;
    fn hdr_init_preallocated(h: *mut hdr_histogram, cfg: *const hdr_histogram_bucket_config);

    fn hdr_shift_values_left(h: *mut hdr_histogram, binary_orders_of_magnitude: int32_t) -> bool;
    fn hdr_shift_values_right(h: *mut hdr_histogram, shift: int32_t) -> bool;
    fn hdr_size_of_equivalent_value_range(h: *const hdr_histogram, value: int64_t) -> int64_t;
    fn hdr_next_non_equivalent_value(h: *const hdr_histogram, value: int64_t) -> int64_t;
    fn hdr_median_equivalent_value(h: *const hdr_histogram, value: int64_t) -> int64_t;

    fn hdr_iter_init(iter: *mut hdr_iter, h: *const hdr_histogram);
    fn hdr_iter_percentile_init(iter: *mut hdr_iter, h: *const hdr_histogram, ticks_per_half_distance: int32_t);
    fn hdr_iter_recorded_init(iter: *mut hdr_iter, h: *const hdr_histogram);
    fn hdr_iter_linear_init(iter: *mut hdr_iter, h: *const hdr_histogram, value_units_per_bucket: int64_t);
    fn hdr_iter_log_init(iter: *mut hdr_iter, h: *const hdr_histogram, value_units_per_bucket: int64_t, log_base: c_double);

    fn hdr_iter_next(iter: *mut hdr_iter) -> bool;

    // hdr_histogram_log

    fn hdr_log_encode(histogram: *const hdr_histogram, encoded_histogram: *mut *mut c_char) -> c_int;
    fn hdr_log_decode(histogram: *mut *mut hdr_histogram, base64_histogram: *const c_char, base64_len: size_t) -> c_int;

    fn hdr_strerror(errnum: c_int) -> *const c_char;
}

/// Catch-all error return.
///
/// Something went wrong.
#[derive(Copy,Clone,Debug)]
pub struct HistogramErr(&'static str);

impl fmt::Display for HistogramErr {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
        let &HistogramErr(msg) = self;
        msg.fmt(fmt)
    }
}

impl Error for HistogramErr {
    fn description(&self) -> &str { let &HistogramErr(msg) = self; msg }
}

/// Instance of a Histogram.
pub struct Histogram {
    histo: *mut hdr_histogram,
    owned: bool,
}

unsafe impl Send for Histogram {}

impl Histogram {
    /// Create a new Histogram instance. `lowest_trackable_value`..`highest_trackable_value` defines
    /// the range of values in the `Histogram`. `lowest_trackable_value` must be >= 1.
    /// `significant_figures` defines the precision, and must be in the range 1..5.
    ///
    /// `HistogramErr` is the catch-all failure case. It doesn't report much detail because the
    /// underlying library doesn't.
    ///
    /// # Example
    /// ```
    /// # use hdrhistogram::Histogram;
    /// let mut h = Histogram::init(1, 100000, 2).unwrap();
    /// h.record_value(10);  // record a single count of '10'
    /// ```
    pub fn init(lowest_trackable_value: u64, highest_trackable_value: u64, significant_figures: u32) -> Result<Histogram> {
        let mut histo : *mut hdr_histogram = ptr::null_mut();
        let r = unsafe {
            hdr_init(lowest_trackable_value as int64_t, highest_trackable_value as int64_t,
                     significant_figures as int32_t, &mut histo)
        };

        if r != 0 || histo.is_null() {
            Err(HistogramErr("Histogram init failed"))
        } else {
            Ok(Histogram { histo: histo, owned: true })
        }
    }

    /// Zero all histogram state in place.
    pub fn reset(&mut self) { unsafe { hdr_reset(self.histo) } }

    /// Record a specific value. Returns true if successful.
    ///
    /// ```
    /// # use hdrhistogram::Histogram;
    /// # let mut h = Histogram::init(1, 10, 1).unwrap();
    /// h.record_value(5);
    /// assert_eq!(h.total_count(), 1);
    /// ```
    #[inline]
    pub fn record_value(&mut self, value: u64) -> bool {
        unsafe { hdr_record_value(self.histo, value as int64_t) }
    }

    /// Record multiple counts of a specific value. Returns true if successful.
    ///
    /// ```
    /// # use hdrhistogram::Histogram;
    /// # let mut h = Histogram::init(1, 10, 1).unwrap();
    /// h.record_values(5, 10);
    /// assert_eq!(h.total_count(), 10);
    /// ```
    #[inline]
    pub fn record_values(&mut self, value: u64, count: u64) -> bool {
        unsafe { hdr_record_values(self.histo, value as int64_t, count as int64_t) }
    }

    /// Record a value, correcting for coordinated omission. This should be used when accumulating
    /// latency measurements taked on a regular timebase (expected_interval). Any latency that's
    /// well above that interval implies some kind of outage in which sampled were lost. This
    /// corrects for those lost samples to preserve the integrity of the overall statistics.
    #[inline]
    pub fn record_corrected_value(&mut self, value: u64, expected_interval: u64) -> bool {
        unsafe { hdr_record_corrected_value(self.histo, value as int64_t, expected_interval as int64_t) }
    }

    /// As with `record_corrected_value()` but multiple counts of the value.
    #[inline]
    pub fn record_corrected_values(&mut self, value: u64, count: u64, expected_interval: u64) -> bool {
        unsafe { hdr_record_corrected_values(self.histo, value as int64_t, count as int64_t, expected_interval as int64_t) }
    }

    /// Sum two histograms, modifying `self` in place. Returns the number of samples dropped;
    /// samples will be dropped if they're out of the range `lowest_trackable_value
    /// .. highest_trackable_value`.
    pub fn add(&mut self, other: &Histogram) -> u64 {
        unsafe { hdr_add(self.histo, other.histo) as u64 }
    }

    /// As with `add` but corrects of potential lost samples while doing periodic latency
    /// measurements, as in `record_corrected_value`.  Only one correction should be applied.
    pub fn add_while_correcting_for_coordinated_omission(&mut self, other: &Histogram, expected_interval: u64) -> u64 {
        unsafe {
            hdr_add_while_correcting_for_coordinated_omission(self.histo, other.histo, expected_interval as int64_t) as u64
        }
    }

    /// Total of all counters
    pub fn total_count(&self) -> u64 { unsafe { (*self.histo).total_count as u64 } }

    /// Smallest recorded value.
    ///
    /// ```
    /// # use hdrhistogram::Histogram;
    /// # let mut h = Histogram::init(1, 10, 1).unwrap();
    /// h.record_value(1);
    /// h.record_value(5);
    /// assert_eq!(h.min(), 1);
    /// ```
    pub fn min(&self) -> u64 {
        unsafe { hdr_min(self.histo) as u64 }
    }

    /// Largest recorded value.
    ///
    /// ```
    /// # use hdrhistogram::Histogram;
    /// # let mut h = Histogram::init(1, 10, 1).unwrap();
    /// h.record_value(1);
    /// h.record_value(5);
    /// assert_eq!(h.max(), 5);
    /// ```
    pub fn max(&self) -> u64 {
        unsafe { hdr_max(self.histo) as u64 }
    }

    /// Value at a particular percentile (0-100).
    ///
    /// ```
    /// # use hdrhistogram::Histogram;
    /// # let mut h = Histogram::init(1, 100, 2).unwrap();
    /// h.record_values(20, 10);
    /// h.record_value(40);
    /// assert_eq!(h.value_at_percentile(50.0), 20);
    /// assert_eq!(h.value_at_percentile(99.0), 40);
    pub fn value_at_percentile(&self, percentile: f64) -> u64 {
        unsafe { hdr_value_at_percentile(self.histo, percentile) as u64 }
    }

    /// Standard deviation of values.
    pub fn stddev(&self) -> f64 {
        unsafe { hdr_stddev(self.histo) }
    }

    /// Mean of values.
    pub fn mean(&self) -> f64 {
        unsafe { hdr_mean(self.histo) }
    }

    /// Returns true if two values are the same according to the lowest, highest and significant
    /// figures parameters.
    pub fn values_are_equivalent(&self, a: u64, b: u64) -> bool {
        unsafe { hdr_values_are_equivalent(self.histo, a as int64_t, b as int64_t) }
    }

    /// Lowest value equivalent to the given value.
    pub fn lowest_equivalent_value(&self, value: u64) -> u64 {
        unsafe { hdr_lowest_equivalent_value(self.histo, value as int64_t) as u64 }
    }

    /// Count for a specific value.
    pub fn count_at_value(&self, value: u64) -> u64 {
        unsafe { hdr_count_at_value(self.histo, value as int64_t) as u64 }
    }

    /// Count at a given index. (XXX safe?)
    #[allow(dead_code)]
    fn count_at_index(&self, index: u32) -> u64 {
        unsafe { hdr_count_at_index(self.histo, index as int32_t) as u64 }
    }

    /// Value of a given index. (XXX safe?)
    #[allow(dead_code)]
    fn value_at_index(&self, index: u32) -> u64 {
        unsafe { hdr_value_at_index(self.histo, index as int32_t) as u64 }
    }

    /// Linear iterator over values. Results are returned in equally weighted buckets.
    ///
    /// ```
    /// # use hdrhistogram::Histogram;
    /// let mut h = Histogram::init(1, 100000, 3).unwrap();
    /// for i in 1..100 { h.record_values(i, i); }
    /// for (i, c) in h.linear_iter(1).enumerate() {    // 100 buckets
    ///     # assert_eq!(i+1, c.count_added_in_this_iteration_step as usize);
    ///     println!("bucket {} = {}", i, c.count_added_in_this_iteration_step);
    /// }
    /// ```
    pub fn linear_iter<'a>(&'a self, value_units_per_bucket: u64) -> LinearIter<'a> {
        let mut ret = LinearIter { iter: Default::default(), histo: PhantomData };
        unsafe { hdr_iter_linear_init(&mut ret.iter, self.histo, value_units_per_bucket as int64_t) };
        ret
    }

    /// Logarithmic iterator over values. Results are returned in logarithmically weighted buckets.
    pub fn log_iter<'a>(&'a self, value_units_per_bucket: u64, log_base: f64) -> LogIter<'a> {
        let mut ret = LogIter { iter: Default::default(), histo: PhantomData };
        unsafe { hdr_iter_log_init(&mut ret.iter, self.histo, value_units_per_bucket as int64_t, log_base) };
        ret
    }

    /// Iterator over recorded values.
    pub fn recorded_iter<'a>(&'a self) -> RecordedIter<'a> {
        let mut ret = RecordedIter { iter: Default::default(), histo: PhantomData };
        unsafe { hdr_iter_recorded_init(&mut ret.iter, self.histo) };
        ret
    }

    /// Iterator over percentiles.
    pub fn percentile_iter<'a>(&'a self, ticks_per_half_distance: u32) -> PercentileIter<'a> {
        let mut ret = PercentileIter { iter: Default::default(), histo: PhantomData };
        unsafe { hdr_iter_percentile_init(&mut ret.iter, self.histo, ticks_per_half_distance as int32_t) };
        ret
    }

    /// Encode `Histogram` state into a Base64 encoded string.
    pub fn encode(&self) -> Result<String> {
        let mut p : *mut c_char = ptr::null_mut();
        let r = unsafe { hdr_log_encode(self.histo, &mut p) };

        if r != 0 || p.is_null() {
            Err(HistogramErr(str::from_utf8(unsafe { CStr::from_ptr(hdr_strerror(r)) }.to_bytes()).unwrap()))
        } else {
            let sz = unsafe { libc::strlen(p) as usize };
            let s = unsafe {
                let mut v = Vec::with_capacity(sz);
                v.set_len(sz);
                ptr::copy(p as *const u8, v.as_mut_ptr(), sz);
                libc::free(p as *mut c_void);
                    
                String::from_utf8_unchecked(v)
            };
            
            Ok(s)
        }
    }

    /// Decode `Histogram` state from a Base64 string generated by `encode`.
    pub fn decode(base64: &String) -> Result<Histogram> {
        let bytes = base64.as_bytes();
        let mut h : *mut hdr_histogram = ptr::null_mut();
        let r = unsafe { hdr_log_decode(&mut h, bytes.as_ptr() as *const c_char, bytes.len() as size_t) };

        if r != 0 || h.is_null() {
            Err(HistogramErr(str::from_utf8(unsafe { CStr::from_ptr(hdr_strerror(r)) }.to_bytes()).unwrap()))
        } else {
            Ok(Histogram { histo: h, owned: true })
        }
    }

    /// Return allocation size.
    fn get_memory_size(&self) -> usize { unsafe { hdr_get_memory_size(self.histo) as usize } }

    /// Return number of counters.
    #[cfg(test)]
    fn get_counts_len(&self) -> u32 { unsafe { (*self.histo).counts_len as u32 } }
}

impl Drop for Histogram {
    fn drop(&mut self) {
        if self.owned && !self.histo.is_null() {
            unsafe { libc::free(self.histo as *mut c_void) }
        }
    }
}

impl Clone for Histogram {
    fn clone(&self) -> Self {
        let sz = self.get_memory_size();
        let p = unsafe { libc::malloc(sz as size_t) as *mut hdr_histogram };

        if p.is_null() { panic!("allocation of hdr_histogram failed"); }

        unsafe { ptr::copy(self.histo as *const u8, p as *mut u8, sz) };

        Histogram { histo: p, owned: true }
    }
}

/// Iterator result producing counts.
#[derive(PartialEq,Eq,PartialOrd,Ord,Clone,Copy,Debug)]
pub struct CountIterItem {
    /// The count of recorded values in the histogram that were added to the `total_count_to_this_value`
    /// (below) as a result on this iteration step. Since multiple iteration steps may occur with
    /// overlapping equivalent value ranges, the count may be lower than the count found at the
    /// value (e.g. multiple linear steps or percentile levels can occur within a single equivalent
    /// value range)
    pub count_added_in_this_iteration_step: u64,

    /// The sum of all recorded values in the histogram at values equal or smaller than `value_from_index`.
    pub count: u64,

    /// The actual value level that was iterated to by the iterator
    pub value: u64,

    /// Highest value equivalent to `value`.
    pub highest_equivalent_value: u64,

    /// Median value equivalent to `value`.
    pub median_equivalent_value: u64,

    /// Lowest value equivalent to `value`.
    pub lowest_equivalent_value: u64,

    value_iterated_from: u64,
    value_iterated_to: u64,
}

/// Iterator result producing percentiles.
#[derive(PartialEq,PartialOrd,Clone,Copy,Debug)]
pub struct PercentileIterItem {
    /// The percentile of recorded values in the histogram at values equal or smaller than `value_from_index`.
    pub percentile: f64,

    /// The sum of all recorded values in the histogram at values equal or smaller than `value_from_index`.
    pub count: u64,

    /// The actual value level that was iterated to by the iterator
    pub value: u64,

    /// Highest value equivalent to `value`.
    pub highest_equivalent_value: u64,

    /// Median value equivalent to `value`.
    pub median_equivalent_value: u64,

    /// Lowest value equivalent to `value`.
    pub lowest_equivalent_value: u64,

    value_iterated_from: u64,
    value_iterated_to: u64,
}

/// Iterator over `Histogram` producing linear buckets.
pub struct LinearIter<'a> {
    iter: hdr_iter,
    histo: PhantomData<&'a Histogram>,
}

impl<'a> Iterator for LinearIter<'a> {
    type Item = CountIterItem;
    
    fn next(&mut self) -> Option<CountIterItem> {
        if unsafe { hdr_iter_next(&mut self.iter) } {
            let lin : &hdr_iter_linear = unsafe { mem::transmute(&self.iter.union) };
            
            Some(CountIterItem { count_added_in_this_iteration_step: lin.count_added_in_this_iteration_step as u64,
                                 count: self.iter.count as u64,
                                 value: self.iter.value as u64,
                                 highest_equivalent_value: self.iter.highest_equivalent_value as u64,
                                 lowest_equivalent_value: self.iter.lowest_equivalent_value as u64,
                                 median_equivalent_value: self.iter.median_equivalent_value as u64,
                                 value_iterated_from: self.iter.value_iterated_from as u64,
                                 value_iterated_to: self.iter.value_iterated_to as u64 })
        } else {
            None
        }
    }
}

/// Iterator over `Histogram` producing logarithmic buckets.
pub struct LogIter<'a> {
    iter: hdr_iter,
    histo: PhantomData<&'a Histogram>,
}

impl<'a> Iterator for LogIter<'a> {
    type Item = CountIterItem;
    
    fn next(&mut self) -> Option<CountIterItem> {
        if unsafe { hdr_iter_next(&mut self.iter) } {
            let log : &hdr_iter_log = unsafe { mem::transmute(&self.iter.union) };
            
            Some(CountIterItem { count_added_in_this_iteration_step: log.count_added_in_this_iteration_step as u64,
                                 count: self.iter.count as u64,
                                 value: self.iter.value as u64,
                                 highest_equivalent_value: self.iter.highest_equivalent_value as u64,
                                 lowest_equivalent_value: self.iter.lowest_equivalent_value as u64,
                                 median_equivalent_value: self.iter.median_equivalent_value as u64,
                                 value_iterated_from: self.iter.value_iterated_from as u64,
                                 value_iterated_to: self.iter.value_iterated_to as u64 })
        } else {
            None
        }
    }
}

/// Iterator over `Histogram` producing recorded values.
pub struct RecordedIter<'a> {
    iter: hdr_iter,
    histo: PhantomData<&'a Histogram>,
}

impl<'a> Iterator for RecordedIter<'a> {
    type Item = CountIterItem;
    
    fn next(&mut self) -> Option<CountIterItem> {
        if unsafe { hdr_iter_next(&mut self.iter) } {
            let rec : &hdr_iter_recorded = unsafe { mem::transmute(&self.iter.union) };
            
            Some(CountIterItem { count_added_in_this_iteration_step: rec.count_added_in_this_iteration_step as u64,
                                 count: self.iter.count as u64,
                                 value: self.iter.value as u64,
                                 highest_equivalent_value: self.iter.highest_equivalent_value as u64,
                                 lowest_equivalent_value: self.iter.lowest_equivalent_value as u64,
                                 median_equivalent_value: self.iter.median_equivalent_value as u64,
                                 value_iterated_from: self.iter.value_iterated_from as u64,
                                 value_iterated_to: self.iter.value_iterated_to as u64 })
        } else {
            None
        }
    }
}

/// Iterator over `Histogram` producing percentile buckets.
pub struct PercentileIter<'a> {
    iter: hdr_iter,
    histo: PhantomData<&'a Histogram>,
}

impl<'a> Iterator for PercentileIter<'a> {
    type Item = PercentileIterItem;
    
    fn next(&mut self) -> Option<PercentileIterItem> {
        if unsafe { hdr_iter_next(&mut self.iter) } {
            let perc : &hdr_iter_percentiles = unsafe { mem::transmute(&self.iter.union) };
            
            Some(PercentileIterItem { percentile: perc.percentile,
                                      count: self.iter.count as u64,
                                      value: self.iter.value as u64,
                                      highest_equivalent_value: self.iter.highest_equivalent_value as u64,
                                      lowest_equivalent_value: self.iter.lowest_equivalent_value as u64,
                                      median_equivalent_value: self.iter.median_equivalent_value as u64,
                                      value_iterated_from: self.iter.value_iterated_from as u64,
                                      value_iterated_to: self.iter.value_iterated_to as u64 })
        } else {
            None
        }
    }
}