nautilus_model/ffi/data/
depth.rs1use std::{
17 collections::hash_map::DefaultHasher,
18 hash::{Hash, Hasher},
19};
20
21use nautilus_core::UnixNanos;
22
23use crate::{
24 data::{
25 depth::{OrderBookDepth10, DEPTH10_LEN},
26 order::BookOrder,
27 },
28 identifiers::InstrumentId,
29};
30
31#[no_mangle]
36#[cfg_attr(feature = "high-precision", allow(improper_ctypes_definitions))]
37pub unsafe extern "C" fn orderbook_depth10_new(
38 instrument_id: InstrumentId,
39 bids_ptr: *const BookOrder,
40 asks_ptr: *const BookOrder,
41 bid_counts_ptr: *const u32,
42 ask_counts_ptr: *const u32,
43 flags: u8,
44 sequence: u64,
45 ts_event: UnixNanos,
46 ts_init: UnixNanos,
47) -> OrderBookDepth10 {
48 assert!(!bids_ptr.is_null());
51 assert!(!asks_ptr.is_null());
52 assert!(!bid_counts_ptr.is_null());
53 assert!(!ask_counts_ptr.is_null());
54
55 let bids_slice = std::slice::from_raw_parts(bids_ptr, DEPTH10_LEN);
56 let asks_slice = std::slice::from_raw_parts(asks_ptr, DEPTH10_LEN);
57 let bids: [BookOrder; DEPTH10_LEN] = bids_slice.try_into().expect("Slice length != 10");
58 let asks: [BookOrder; DEPTH10_LEN] = asks_slice.try_into().expect("Slice length != 10");
59
60 let bid_counts_slice = std::slice::from_raw_parts(bid_counts_ptr, DEPTH10_LEN);
61 let ask_counts_slice = std::slice::from_raw_parts(ask_counts_ptr, DEPTH10_LEN);
62 let bid_counts: [u32; DEPTH10_LEN] = bid_counts_slice.try_into().expect("Slice length != 10");
63 let ask_counts: [u32; DEPTH10_LEN] = ask_counts_slice.try_into().expect("Slice length != 10");
64
65 OrderBookDepth10::new(
66 instrument_id,
67 bids,
68 asks,
69 bid_counts,
70 ask_counts,
71 flags,
72 sequence,
73 ts_event,
74 ts_init,
75 )
76}
77
78#[no_mangle]
79pub extern "C" fn orderbook_depth10_eq(lhs: &OrderBookDepth10, rhs: &OrderBookDepth10) -> u8 {
80 u8::from(lhs == rhs)
81}
82
83#[no_mangle]
84pub extern "C" fn orderbook_depth10_hash(delta: &OrderBookDepth10) -> u64 {
85 let mut hasher = DefaultHasher::new();
86 delta.hash(&mut hasher);
87 hasher.finish()
88}
89
90#[no_mangle]
91pub extern "C" fn orderbook_depth10_bids_array(depth: &OrderBookDepth10) -> *const BookOrder {
92 depth.bids.as_ptr()
93}
94
95#[no_mangle]
96pub extern "C" fn orderbook_depth10_asks_array(depth: &OrderBookDepth10) -> *const BookOrder {
97 depth.asks.as_ptr()
98}
99
100#[no_mangle]
101pub extern "C" fn orderbook_depth10_bid_counts_array(depth: &OrderBookDepth10) -> *const u32 {
102 depth.bid_counts.as_ptr()
103}
104
105#[no_mangle]
106pub extern "C" fn orderbook_depth10_ask_counts_array(depth: &OrderBookDepth10) -> *const u32 {
107 depth.ask_counts.as_ptr()
108}