Skip to main content

dial9_trace_format/
lib.rs

1pub mod codec;
2pub mod decoder;
3pub mod encoder;
4pub mod leb128;
5pub mod schema;
6pub mod types;
7
8pub use dial9_trace_format_derive::TraceEvent;
9pub use types::EventEncoder;
10pub use types::InternedString;
11pub use types::StackFrames;
12pub use types::TraceField;
13
14use schema::{FieldDef, SchemaEntry};
15use types::FieldValueRef;
16
17/// Trait implemented by `#[derive(TraceEvent)]` for compile-time event types.
18pub trait TraceEvent {
19    /// Decoded form of this event, potentially borrowing from the input buffer.
20    type Ref<'a>;
21
22    /// The event type name (used in schema registration).
23    fn event_name() -> &'static str;
24    /// Field definitions for schema registration.
25    /// When `has_timestamp()` is true, the timestamp is NOT included here —
26    /// it is encoded in the event frame header.
27    fn field_defs() -> Vec<FieldDef>;
28    /// Whether this event type carries a packed timestamp in the event header.
29    fn has_timestamp() -> bool {
30        true
31    }
32    /// Return the event's timestamp in nanoseconds.
33    fn timestamp(&self) -> u64;
34    /// Encode this event's non-timestamp fields into the encoder.
35    fn encode_fields<W: std::io::Write>(
36        &self,
37        enc: &mut types::EventEncoder<'_, W>,
38    ) -> std::io::Result<()>;
39    /// Decode from a slice of zero-copy field values.
40    /// `timestamp_ns` is the absolute timestamp from the event header (if present).
41    fn decode<'a>(timestamp_ns: Option<u64>, fields: &[FieldValueRef<'a>])
42    -> Option<Self::Ref<'a>>;
43
44    /// Build a SchemaEntry for this event type.
45    fn schema_entry() -> SchemaEntry {
46        SchemaEntry {
47            name: Self::event_name().to_string(),
48            has_timestamp: Self::has_timestamp(),
49            fields: Self::field_defs(),
50        }
51    }
52}