From a722af44556135ad1296efff867e6f7b49fbc9e4 Mon Sep 17 00:00:00 2001 From: Alin Sinpalean Date: Sat, 3 Oct 2020 12:22:01 +0200 Subject: [PATCH] src/macros: Have `labels!` accept any type that implements `Display` This allows (among other things) `histogram_opts!` to take the same kinds of types as `opts!`. E.g. to replace this: ```rust let status = String::from("200"); histogram_opts!( ... labels! {"status".to_string() => status.clone()} ) // Do more stuff with `status`. ``` with this: ```rust let status = String::from("200"); histogram_opts!( ... labels! {"status" => &status} ) // Do more stuff with `status`. ``` And a lot more, such as using numbers or enums as label values directly. Signed-off-by: Alin Sinpalean --- src/macros.rs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 61511c73..e021a34a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -8,15 +8,17 @@ /// # #[macro_use] extern crate prometheus; /// # use std::collections::HashMap; /// # fn main() { +/// let path = String::from("/metrics"); +/// let status = 200; /// let labels = labels!{ -/// "test" => "hello", -/// "foo" => "bar", +/// "path" => path, +/// "status" => status, /// }; /// assert_eq!(labels.len(), 2); -/// assert!(labels.get("test").is_some()); -/// assert_eq!(*(labels.get("test").unwrap()), "hello"); +/// assert_eq!(*(labels.get("path").unwrap()), "/metrics"); +/// assert_eq!(*(labels.get("status").unwrap()), "200"); /// -/// let labels: HashMap<&str, &str> = labels!{}; +/// let labels = labels!{}; /// assert!(labels.is_empty()); /// # } /// ``` @@ -26,9 +28,9 @@ macro_rules! labels { { use std::collections::HashMap; - let mut lbs = HashMap::new(); + let mut lbs = HashMap::::new(); $( - lbs.insert($KEY, $VALUE); + lbs.insert($KEY.to_string(), $VALUE.to_string()); )* lbs @@ -55,10 +57,11 @@ macro_rules! labels { /// assert!(opts.const_labels.get("foo").is_some()); /// assert_eq!(opts.const_labels.get("foo").unwrap(), "bar"); /// -/// let opts = opts!(name, -/// help, -/// labels!{"test" => "hello", "foo" => "bar",}, -/// labels!{"ans" => "42",}); +/// let opts = opts!( +/// name, +/// help, +/// labels!{"test" => "hello", "foo" => "bar"}, +/// labels!{"ans" => 42}); /// assert_eq!(opts.const_labels.len(), 3); /// assert!(opts.const_labels.get("ans").is_some()); /// assert_eq!(opts.const_labels.get("ans").unwrap(), "42"); @@ -74,7 +77,7 @@ macro_rules! opts { let lbs = HashMap::::new(); $( let mut lbs = lbs; - lbs.extend($CONST_LABELS.iter().map(|(k, v)| ((*k).into(), (*v).into()))); + lbs.extend($CONST_LABELS.into_iter().map(|(k, v)| (k.to_string(), v.to_string()))); )* opts.const_labels(lbs) @@ -102,15 +105,16 @@ macro_rules! opts { /// assert_eq!(opts.common_opts.help, help); /// assert_eq!(opts.buckets.len(), 4); /// -/// let opts = histogram_opts!(name, -/// help, -/// vec![1.0, 2.0], -/// labels!{"key".to_string() => "value".to_string(),}); +/// let opts = histogram_opts!( +/// name, +/// help, +/// vec![1.0, 2.0], +/// labels!{"key" => "value", "status" => 200}); /// assert_eq!(opts.common_opts.name, name); /// assert_eq!(opts.common_opts.help, help); /// assert_eq!(opts.buckets.len(), 2); -/// assert!(opts.common_opts.const_labels.get("key").is_some()); /// assert_eq!(opts.common_opts.const_labels.get("key").unwrap(), "value"); +/// assert_eq!(opts.common_opts.const_labels.get("status").unwrap(), "200"); /// # } /// ``` #[macro_export(local_inner_macros)]