chronicle_core/events.rs
1//! Execution events and the caller-owned event buffer.
2//!
3//! Events carry only the sequence number of their command, order ids,
4//! prices, and quantities (ARCHITECTURE.md section 7.6). No slot
5//! indices, no timestamps, no pointers: event streams are comparable
6//! byte for byte across processes and across snapshot restore.
7
8use crate::types::OrderId;
9
10/// Why an order was cancelled.
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
12pub enum CancelReason {
13 /// Explicit cancel command from the caller.
14 Requested,
15 /// The participant cancelled all of its orders in one command.
16 ByParticipant,
17 /// Self-trade prevention cancelled this resting order.
18 SelfTradePrevention,
19}
20
21/// One execution event. Every variant carries the `seq` of the command
22/// that produced it.
23#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24pub enum ExecutionEvent {
25 /// An incoming order rested its remainder on the book.
26 Accepted {
27 /// Command sequence number.
28 seq: u64,
29 /// The order that rested.
30 order_id: OrderId,
31 },
32 /// A trade between a resting maker and the incoming taker, at the
33 /// maker price.
34 Trade {
35 /// Command sequence number.
36 seq: u64,
37 /// The resting order.
38 maker_order_id: OrderId,
39 /// The incoming order.
40 taker_order_id: OrderId,
41 /// Trade price in quote units (the maker price).
42 price: u64,
43 /// Traded quantity.
44 qty: u64,
45 },
46 /// An order reached zero remaining quantity by trading.
47 Filled {
48 /// Command sequence number.
49 seq: u64,
50 /// The order that filled.
51 order_id: OrderId,
52 },
53 /// An order left the book by cancellation.
54 Cancelled {
55 /// Command sequence number.
56 seq: u64,
57 /// The cancelled order.
58 order_id: OrderId,
59 /// Why it was cancelled.
60 reason: CancelReason,
61 },
62 /// An order was amended. Price and quantity are the new values.
63 Amended {
64 /// Command sequence number.
65 seq: u64,
66 /// The amended order.
67 order_id: OrderId,
68 /// New raw price in quote units.
69 price: u64,
70 /// New remaining quantity.
71 qty: u64,
72 },
73 /// An immediate-or-cancel order remainder expired unfilled.
74 Expired {
75 /// Command sequence number.
76 seq: u64,
77 /// The expired order.
78 order_id: OrderId,
79 /// Quantity that did not fill.
80 remaining_qty: u64,
81 },
82 /// Self-trade prevention stopped the incoming order at its own
83 /// resting order.
84 SelfTradePrevented {
85 /// Command sequence number.
86 seq: u64,
87 /// The incoming order whose remainder was cancelled.
88 order_id: OrderId,
89 },
90}
91
92/// Caller-owned, pre-allocated event sink for one or more commands.
93///
94/// The buffer borrows a `Vec<ExecutionEvent>` the caller has already
95/// reserved capacity on. The engine checks the remaining capacity in
96/// preflight against the command's worst-case event count and returns
97/// [`crate::BookError::EventBufferFull`] before mutating anything when
98/// it does not fit, so a `push` inside the engine can never grow the
99/// vector. Events written by one command are the tail slice reported by
100/// [`EventBuffer::written`].
101pub struct EventBuffer<'a> {
102 out: &'a mut Vec<ExecutionEvent>,
103 start: usize,
104}
105
106impl<'a> EventBuffer<'a> {
107 /// Wrap a caller vector. Events already in the vector are left
108 /// alone; capacity beyond its current length is the space commands
109 /// may use.
110 #[must_use]
111 pub const fn new(out: &'a mut Vec<ExecutionEvent>) -> Self {
112 let start = out.len();
113 Self { out, start }
114 }
115
116 /// Free event slots: reserved capacity minus current length.
117 #[must_use]
118 pub const fn remaining(&self) -> usize {
119 self.out.capacity() - self.out.len()
120 }
121
122 /// Events written since this buffer was created.
123 #[must_use]
124 pub fn written(&self) -> &[ExecutionEvent] {
125 &self.out[self.start..]
126 }
127
128 /// Append one event. Engine-internal: preflight has already proved
129 /// the capacity suffices, so this never reallocates.
130 pub(crate) fn emit(&mut self, event: ExecutionEvent) {
131 debug_assert!(self.remaining() > 0, "emit beyond preflight bound");
132 self.out.push(event);
133 }
134}