nautilus_model/ffi/data/
depth.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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;
22
23use crate::{
24    data::{
25        depth::{DEPTH10_LEN, OrderBookDepth10},
26        order::BookOrder,
27    },
28    identifiers::InstrumentId,
29};
30
31/// # Safety
32///
33/// - Assumes `bids` and `asks` are valid pointers to arrays of `BookOrder` of length 10.
34/// - Assumes `bid_counts` and `ask_counts` are valid pointers to arrays of `u32` of length 10.
35#[unsafe(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    // Safety: Ensure that `bids_ptr` and `asks_ptr` are valid pointers.
49    // The caller must guarantee that they point to arrays of `BookOrder` of at least `DEPTH10_LEN` length.
50    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 = unsafe { std::slice::from_raw_parts(bids_ptr, DEPTH10_LEN) };
56    let asks_slice = unsafe { 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 = unsafe { std::slice::from_raw_parts(bid_counts_ptr, DEPTH10_LEN) };
61    let ask_counts_slice = unsafe { 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#[unsafe(no_mangle)]
79#[cfg_attr(feature = "high-precision", allow(improper_ctypes_definitions))]
80pub extern "C" fn orderbook_depth10_clone(depth: &OrderBookDepth10) -> OrderBookDepth10 {
81    *depth
82}
83
84#[unsafe(no_mangle)]
85pub extern "C" fn orderbook_depth10_eq(lhs: &OrderBookDepth10, rhs: &OrderBookDepth10) -> u8 {
86    u8::from(lhs == rhs)
87}
88
89#[unsafe(no_mangle)]
90pub extern "C" fn orderbook_depth10_hash(delta: &OrderBookDepth10) -> u64 {
91    let mut hasher = DefaultHasher::new();
92    delta.hash(&mut hasher);
93    hasher.finish()
94}
95
96#[unsafe(no_mangle)]
97pub extern "C" fn orderbook_depth10_bids_array(depth: &OrderBookDepth10) -> *const BookOrder {
98    depth.bids.as_ptr()
99}
100
101#[unsafe(no_mangle)]
102pub extern "C" fn orderbook_depth10_asks_array(depth: &OrderBookDepth10) -> *const BookOrder {
103    depth.asks.as_ptr()
104}
105
106#[unsafe(no_mangle)]
107pub extern "C" fn orderbook_depth10_bid_counts_array(depth: &OrderBookDepth10) -> *const u32 {
108    depth.bid_counts.as_ptr()
109}
110
111#[unsafe(no_mangle)]
112pub extern "C" fn orderbook_depth10_ask_counts_array(depth: &OrderBookDepth10) -> *const u32 {
113    depth.ask_counts.as_ptr()
114}