From 9d4ca0b407bfedde7029bdc320cf0beebb06c611 Mon Sep 17 00:00:00 2001 From: Jonathan Davies Date: Wed, 10 Apr 2024 21:25:40 +0100 Subject: [PATCH] label.rs: Implemented inner reference trait. Fixes: #469 --- metrics/src/label.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/metrics/src/label.rs b/metrics/src/label.rs index 4e04cbce..65cee591 100644 --- a/metrics/src/label.rs +++ b/metrics/src/label.rs @@ -56,6 +56,16 @@ where } } +impl From<(&K, &V)> for Label +where + K: Into + Clone, + V: Into + 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. @@ -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); + } }