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

Change Tag to hold a String instead of Cow<str> #52

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
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
22 changes: 12 additions & 10 deletions ddcommon/src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::Serialize;
#[derive(Clone, Eq, PartialEq, Hash, Serialize)]
#[serde(transparent)]
pub struct Tag {
value: Cow<'static, str>,
value: String,
}

impl Debug for Tag {
Expand All @@ -18,6 +18,12 @@ impl Debug for Tag {
}
}

impl AsRef<str> for Tag {
fn as_ref(&self) -> &str {
self.value.as_ref()
}
}

// Any type which implements Display automatically has to_string.
impl Display for Tag {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Expand All @@ -28,9 +34,10 @@ impl Display for Tag {
impl Tag {
/// It's recommended to use Tag::new when possible, as tags that are in
/// the <KEY>:<VALUE> format are preferred.
pub fn from_value<'a, IntoCow: Into<Cow<'a, str>>>(
chunk: IntoCow,
) -> Result<Self, Cow<'static, str>> {
pub fn from_value<'a, IntoCow>(chunk: IntoCow) -> Result<Self, Cow<'static, str>>
where
IntoCow: Into<Cow<'a, str>>,
{
let chunk = chunk.into();

/* The docs have various rules, which we are choosing not to enforce:
Expand All @@ -55,7 +62,7 @@ impl Tag {
}

Ok(Tag {
value: chunk.into_owned().into(),
value: chunk.into_owned(),
})
}

Expand All @@ -69,11 +76,6 @@ impl Tag {

Tag::from_value(format!("{}:{}", key, value))
}

pub fn into_owned(mut self) -> Self {
self.value = self.value.to_owned();
self
}
}

/// Parse a string of tags typically provided by environment variables
Expand Down
2 changes: 1 addition & 1 deletion profiling-ffi/src/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub extern "C" fn profile_exporter_new(
match || -> anyhow::Result<ProfileExporter> {
let family = unsafe { family.to_utf8_lossy() }.into_owned();
let converted_endpoint = unsafe { try_to_endpoint(endpoint)? };
let tags = tags.map(|tags| tags.iter().map(|tag| tag.clone().into_owned()).collect());
let tags = tags.map(|tags| tags.iter().map(Tag::clone).collect());
ProfileExporter::new(family, tags, converted_endpoint)
}() {
Ok(exporter) => NewProfileExporterResult::Ok(Box::into_raw(Box::new(exporter))),
Expand Down
6 changes: 3 additions & 3 deletions profiling/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ impl ProfileExporter {
/// Build a Request object representing the profile information provided.
pub fn build(
&self,
start: chrono::DateTime<chrono::Utc>,
end: chrono::DateTime<chrono::Utc>,
start: DateTime<Utc>,
end: DateTime<Utc>,
files: &[File],
additional_tags: Option<&Vec<Tag>>,
timeout: std::time::Duration,
Expand All @@ -133,7 +133,7 @@ impl ProfileExporter {
form.add_text("version", "3");
form.add_text("start", start.format("%Y-%m-%dT%H:%M:%S%.9fZ").to_string());
form.add_text("end", end.format("%Y-%m-%dT%H:%M:%S%.9fZ").to_string());
form.add_text("family", self.family.to_owned());
form.add_text("family", self.family.as_ref());

for tags in self.tags.as_ref().iter().chain(additional_tags.iter()) {
for tag in tags.iter() {
Expand Down