eep/traits.rs
1extern crate thread_id;
2
3/// A unique identifier for a thread.
4#[derive(Copy, Clone, Debug, Eq, PartialEq)]
5pub struct ThreadId(pub usize);
6
7impl ThreadId {
8 /// Get the current thread's ID.
9 pub fn get() -> ThreadId {
10 ThreadId(thread_id::get())
11 }
12}
13
14/// A unique identifier for a traced event or start/stop pair.
15///
16/// The pair of `(id.u32(), id.thread())` must be unique across all IDs of a
17/// particalur `TraceId` type. In other words, either:
18///
19/// * `id.u32()` is unique and `id.thread()` can always return `None`, or
20///
21/// * `id.u32()` is only unique within a thread, not globally, and
22/// `id.thread()` always returns `Some` to disambiguate IDs across threads.
23pub trait TraceId: Copy {
24 /// Construct a fresh ID.
25 fn new_id() -> Self;
26
27 /// Turn this `TraceId` into a `u32`.
28 fn u32(&self) -> u32;
29
30 /// Get the ID of the thread upon which this ID's trace was taken.
31 fn thread(&self) -> Option<ThreadId>;
32}
33
34/// TODO FITZGEN
35///
36/// We require `Copy` because we can't run `Drop` implementations, and we don't
37/// want to leak all over.
38pub trait Trace: Copy {
39 type Id: TraceId;
40
41 fn label(u32) -> &'static str;
42 fn tag(&self) -> u32;
43}
44
45/// TODO FITZGEN
46pub trait TraceSink<T>
47 where T: Trace
48{
49 /// Trace a one-off event.
50 fn trace_event(&mut self, trace: T, why: Option<T::Id>) -> T::Id;
51
52 /// Trace the start of an operation.
53 ///
54 /// Finish the trace by calling `trace_stop` with the returned ID.
55 fn trace_start(&mut self, trace: T, why: Option<T::Id>) -> T::Id;
56
57 /// Trace the end of the operation with the given `id`.
58 ///
59 /// Start the trace by calling `trace_start` to obtain an ID.
60 fn trace_stop(&mut self, id: T::Id, trace: T);
61}