Skip to main content

moire_types/objects/
scopes.rs

1use facet::Facet;
2
3use crate::{next_scope_id, BacktraceId, PTime, ScopeId};
4
5// r[impl model.scope.fields]
6/// A scope groups execution context over time (for example process/thread/task/connection).
7#[derive(Facet)]
8pub struct Scope {
9    /// Opaque scope identifier.
10    pub id: ScopeId,
11
12    /// When we first started tracking this scope.
13    pub birth: PTime,
14
15    /// Location in source code and crate information.
16    pub backtrace: BacktraceId,
17
18    /// Human-facing name for this scope.
19    pub name: String,
20
21    /// More specific info about the scope.
22    pub body: ScopeBody,
23}
24
25impl Scope {
26    /// Create a new scope: ID and birth time are generated automatically.
27    pub fn new(backtrace: BacktraceId, name: impl Into<String>, body: ScopeBody) -> Scope {
28        Scope {
29            id: next_scope_id(),
30            birth: PTime::now(),
31            backtrace,
32            name: name.into(),
33            body,
34        }
35    }
36}
37
38// r[impl model.scope.kinds]
39#[derive(Facet)]
40#[repr(u8)]
41#[facet(rename_all = "snake_case")]
42pub enum ScopeBody {
43    Process(ProcessScopeBody),
44    Thread(ThreadScopeBody),
45    Task(TaskScopeBody),
46    Connection(ConnectionScopeBody),
47}
48
49#[derive(Facet)]
50pub struct ProcessScopeBody {
51    pub pid: u32,
52}
53
54#[derive(Facet)]
55pub struct ThreadScopeBody {
56    pub thread_name: Option<String>,
57}
58
59#[derive(Facet)]
60pub struct TaskScopeBody {
61    pub task_key: String,
62}
63
64#[derive(Facet)]
65pub struct ConnectionScopeBody {
66    pub local_addr: Option<String>,
67    pub peer_addr: Option<String>,
68}
69
70crate::impl_sqlite_json!(ScopeBody);
71
72crate::declare_scope_body_slots!(
73    ProcessScopeSlot::Process(ProcessScopeBody),
74    ThreadScopeSlot::Thread(ThreadScopeBody),
75    TaskScopeSlot::Task(TaskScopeBody),
76    ConnectionScopeSlot::Connection(ConnectionScopeBody),
77);