Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

label.rs: Implemented inner reference trait for Label #470

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 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,14 @@ 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);
}
}
Loading