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

Refactor Registry::new_custom() to accept AsRef<str> instead of String for improved flexibility #522

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 21 additions & 7 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,35 @@ impl Registry {
}

/// Create a new registry, with optional custom prefix and labels.
pub fn new_custom(
prefix: Option<String>,
labels: Option<HashMap<String, String>>,
) -> Result<Registry> {
pub fn new_custom<T, K, V>(prefix: Option<T>, labels: Option<HashMap<K, V>>) -> Result<Registry>
where
T: AsRef<str>,
K: AsRef<str>,
V: AsRef<str>,
{
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()));
}
}

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)
}
Expand Down
Loading