From 20e1bc9a3d8492c06db955dd674fd17339797f48 Mon Sep 17 00:00:00 2001 From: Sebastian Rollen Date: Fri, 18 Jun 2021 19:22:27 -0400 Subject: [PATCH] fix lifetimes --- Cargo.toml | 2 +- src/metric.rs | 16 ++++++++++++---- src/tag.rs | 33 +++++++++------------------------ 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0ba81e9..3939869 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dogstatsd" -version = "0.2.0" +version = "0.3.0" authors = ["Sebastian Rollen "] edition = "2018" diff --git a/src/metric.rs b/src/metric.rs index 4987f04..70443e6 100644 --- a/src/metric.rs +++ b/src/metric.rs @@ -14,7 +14,7 @@ enum Type<'a> { pub struct Metric<'a> { frame_type: Type<'a>, message: &'a str, - tags: Vec>, + tags: Vec, } impl<'a> Metric<'a> { @@ -54,8 +54,14 @@ impl<'a> Metric<'a> { Self::new(Type::Set(value), message) } - pub fn add_tag>>(mut self, tag: T) -> Self { - self.tags.push(tag.into()); + pub fn add_tag(mut self, tag: T) -> Self { + self.tags.push(Tag::Single(tag.to_string())); + self + } + + pub fn add_key_value(mut self, key: K, val: V) -> Self { + self.tags + .push(Tag::KeyValue(key.to_string(), val.to_string())); self } @@ -163,9 +169,11 @@ mod test { Metric::set("1.2", "test").into_bytes().as_ref(), b"test:1.2|s" ); + + let val = String::from("b"); assert_eq!( Metric::increase("test") - .add_tag(("a", "b")) + .add_key_value("a", val) .add_tag("c") .into_bytes() .as_ref(), diff --git a/src/tag.rs b/src/tag.rs index 708e5f9..2a146b4 100644 --- a/src/tag.rs +++ b/src/tag.rs @@ -1,31 +1,16 @@ -use std::borrow::Cow; - -pub enum Tag<'a> { - Single(Cow<'a, str>), - KeyValue(Cow<'a, str>, Cow<'a, str>), -} - -impl<'a> From<&'a str> for Tag<'a> { - fn from(s: &'a str) -> Tag<'a> { - Tag::Single(Cow::Borrowed(s)) - } -} - -impl<'a> From<(&'a str, &'a str)> for Tag<'a> { - fn from(s: (&'a str, &'a str)) -> Tag<'a> { - Tag::KeyValue(Cow::Borrowed(s.0), Cow::Borrowed(s.1)) - } +pub enum Tag { + Single(String), + KeyValue(String, String), } -impl<'a> From> for Cow<'a, str> { - fn from(tag: Tag<'a>) -> Cow<'a, str> { +impl<'a> From for String { + fn from(tag: Tag) -> String { match tag { Tag::Single(single) => single, - Tag::KeyValue(key, value) => { - let mut out = key.to_string(); - out.push(':'); - out.push_str(value.as_ref()); - Cow::Owned(out) + Tag::KeyValue(mut key, value) => { + key.push(':'); + key.push_str(value.as_ref()); + key } } }