From fa690027743e8f330eb3e2038ba03df71c5c2a69 Mon Sep 17 00:00:00 2001 From: Darksome Date: Tue, 2 Jul 2024 15:01:29 +0000 Subject: [PATCH] fix: make Optional label compatible with macro --- crates/metrics/src/label.rs | 43 ++++++++++++++++++-- crates/metrics/src/lib.rs | 2 +- crates/metrics/src/macros.rs | 78 ++++++++++++++++++++++++++++++------ 3 files changed, 107 insertions(+), 16 deletions(-) diff --git a/crates/metrics/src/label.rs b/crates/metrics/src/label.rs index b9d0416..f5369c3 100644 --- a/crates/metrics/src/label.rs +++ b/crates/metrics/src/label.rs @@ -8,7 +8,7 @@ use { metrics::Label, parking_lot::Mutex, smallvec::SmallVec, - std::{borrow::Borrow, collections::HashMap, marker::PhantomData, sync::Arc}, + std::{borrow::Borrow, collections::HashMap, sync::Arc}, }; pub type DynamicLabels = SmallVec<[Label; 4]>; @@ -343,9 +343,33 @@ where } } -/// Makes any other label optional by accepting `Option` instead of the actual +/// Makes any other label optional by accepting [`Option`] instead of the actual /// label value during the label resolution. -pub struct Optional(PhantomData); +pub struct Optional(pub Option); + +impl Optional> { + /// Creates a new [`Optional`] [`EnumLabel`]. + pub fn new(v: Option>) -> Self { + Self(v.map(EnumLabel::new)) + } +} + +impl Optional> { + /// Creates a new [`Optional`] [`BoolLabel`]. + pub fn new(v: Option) -> Self { + Self(v.map(BoolLabel::new)) + } +} + +impl Optional> { + /// Creates a new [`Optional`] [`StringLabel`]. + pub fn new(v: Option<&U>) -> Optional> + where + T: Borrow, + { + Optional(v.map(StringLabel::::new)) + } +} impl DynamicLabel for Optional where @@ -386,6 +410,19 @@ where } } +impl ResolveLabels<(Optional,)> for WithLabel, M> +where + T: DynamicLabel, + M: Metric, + WithLabel: ResolveLabels<(U,), Target = M>, +{ + type Target = M; + + fn resolve_labels(&self, (label,): (Optional,)) -> &M { + self.resolve_labels((label.0,)) + } +} + // TODO: macro to autogenerate these impl ResolveLabels<(A, B)> for WithLabel diff --git a/crates/metrics/src/lib.rs b/crates/metrics/src/lib.rs index 8efd4b0..d6cbda6 100644 --- a/crates/metrics/src/lib.rs +++ b/crates/metrics/src/lib.rs @@ -317,4 +317,4 @@ pub type LabeledFutureMetrics4 = Labeled4 pub type OptionalEnumLabel = Optional>; pub type OptionalBoolLabel = Optional>; -pub type OptionalStringLabel = Optional>; +pub type OptionalStringLabel = Optional>; diff --git a/crates/metrics/src/macros.rs b/crates/metrics/src/macros.rs index 36d99be..86a4366 100644 --- a/crates/metrics/src/macros.rs +++ b/crates/metrics/src/macros.rs @@ -13,7 +13,16 @@ /// /// Usage: /// ``` -/// use wc_metrics::{enum_ordinalize::Ordinalize, BoolLabel, EnumLabel, StringLabel, counter}; +/// use wc_metrics::{ +/// counter, +/// enum_ordinalize::Ordinalize, +/// BoolLabel, +/// EnumLabel, +/// OptionalBoolLabel, +/// OptionalEnumLabel, +/// OptionalStringLabel, +/// StringLabel, +/// }; /// /// #[derive(Clone, Copy, Debug, Ordinalize)] /// enum MyEnum { @@ -99,8 +108,12 @@ /// StringLabel<"b"> => s, /// StringLabel<"c", u8> => &u, /// BoolLabel<"d"> => b, -/// "e" => "1", -/// "f" => "2" +/// OptionalEnumLabel<"e", MyEnum> => Some(e), +/// OptionalStringLabel<"f"> => Some(s), +/// OptionalStringLabel<"g", u8> => Some(&u), +/// OptionalBoolLabel<"h"> => Some(b), +/// "i" => "1", +/// "j" => "2" /// ) /// .increment(1); /// ``` @@ -126,7 +139,16 @@ macro_rules! counter { /// /// Usage: /// ``` -/// use wc_metrics::{enum_ordinalize::Ordinalize, BoolLabel, EnumLabel, StringLabel, gauge}; +/// use wc_metrics::{ +/// gauge, +/// enum_ordinalize::Ordinalize, +/// BoolLabel, +/// EnumLabel, +/// OptionalBoolLabel, +/// OptionalEnumLabel, +/// OptionalStringLabel, +/// StringLabel, +/// }; /// /// #[derive(Clone, Copy, Debug, Ordinalize)] /// enum MyEnum { @@ -212,8 +234,12 @@ macro_rules! counter { /// StringLabel<"b"> => s, /// StringLabel<"c", u8> => &u, /// BoolLabel<"d"> => b, -/// "e" => "1", -/// "f" => "2" +/// OptionalEnumLabel<"e", MyEnum> => Some(e), +/// OptionalStringLabel<"f"> => Some(s), +/// OptionalStringLabel<"g", u8> => Some(&u), +/// OptionalBoolLabel<"h"> => Some(b), +/// "i" => "1", +/// "j" => "2" /// ) /// .set(1); /// ``` @@ -239,7 +265,16 @@ macro_rules! gauge { /// /// Usage: /// ``` -/// use wc_metrics::{enum_ordinalize::Ordinalize, BoolLabel, EnumLabel, StringLabel, histogram}; +/// use wc_metrics::{ +/// histogram, +/// enum_ordinalize::Ordinalize, +/// BoolLabel, +/// EnumLabel, +/// OptionalBoolLabel, +/// OptionalEnumLabel, +/// OptionalStringLabel, +/// StringLabel, +/// }; /// /// #[derive(Clone, Copy, Debug, Ordinalize)] /// enum MyEnum { @@ -325,8 +360,12 @@ macro_rules! gauge { /// StringLabel<"b"> => s, /// StringLabel<"c", u8> => &u, /// BoolLabel<"d"> => b, -/// "e" => "1", -/// "f" => "2" +/// OptionalEnumLabel<"e", MyEnum> => Some(e), +/// OptionalStringLabel<"f"> => Some(s), +/// OptionalStringLabel<"g", u8> => Some(&u), +/// OptionalBoolLabel<"h"> => Some(b), +/// "i" => "1", +/// "j" => "2" /// ) /// .record(1); /// ``` @@ -343,7 +382,18 @@ macro_rules! histogram { /// Usage: /// ``` /// use std::future::Future; -/// use wc_metrics::{enum_ordinalize::Ordinalize, BoolLabel, EnumLabel, StringLabel, future_metrics, FutureMetrics, FutureExt}; +/// use wc_metrics::{ +/// future_metrics, +/// enum_ordinalize::Ordinalize, +/// BoolLabel, +/// EnumLabel, +/// OptionalBoolLabel, +/// OptionalEnumLabel, +/// OptionalStringLabel, +/// StringLabel, +/// FutureExt, +/// FutureMetrics, +/// }; /// /// #[derive(Clone, Copy, Debug, Ordinalize)] /// enum MyEnum { @@ -399,8 +449,12 @@ macro_rules! histogram { /// StringLabel<"b"> => s, /// StringLabel<"c", u8> => &u, /// BoolLabel<"d"> => b, -/// "e" => "1", -/// "f" => "2" +/// OptionalEnumLabel<"e", MyEnum> => Some(e), +/// OptionalStringLabel<"f"> => Some(s), +/// OptionalStringLabel<"g", u8> => Some(&u), +/// OptionalBoolLabel<"h"> => Some(b), +/// "i" => "1", +/// "j" => "2" /// ))); /// ``` #[cfg(feature = "future")]