iroh_metrics/
iterable.rs

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
//! Traits for iterating over the fields of structs.

use std::fmt;

/// Derives [`Iterable`] for a struct.
///
/// You can use this derive instead of [`MetricsGroup`] if you want to implement `Default`
/// and `MetricsGroup` manually, but still use a derived `Iterable` impl.
///
/// [`Iterable`]: ::iroh_metrics::iterable::Iterable
/// [`MetricsGroup`]: ::iroh_metrics::MetricsGroup
pub use iroh_metrics_derive::Iterable;

use crate::MetricItem;

/// Trait for iterating over the fields of a struct.
pub trait Iterable {
    /// Returns the number of fields in the struct.
    fn field_count(&self) -> usize;
    /// Returns the field name and dyn reference to the field.
    fn field_ref(&self, n: usize) -> Option<MetricItem<'_>>;
}

/// Helper trait to convert from `self` to `dyn Iterable`.
pub trait IntoIterable {
    /// Returns `self` as `dyn Iterable`
    fn as_iterable(&self) -> &dyn Iterable;

    /// Returns an iterator over the fields of the struct.
    fn field_iter(&self) -> FieldIter<'_> {
        FieldIter::new(self.as_iterable())
    }
}

impl<T> IntoIterable for T
where
    T: Iterable,
{
    fn as_iterable(&self) -> &dyn Iterable {
        self
    }
}

/// Iterator over the fields of a struct.
///
/// Returned from [`IntoIterable::field_iter`].
pub struct FieldIter<'a> {
    pos: usize,
    inner: &'a dyn Iterable,
}

impl fmt::Debug for FieldIter<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "FieldIter")
    }
}

impl<'a> FieldIter<'a> {
    pub(crate) fn new(inner: &'a dyn Iterable) -> Self {
        Self { pos: 0, inner }
    }
}
impl<'a> Iterator for FieldIter<'a> {
    type Item = MetricItem<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pos == self.inner.field_count() {
            None
        } else {
            let out = self.inner.field_ref(self.pos);
            self.pos += 1;
            out
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let n = self.inner.field_count() - self.pos;
        (n, Some(n))
    }
}