Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #3 from Overmuse/SR/fix_lifetimes
Browse files Browse the repository at this point in the history
fix lifetimes
  • Loading branch information
SebRollen authored Jun 18, 2021
2 parents e5521de + 20e1bc9 commit d3734c8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dogstatsd"
version = "0.2.0"
version = "0.3.0"
authors = ["Sebastian Rollen <[email protected]>"]
edition = "2018"

Expand Down
16 changes: 12 additions & 4 deletions src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum Type<'a> {
pub struct Metric<'a> {
frame_type: Type<'a>,
message: &'a str,
tags: Vec<Tag<'a>>,
tags: Vec<Tag>,
}

impl<'a> Metric<'a> {
Expand Down Expand Up @@ -54,8 +54,14 @@ impl<'a> Metric<'a> {
Self::new(Type::Set(value), message)
}

pub fn add_tag<T: Into<Tag<'a>>>(mut self, tag: T) -> Self {
self.tags.push(tag.into());
pub fn add_tag<T: ToString>(mut self, tag: T) -> Self {
self.tags.push(Tag::Single(tag.to_string()));
self
}

pub fn add_key_value<K: ToString, V: ToString>(mut self, key: K, val: V) -> Self {
self.tags
.push(Tag::KeyValue(key.to_string(), val.to_string()));
self
}

Expand Down Expand Up @@ -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(),
Expand Down
33 changes: 9 additions & 24 deletions src/tag.rs
Original file line number Diff line number Diff line change
@@ -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<Tag<'a>> for Cow<'a, str> {
fn from(tag: Tag<'a>) -> Cow<'a, str> {
impl<'a> From<Tag> 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
}
}
}
Expand Down

0 comments on commit d3734c8

Please sign in to comment.