Skip to content

Commit

Permalink
label.rs: Implemented inner reference trait.
Browse files Browse the repository at this point in the history
Fixes: #469
  • Loading branch information
jpds committed Apr 10, 2024
1 parent a143ef6 commit 9d4ca0b
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions metrics/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ where
}
}

impl<K, V> From<(&K, &V)> for Label
where
K: Into<SharedString> + Clone,
V: Into<SharedString> + Clone,
{
fn from(pair: (&K, &V)) -> Label {
Label::new(pair.0.clone(), pair.1.clone())
}
}

/// A value that can be converted to a vector of [`Label`]s.
pub trait IntoLabels {
/// Consumes this value, turning it into a vector of [`Label`]s.
Expand Down Expand Up @@ -99,4 +109,16 @@ mod label_tests {
let expected = vec![Label::new("x", "a"), Label::new("y", "b")];
assert_eq!(from_slice_to_labels(&labels), expected);
}

#[test]
fn btreemap_to_labels() {
use std::collections::BTreeMap;

let labels_btreemap = BTreeMap::from([
("customer", "Rust Foundation"),
]);

let expected = vec![Label::new("customer", "Rust Foundation")];
assert_eq!(labels_btreemap.into_labels(), expected);
}
}

0 comments on commit 9d4ca0b

Please sign in to comment.