Skip to main content

micromegas_tracing/
property_value.rs

1//! Values that the logging and metric macros accept as bare `key = value` entries.
2use std::{borrow::Cow, rc::Rc, sync::Arc};
3
4/// A value that can be recorded as a property without being formatted first.
5///
6/// Strings borrow, so a bare value costs no allocation. Use `%value` (`Display`) or `?value`
7/// (`Debug`) for anything else. The trait is deliberately owned by this crate: integer and
8/// bool impls can be added later without breaking callers, since downstream crates can't
9/// implement it for foreign types.
10pub trait PropertyValue {
11    fn property_value(&self) -> Cow<'_, str>;
12}
13
14impl PropertyValue for str {
15    fn property_value(&self) -> Cow<'_, str> {
16        Cow::Borrowed(self)
17    }
18}
19
20impl PropertyValue for String {
21    fn property_value(&self) -> Cow<'_, str> {
22        Cow::Borrowed(self.as_str())
23    }
24}
25
26impl PropertyValue for Cow<'_, str> {
27    fn property_value(&self) -> Cow<'_, str> {
28        Cow::Borrowed(&**self)
29    }
30}
31
32impl<T: PropertyValue + ?Sized> PropertyValue for &T {
33    fn property_value(&self) -> Cow<'_, str> {
34        (**self).property_value()
35    }
36}
37
38impl<T: PropertyValue + ?Sized> PropertyValue for Box<T> {
39    fn property_value(&self) -> Cow<'_, str> {
40        (**self).property_value()
41    }
42}
43
44impl<T: PropertyValue + ?Sized> PropertyValue for Arc<T> {
45    fn property_value(&self) -> Cow<'_, str> {
46        (**self).property_value()
47    }
48}
49
50impl<T: PropertyValue + ?Sized> PropertyValue for Rc<T> {
51    fn property_value(&self) -> Cow<'_, str> {
52        (**self).property_value()
53    }
54}