metrics_util/
key.rs

1use crate::MetricKind;
2use metrics::Key;
3
4/// A composite key that stores both the metric key and the metric kind.
5#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
6pub struct CompositeKey(MetricKind, Key);
7
8impl CompositeKey {
9    /// Creates a new `CompositeKey`.
10    pub const fn new(kind: MetricKind, key: Key) -> CompositeKey {
11        CompositeKey(kind, key)
12    }
13
14    /// Gets the inner key represented by this `CompositeKey`.
15    pub fn key(&self) -> &Key {
16        &self.1
17    }
18
19    /// Gets the inner kind represented by this `CompositeKey`.
20    pub fn kind(&self) -> MetricKind {
21        self.0
22    }
23
24    /// Takes the individual pieces of this `CompositeKey`.
25    pub fn into_parts(self) -> (MetricKind, Key) {
26        (self.0, self.1)
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use std::cmp::Ordering;
33
34    use metrics::Key;
35
36    use super::*;
37
38    #[test]
39    fn test_same_keys_different_kinds_not_equal() {
40        let key = Key::from_name("test");
41        let key1 = CompositeKey::new(MetricKind::Counter, key.clone());
42        let key2 = CompositeKey::new(MetricKind::Gauge, key);
43
44        assert_ne!(key1.cmp(&key2), Ordering::Equal);
45    }
46}