Skip to main content

nautilus_model/ffi/data/
delta.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;
22
23use crate::{
24    data::OrderBookDelta, enums::BookAction, ffi::data::order::BookOrderFfi,
25    identifiers::InstrumentId,
26};
27
28/// The stable C representation of an [`OrderBookDelta`].
29#[repr(C)]
30#[derive(Clone, Copy, Debug)]
31pub struct OrderBookDeltaFfi {
32    pub instrument_id: InstrumentId,
33    pub action: BookAction,
34    pub order: BookOrderFfi,
35    pub flags: u8,
36    pub sequence: u64,
37    pub ts_event: UnixNanos,
38    pub ts_init: UnixNanos,
39}
40
41impl From<OrderBookDeltaFfi> for OrderBookDelta {
42    fn from(value: OrderBookDeltaFfi) -> Self {
43        Self {
44            instrument_id: value.instrument_id,
45            action: value.action,
46            order: value.order.into(),
47            flags: value.flags,
48            sequence: value.sequence,
49            ts_event: value.ts_event,
50            ts_init: value.ts_init,
51        }
52    }
53}
54
55impl From<OrderBookDelta> for OrderBookDeltaFfi {
56    fn from(value: OrderBookDelta) -> Self {
57        Self {
58            instrument_id: value.instrument_id,
59            action: value.action,
60            order: value.order.into(),
61            flags: value.flags,
62            sequence: value.sequence,
63            ts_event: value.ts_event,
64            ts_init: value.ts_init,
65        }
66    }
67}
68
69#[unsafe(no_mangle)]
70#[cfg_attr(feature = "high-precision", allow(improper_ctypes_definitions))]
71pub extern "C" fn orderbook_delta_new(
72    instrument_id: InstrumentId,
73    action: BookAction,
74    order: BookOrderFfi,
75    flags: u8,
76    sequence: u64,
77    ts_event: UnixNanos,
78    ts_init: UnixNanos,
79) -> OrderBookDeltaFfi {
80    OrderBookDelta::new(
81        instrument_id,
82        action,
83        order.into(),
84        flags,
85        sequence,
86        ts_event,
87        ts_init,
88    )
89    .into()
90}
91
92#[unsafe(no_mangle)]
93pub extern "C" fn orderbook_delta_eq(lhs: &OrderBookDeltaFfi, rhs: &OrderBookDeltaFfi) -> u8 {
94    u8::from(OrderBookDelta::from(*lhs) == OrderBookDelta::from(*rhs))
95}
96
97#[unsafe(no_mangle)]
98pub extern "C" fn orderbook_delta_hash(delta: &OrderBookDeltaFfi) -> u64 {
99    let mut hasher = DefaultHasher::new();
100    OrderBookDelta::from(*delta).hash(&mut hasher);
101    hasher.finish()
102}