use smallvec::SmallVec;
use crate::{Profiler, StringComponent, StringId};
pub const SEPARATOR_BYTE: &str = "\x1E";
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
#[repr(C)]
pub struct EventId(StringId);
impl EventId {
pub const INVALID: EventId = EventId(StringId::INVALID);
#[inline]
pub fn to_string_id(self) -> StringId {
self.0
}
#[inline]
pub fn as_u64(self) -> u64 {
self.0.as_u64()
}
#[inline]
pub fn from_label(label: StringId) -> Self {
EventId(label)
}
#[inline]
pub fn from_virtual(virtual_id: StringId) -> Self {
EventId(virtual_id)
}
#[inline]
pub fn from_u64(raw_id: u64) -> Self {
EventId(StringId::new(raw_id))
}
}
pub struct EventIdBuilder<'p> {
profiler: &'p Profiler,
}
impl<'p> EventIdBuilder<'p> {
pub fn new(profiler: &Profiler) -> EventIdBuilder<'_> {
EventIdBuilder { profiler }
}
#[inline]
pub fn from_label(&self, label: StringId) -> EventId {
EventId::from_label(label)
}
pub fn from_label_and_arg(&self, label: StringId, arg: StringId) -> EventId {
EventId(self.profiler.alloc_string(&[
StringComponent::Ref(label),
StringComponent::Value(SEPARATOR_BYTE),
StringComponent::Ref(arg),
]))
}
pub fn from_label_and_args(&self, label: StringId, args: &[StringId]) -> EventId {
let mut parts = SmallVec::<[StringComponent<'_>; 7]>::with_capacity(1 + args.len() * 2);
parts.push(StringComponent::Ref(label));
for arg in args {
parts.push(StringComponent::Value(SEPARATOR_BYTE));
parts.push(StringComponent::Ref(*arg));
}
EventId(self.profiler.alloc_string(&parts[..]))
}
}