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::{DEPTH10_LEN, OrderBookDepth10},
26 order::BookOrder,
27 },
28 identifiers::InstrumentId,
29};
30
31#[unsafe(no_mangle)]
41#[cfg_attr(feature = "high-precision", allow(improper_ctypes_definitions))]
42pub unsafe extern "C" fn orderbook_depth10_new(
43 instrument_id: InstrumentId,
44 bids_ptr: *const BookOrder,
45 asks_ptr: *const BookOrder,
46 bid_counts_ptr: *const u32,
47 ask_counts_ptr: *const u32,
48 flags: u8,
49 sequence: u64,
50 ts_event: UnixNanos,
51 ts_init: UnixNanos,
52) -> OrderBookDepth10 {
53 assert!(!bids_ptr.is_null());
56 assert!(!asks_ptr.is_null());
57 assert!(!bid_counts_ptr.is_null());
58 assert!(!ask_counts_ptr.is_null());
59
60 let bids_slice = unsafe { std::slice::from_raw_parts(bids_ptr, DEPTH10_LEN) };
61 let asks_slice = unsafe { std::slice::from_raw_parts(asks_ptr, DEPTH10_LEN) };
62 let bids: [BookOrder; DEPTH10_LEN] = bids_slice.try_into().expect("Slice length != 10");
63 let asks: [BookOrder; DEPTH10_LEN] = asks_slice.try_into().expect("Slice length != 10");
64
65 let bid_counts_slice = unsafe { std::slice::from_raw_parts(bid_counts_ptr, DEPTH10_LEN) };
66 let ask_counts_slice = unsafe { std::slice::from_raw_parts(ask_counts_ptr, DEPTH10_LEN) };
67 let bid_counts: [u32; DEPTH10_LEN] = bid_counts_slice.try_into().expect("Slice length != 10");
68 let ask_counts: [u32; DEPTH10_LEN] = ask_counts_slice.try_into().expect("Slice length != 10");
69
70 OrderBookDepth10::new(
71 instrument_id,
72 bids,
73 asks,
74 bid_counts,
75 ask_counts,
76 flags,
77 sequence,
78 ts_event,
79 ts_init,
80 )
81}
82
83#[unsafe(no_mangle)]
84#[cfg_attr(feature = "high-precision", allow(improper_ctypes_definitions))]
85pub extern "C" fn orderbook_depth10_clone(depth: &OrderBookDepth10) -> OrderBookDepth10 {
86 *depth
87}
88
89#[unsafe(no_mangle)]
90pub extern "C" fn orderbook_depth10_eq(lhs: &OrderBookDepth10, rhs: &OrderBookDepth10) -> u8 {
91 u8::from(lhs == rhs)
92}
93
94#[unsafe(no_mangle)]
95pub extern "C" fn orderbook_depth10_hash(delta: &OrderBookDepth10) -> u64 {
96 let mut hasher = DefaultHasher::new();
97 delta.hash(&mut hasher);
98 hasher.finish()
99}
100
101#[unsafe(no_mangle)]
102pub extern "C" fn orderbook_depth10_bids_array(depth: &OrderBookDepth10) -> *const BookOrder {
103 depth.bids.as_ptr()
104}
105
106#[unsafe(no_mangle)]
107pub extern "C" fn orderbook_depth10_asks_array(depth: &OrderBookDepth10) -> *const BookOrder {
108 depth.asks.as_ptr()
109}
110
111#[unsafe(no_mangle)]
112pub extern "C" fn orderbook_depth10_bid_counts_array(depth: &OrderBookDepth10) -> *const u32 {
113 depth.bid_counts.as_ptr()
114}
115
116#[unsafe(no_mangle)]
117pub extern "C" fn orderbook_depth10_ask_counts_array(depth: &OrderBookDepth10) -> *const u32 {
118 depth.ask_counts.as_ptr()
119}