Skip to main content

nautilus_model/ffi/data/
depth.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::{
17    collections::hash_map::DefaultHasher,
18    hash::{Hash, Hasher},
19};
20
21use nautilus_core::{UnixNanos, ffi::abort_on_panic};
22
23use crate::{
24    data::{
25        depth::{DEPTH10_LEN, OrderBookDepth10},
26        order::BookOrder,
27    },
28    identifiers::InstrumentId,
29};
30
31/// # Safety
32///
33/// This function assumes:
34/// - `bids` and `asks` are valid pointers to arrays of `BookOrder` of length 10.
35/// - `bid_counts` and `ask_counts` are valid pointers to arrays of `u32` of length 10.
36///
37/// # Panics
38///
39/// Panics if any input pointer is null or if slice conversion for bids or asks fails.
40#[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    abort_on_panic(|| {
54        // SAFETY: Null checks run before slice construction. The caller still
55        // guarantees each pointer refers to `DEPTH10_LEN` initialized elements.
56        assert!(!bids_ptr.is_null());
57        assert!(!asks_ptr.is_null());
58        assert!(!bid_counts_ptr.is_null());
59        assert!(!ask_counts_ptr.is_null());
60
61        let bids_slice = unsafe { std::slice::from_raw_parts(bids_ptr, DEPTH10_LEN) };
62        let asks_slice = unsafe { std::slice::from_raw_parts(asks_ptr, DEPTH10_LEN) };
63        let bids: [BookOrder; DEPTH10_LEN] = bids_slice.try_into().expect("Slice length != 10");
64        let asks: [BookOrder; DEPTH10_LEN] = asks_slice.try_into().expect("Slice length != 10");
65
66        let bid_counts_slice = unsafe { std::slice::from_raw_parts(bid_counts_ptr, DEPTH10_LEN) };
67        let ask_counts_slice = unsafe { std::slice::from_raw_parts(ask_counts_ptr, DEPTH10_LEN) };
68        let bid_counts: [u32; DEPTH10_LEN] =
69            bid_counts_slice.try_into().expect("Slice length != 10");
70        let ask_counts: [u32; DEPTH10_LEN] =
71            ask_counts_slice.try_into().expect("Slice length != 10");
72
73        OrderBookDepth10::new(
74            instrument_id,
75            bids,
76            asks,
77            bid_counts,
78            ask_counts,
79            flags,
80            sequence,
81            ts_event,
82            ts_init,
83        )
84    })
85}
86
87#[unsafe(no_mangle)]
88#[cfg_attr(feature = "high-precision", allow(improper_ctypes_definitions))]
89pub extern "C" fn orderbook_depth10_clone(depth: &OrderBookDepth10) -> OrderBookDepth10 {
90    *depth
91}
92
93#[unsafe(no_mangle)]
94pub extern "C" fn orderbook_depth10_eq(lhs: &OrderBookDepth10, rhs: &OrderBookDepth10) -> u8 {
95    u8::from(lhs == rhs)
96}
97
98#[unsafe(no_mangle)]
99pub extern "C" fn orderbook_depth10_hash(delta: &OrderBookDepth10) -> u64 {
100    let mut hasher = DefaultHasher::new();
101    delta.hash(&mut hasher);
102    hasher.finish()
103}
104
105#[unsafe(no_mangle)]
106pub extern "C" fn orderbook_depth10_bids_array(depth: &OrderBookDepth10) -> *const BookOrder {
107    depth.bids.as_ptr()
108}
109
110#[unsafe(no_mangle)]
111pub extern "C" fn orderbook_depth10_asks_array(depth: &OrderBookDepth10) -> *const BookOrder {
112    depth.asks.as_ptr()
113}
114
115#[unsafe(no_mangle)]
116pub extern "C" fn orderbook_depth10_bid_counts_array(depth: &OrderBookDepth10) -> *const u32 {
117    depth.bid_counts.as_ptr()
118}
119
120#[unsafe(no_mangle)]
121pub extern "C" fn orderbook_depth10_ask_counts_array(depth: &OrderBookDepth10) -> *const u32 {
122    depth.ask_counts.as_ptr()
123}