use std::borrow::Cow;
use crate::Span;
use crate::local::LocalSpan;
use crate::util::Properties;
pub struct Event {
pub(crate) name: Cow<'static, str>,
pub(crate) properties: Properties,
}
impl Event {
#[inline]
pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
Event {
name: name.into(),
properties: Properties::default(),
}
}
#[inline]
pub fn with_property<K, V, F>(self, property: F) -> Self
where
K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
F: FnOnce() -> (K, V),
{
self.with_properties(|| [property()])
}
#[inline]
pub fn with_properties<K, V, I, F>(mut self, properties: F) -> Self
where
K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
I: IntoIterator<Item = (K, V)>,
F: FnOnce() -> I,
{
#[cfg(feature = "enable")]
{
self.properties
.extend(properties().into_iter().map(|(k, v)| (k.into(), v.into())))
}
self
}
#[deprecated(since = "0.7.8", note = "use `Span::add_event` instead")]
pub fn add_to_parent<I, F>(name: impl Into<Cow<'static, str>>, parent: &Span, properties: F)
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
F: FnOnce() -> I,
{
let event = Event::new(name).with_properties(properties);
parent.add_event(event);
}
#[deprecated(since = "0.7.8", note = "use `LocalSpan::add_event` instead")]
pub fn add_to_local_parent<I, F>(name: impl Into<Cow<'static, str>>, properties: F)
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
F: FnOnce() -> I,
{
let event = Event::new(name).with_properties(properties);
LocalSpan::add_event(event);
}
}