From a8e769891a1845b4a84682d47fdcd4fc9c3b7bfc Mon Sep 17 00:00:00 2001 From: Jerry Wang Date: Sat, 1 Jun 2024 21:55:10 -0700 Subject: [PATCH] Refactor `Registry::new_custom()` to accept `AsRef` instead of `String` for improved flexibility Signed-off-by: Jerry Wang --- src/registry.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/registry.rs b/src/registry.rs index 4d07d4bb..a82d91f4 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -229,12 +229,14 @@ impl Registry { } /// Create a new registry, with optional custom prefix and labels. - pub fn new_custom( - prefix: Option, - labels: Option>, - ) -> Result { + pub fn new_custom(prefix: Option, labels: Option>) -> Result + where + T: AsRef, + K: AsRef, + V: AsRef, + { if let Some(ref namespace) = prefix { - if namespace.is_empty() { + if namespace.as_ref().is_empty() { return Err(Error::Msg("empty prefix namespace".to_string())); } } @@ -242,8 +244,20 @@ impl Registry { let reg = Registry::default(); { let mut core = reg.r.write(); - core.prefix = prefix; - core.labels = labels; + + core.prefix = match prefix { + Some(s) => Some(s.as_ref().to_string()), + None => None, + }; + + core.labels = match labels { + Some(m) => Some( + m.into_iter() + .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string())) + .collect(), + ), + None => None, + }; } Ok(reg) }