Skip to content

Commit 7068677

Browse files
authored
Change Tag to hold a String instead of Cow<str> (#52)
* Change Tag to hold a String instead of Cow<str> This originated from a lint on nightly which didn't like Cow::to_owned being called, and instead callers should use Cow::clone or Cow::into_owned. However, while inspecting the code with fresh eyes, we saw that we're always holding a Cow::Owned variant, so just hold the String instead. * Implement AsRef<str> for Tag This allows callers to see if a Tag matches a prefix like: if tag.as_ref().starts_with("process_id:")
1 parent 81b1753 commit 7068677

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

ddcommon/src/tag.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use serde::Serialize;
99
#[derive(Clone, Eq, PartialEq, Hash, Serialize)]
1010
#[serde(transparent)]
1111
pub struct Tag {
12-
value: Cow<'static, str>,
12+
value: String,
1313
}
1414

1515
impl Debug for Tag {
@@ -18,6 +18,12 @@ impl Debug for Tag {
1818
}
1919
}
2020

21+
impl AsRef<str> for Tag {
22+
fn as_ref(&self) -> &str {
23+
self.value.as_ref()
24+
}
25+
}
26+
2127
// Any type which implements Display automatically has to_string.
2228
impl Display for Tag {
2329
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
@@ -28,9 +34,10 @@ impl Display for Tag {
2834
impl Tag {
2935
/// It's recommended to use Tag::new when possible, as tags that are in
3036
/// the <KEY>:<VALUE> format are preferred.
31-
pub fn from_value<'a, IntoCow: Into<Cow<'a, str>>>(
32-
chunk: IntoCow,
33-
) -> Result<Self, Cow<'static, str>> {
37+
pub fn from_value<'a, IntoCow>(chunk: IntoCow) -> Result<Self, Cow<'static, str>>
38+
where
39+
IntoCow: Into<Cow<'a, str>>,
40+
{
3441
let chunk = chunk.into();
3542

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

5764
Ok(Tag {
58-
value: chunk.into_owned().into(),
65+
value: chunk.into_owned(),
5966
})
6067
}
6168

@@ -69,11 +76,6 @@ impl Tag {
6976

7077
Tag::from_value(format!("{}:{}", key, value))
7178
}
72-
73-
pub fn into_owned(mut self) -> Self {
74-
self.value = self.value.to_owned();
75-
self
76-
}
7779
}
7880

7981
/// Parse a string of tags typically provided by environment variables

profiling-ffi/src/exporter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub extern "C" fn profile_exporter_new(
123123
match || -> anyhow::Result<ProfileExporter> {
124124
let family = unsafe { family.to_utf8_lossy() }.into_owned();
125125
let converted_endpoint = unsafe { try_to_endpoint(endpoint)? };
126-
let tags = tags.map(|tags| tags.iter().map(|tag| tag.clone().into_owned()).collect());
126+
let tags = tags.map(|tags| tags.iter().map(Tag::clone).collect());
127127
ProfileExporter::new(family, tags, converted_endpoint)
128128
}() {
129129
Ok(exporter) => NewProfileExporterResult::Ok(Box::into_raw(Box::new(exporter))),

profiling/src/exporter/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ impl ProfileExporter {
122122
/// Build a Request object representing the profile information provided.
123123
pub fn build(
124124
&self,
125-
start: chrono::DateTime<chrono::Utc>,
126-
end: chrono::DateTime<chrono::Utc>,
125+
start: DateTime<Utc>,
126+
end: DateTime<Utc>,
127127
files: &[File],
128128
additional_tags: Option<&Vec<Tag>>,
129129
timeout: std::time::Duration,
@@ -133,7 +133,7 @@ impl ProfileExporter {
133133
form.add_text("version", "3");
134134
form.add_text("start", start.format("%Y-%m-%dT%H:%M:%S%.9fZ").to_string());
135135
form.add_text("end", end.format("%Y-%m-%dT%H:%M:%S%.9fZ").to_string());
136-
form.add_text("family", self.family.to_owned());
136+
form.add_text("family", self.family.as_ref());
137137

138138
for tags in self.tags.as_ref().iter().chain(additional_tags.iter()) {
139139
for tag in tags.iter() {

0 commit comments

Comments
 (0)