pub struct PreTradeLock { /* private fields */ }Expand description
Stable lock context captured during pre-trade reservation.
PreTradeLock is not just a copy of request input. It is the serialized
context of what the engine actually reserved and how that reservation must
later be reconciled.
A lock groups (group_id, price) records under their group identifier.
Pushing several prices for the same group accumulates them in insertion
order; the resulting list is what Self::prices_of returns.
The lock context must travel together with the order lifecycle. If a policy relies on execution report fill details to reconcile the reservation, the lock produced during pre-trade must be stored until the final execution report for that order has been processed. Dropping it too early breaks the engine’s ability to correctly unlock the unused remainder or finalize the reserved state using the same assumptions that were applied when the order was accepted.
§Performance
In practice the overwhelming majority of orders carry only prices for
DEFAULT_POLICY_GROUP_ID. The internal storage keeps the default group in a
dedicated inline-capacity slot, so the hot path neither scans nor allocates.
Non-default groups are stored in a second inline-capacity list of sections,
each section grouping prices for one group identifier. Reads, writes, and
lookups for the default group are O(1); operations for non-default groups
scan the small sections list once.
The concrete representation is intentionally private. Construct values through the provided constructors and inspect them through the returned iterators. This keeps the type free to evolve its internal layout without affecting cross-language bindings.
§Serialization
With the serde feature enabled the lock uses an extremely compact wire
format that emits nothing the receiver does not need:
- no field names, no map keys, no struct tags;
- the default group identifier is implicit (it is the first sublist);
- non-default groups are sent once with all their prices, never repeating the group identifier.
The on-wire shape is a sequence of sublists. The first sublist is the list
of prices for DEFAULT_POLICY_GROUP_ID (it may be empty). Every following
sublist describes one non-default group: its first element is the u16
group identifier, the remaining elements are the prices stored for that
group.
Because the format only uses serialize_seq, the same compactness applies
to every self-describing serde format: JSON (the canonical FFI exchange
format), MessagePack (rmp-serde), CBOR (ciborium), BSON, YAML
(serde_yaml), TOML, RON, Apache Avro, and FlexBuffers. Rust-only
formats such as Bincode and postcard work too but remain outside the
cross-language compatibility contract. The format-specific overhead per
sublist is the array header only (one byte for short arrays in
MessagePack/CBOR, two characters [ and ] in JSON), so the wire size is
dominated by the Price encoding itself.
Same lock, three formats:
// a default-only lock with a single price 185
JSON [["185"]] // 9 bytes
MessagePack 91 91 a3 31 38 35 // 6 bytes (hex)
CBOR 81 81 63 31 38 35 // 6 bytes (hex)
// a mixed lock: default 185, group 7 with two prices 200 and 201
JSON [["185"],[7,"200","201"]] // 25 bytes
MessagePack 92 91 a3 31 38 35 93 07 a3 32 30
30 a3 32 30 31 // 16 bytes (hex)
CBOR 82 81 63 31 38 35 83 07 63 32 30
30 63 32 30 31 // 16 bytes (hex)
// empty lock
JSON [] // 2 bytes
MessagePack 90 // 1 byte
CBOR 80 // 1 byteImplementations§
Source§impl PreTradeLock
impl PreTradeLock
Sourcepub fn from_entries<EntriesIter>(entries: EntriesIter) -> Self
pub fn from_entries<EntriesIter>(entries: EntriesIter) -> Self
Creates a lock context populated from the given (group_id, price)
records.
Records with the same group_id are merged in insertion order, just
like repeated Self::push calls.
Sourcepub fn push(&mut self, policy_group_id: PolicyGroupId, price: Price)
pub fn push(&mut self, policy_group_id: PolicyGroupId, price: Price)
Stores price under policy_group_id.
Prices already stored under the same policy_group_id are preserved; the new
price is appended to the end of that group’s list.
Sourcepub fn push_many<PricesIter>(
&mut self,
policy_group_id: PolicyGroupId,
prices: PricesIter,
)
pub fn push_many<PricesIter>( &mut self, policy_group_id: PolicyGroupId, prices: PricesIter, )
Stores every price under policy_group_id, preserving any prices already
stored under the same group.
Target capacity is computed up-front from the iterator’s exact length, so the destination storage grows at most once regardless of how many prices are pushed.
Sourcepub fn merge(&mut self, other: &Self)
pub fn merge(&mut self, other: &Self)
Appends all entries from other into self.
Prices for the default group are bulk-copied in one operation. For each
non-default group in other, prices are bulk-copied into the matching
existing section or into a new section if none exists yet.
The hot path — a single-entry lock merging into an existing one — costs
exactly one extend_from_slice call with no iteration or allocation.
Sourcepub fn entries(&self) -> Entries<'_>
pub fn entries(&self) -> Entries<'_>
Iterates over every (group_id, price) pair, default-group prices
first, then each non-default group in insertion order.
Sourcepub fn prices_of(&self, policy_group_id: PolicyGroupId) -> PricesByGroup<'_>
pub fn prices_of(&self, policy_group_id: PolicyGroupId) -> PricesByGroup<'_>
Iterates over every price stored under policy_group_id, in insertion order.
Default-group lookups are O(1); non-default lookups scan the section list once. Both return cheap iterators that walk an inline-storage slice with no allocation.
Trait Implementations§
Source§impl Clone for PreTradeLock
impl Clone for PreTradeLock
Source§fn clone(&self) -> PreTradeLock
fn clone(&self) -> PreTradeLock
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for PreTradeLock
impl Debug for PreTradeLock
Source§impl Default for PreTradeLock
impl Default for PreTradeLock
Source§fn default() -> PreTradeLock
fn default() -> PreTradeLock
impl Eq for PreTradeLock
Source§impl Extend<(PolicyGroupId, Price)> for PreTradeLock
impl Extend<(PolicyGroupId, Price)> for PreTradeLock
Source§fn extend<EntriesIter>(&mut self, iter: EntriesIter)
fn extend<EntriesIter>(&mut self, iter: EntriesIter)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl FromIterator<(PolicyGroupId, Price)> for PreTradeLock
impl FromIterator<(PolicyGroupId, Price)> for PreTradeLock
Source§impl PartialEq for PreTradeLock
impl PartialEq for PreTradeLock
Source§fn eq(&self, other: &PreTradeLock) -> bool
fn eq(&self, other: &PreTradeLock) -> bool
self and other values to be equal, and is used by ==.