]> git.proxmox.com Git - rustc.git/blame - vendor/measureme/src/event_id.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / vendor / measureme / src / event_id.rs
CommitLineData
dfeec247
XL
1use crate::{Profiler, SerializationSink, StringComponent, StringId};
2
3/// Event IDs are strings conforming to the following grammar:
4///
5/// ```ignore
6/// <event_id> = <label> {<argument>}
7/// <label> = <text>
8/// <argument> = '\x1E' <text>
9/// <text> = regex([^[[:cntrl:]]]+) // Anything but ASCII control characters
10/// ```
11///
12/// This means there's always a "label", followed by an optional list of
13/// arguments. Future versions my support other optional suffixes (with a tag
14/// other than '\x11' after the '\x1E' separator), such as a "category".
15
16/// The byte used to separate arguments from the label and each other.
17pub const SEPARATOR_BYTE: &str = "\x1E";
18
19/// An `EventId` is a `StringId` with the additional guarantee that the
20/// corresponding string conforms to the event_id grammar.
21#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
22#[repr(C)]
23pub struct EventId(StringId);
24
25impl EventId {
26 pub const INVALID: EventId = EventId(StringId::INVALID);
27
28 #[inline]
29 pub fn to_string_id(self) -> StringId {
30 self.0
31 }
32
33 #[inline]
34 pub fn as_u32(self) -> u32 {
35 self.0.as_u32()
36 }
37
38 #[inline]
39 pub fn from_label(label: StringId) -> EventId {
40 EventId(label)
41 }
42
43 #[inline]
44 pub fn from_virtual(virtual_id: StringId) -> EventId {
45 EventId(virtual_id)
46 }
47
48 /// Create an EventId from a raw u32 value. Only used internally for
49 /// deserialization.
50 #[inline]
51 pub fn from_u32(raw_id: u32) -> EventId {
52 EventId(StringId::new(raw_id))
53 }
54}
55
56pub struct EventIdBuilder<'p, S: SerializationSink> {
57 profiler: &'p Profiler<S>,
58}
59
60impl<'p, S: SerializationSink> EventIdBuilder<'p, S> {
61 pub fn new(profiler: &Profiler<S>) -> EventIdBuilder<'_, S> {
62 EventIdBuilder { profiler }
63 }
64
65 #[inline]
66 pub fn from_label(&self, label: StringId) -> EventId {
67 // Just forward the string ID, a single identifier is a valid event_id
68 EventId::from_label(label)
69 }
70
71 pub fn from_label_and_arg(&self, label: StringId, arg: StringId) -> EventId {
72 EventId(self.profiler.alloc_string(&[
73 // Label
74 StringComponent::Ref(label),
75 // Seperator and start tag for arg
76 StringComponent::Value(SEPARATOR_BYTE),
77 // Arg string id
78 StringComponent::Ref(arg),
79 ]))
80 }
81}