architect_sdk/
order_id_allocator.rs1use architect_api::OrderId;
2use serde::{Deserialize, Serialize};
3use std::sync::atomic::{AtomicU64, Ordering};
4use uuid::Uuid;
5
6#[derive(Debug, Serialize, Deserialize)]
7pub struct OrderIdAllocator {
8 pub seqid: Uuid,
9 pub seqno: u64,
10}
11
12impl OrderIdAllocator {
13 pub fn new() -> Self {
14 Self { seqid: Uuid::new_v4(), seqno: 0 }
15 }
16
17 pub fn next_order_id(&mut self) -> OrderId {
18 let seqno = self.seqno;
19 self.seqno += 1;
20 OrderId { seqid: self.seqid, seqno }
21 }
22}
23
24impl Default for OrderIdAllocator {
25 fn default() -> Self {
26 Self::new()
27 }
28}
29
30#[derive(Debug, Serialize, Deserialize)]
31pub struct AtomicOrderIdAllocator {
32 pub seqid: Uuid,
33 pub seqno: AtomicU64,
34}
35
36impl Default for AtomicOrderIdAllocator {
37 fn default() -> Self {
38 Self::new()
39 }
40}
41
42impl AtomicOrderIdAllocator {
43 pub fn new() -> Self {
44 Self { seqid: Uuid::new_v4(), seqno: AtomicU64::new(0) }
45 }
46
47 pub fn new_with_seqid(seqid: Uuid) -> Self {
48 Self { seqid, seqno: AtomicU64::new(0) }
49 }
50
51 pub fn next_order_id(&self) -> OrderId {
52 let seqno = self.seqno.fetch_add(1, Ordering::Relaxed);
53 OrderId { seqid: self.seqid, seqno }
54 }
55}