dogstatsd-rs icon indicating copy to clipboard operation
dogstatsd-rs copied to clipboard

Add the Tag trait

Open sebastianblunt opened this issue 5 years ago • 1 comments

This allows people to write custom implementations of the Tag trait rather than having to rely on format! or similar and passing in tags as str/String.

The Tag trait has a blanket implementation for AsRef so this change should be backwards compatible.

I don't have any strong need to include TagTuple in the upstream library, it's mostly just as an example usage. The primary purpose is just to be able to have tags that don't rely on being formatted before being passed to this library.

sebastianblunt avatar Dec 24 '19 23:12 sebastianblunt

As is this doesn't let people create Tag trait objects because of the generic writer in write_tag.

I could change it in a few ways

  • Move W to the Tag trait (pub trait Tag<W: io::Write>) - this would add some verbosity to the types in this library as now every function will need to be generic over W, T where W: io::Write, T: Tag<W> and similarly any places using Tag trait objects will now also have to be generic over W.
  • Make the write_tag function take a trait object to the writer, (fn write_tag(&self, w: &mut dyn io::Write)). Theoretically it should be a very small performance loss, but it seems like the compiler is able to optimize away the dynamic dispatch in release mode.
  • Specify the exact writer type. It's only Vec<u8> right now. However doing this would cause changing that type to be a breaking change for any crates that have custom Tag types.
  • Do nothing. Library consumers wishing to pass multiple types of tags in at once will need to define an enum covering all the different types. Could write a macro let people easily create such enums.

sebastianblunt avatar Dec 27 '19 23:12 sebastianblunt